@@ -0,0 +1,84 @@
|
||||
package dramaser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
redisutil "91porn-server/common/redis"
|
||||
)
|
||||
|
||||
type fakeDramaFeedRedis struct {
|
||||
calls int
|
||||
run func(int, *redisutil.Script, []string, []interface{}) (interface{}, error)
|
||||
}
|
||||
|
||||
func (f *fakeDramaFeedRedis) RunScriptContext(
|
||||
_ context.Context,
|
||||
script *redisutil.Script,
|
||||
keys []string,
|
||||
args ...interface{},
|
||||
) (interface{}, error) {
|
||||
f.calls++
|
||||
return f.run(f.calls, script, keys, args)
|
||||
}
|
||||
|
||||
func TestReserveDramaFeedPagePublishesOnceAndReturnsRingSegment(t *testing.T) {
|
||||
ids := []string{"a", "b", "c", "d"}
|
||||
client := &fakeDramaFeedRedis{}
|
||||
client.run = func(call int, script *redisutil.Script, keys []string, args []interface{}) (interface{}, error) {
|
||||
switch call {
|
||||
case 1:
|
||||
if script != reserveDramaFeedPageScript {
|
||||
t.Fatal("first call should reserve existing queue")
|
||||
}
|
||||
return []interface{}{"QUEUE_MISSING"}, nil
|
||||
case 2:
|
||||
if script != publishDramaFeedQueueScript || keys[0] != "recommend:drama:queue:20260826" {
|
||||
t.Fatalf("unexpected publish call: keys=%v", keys)
|
||||
}
|
||||
if len(args) != len(ids)+1 {
|
||||
t.Fatalf("publish args = %d, want %d", len(args), len(ids)+1)
|
||||
}
|
||||
return int64(1), nil
|
||||
case 3:
|
||||
if script != reserveDramaFeedPageScript {
|
||||
t.Fatal("third call should reserve published queue")
|
||||
}
|
||||
token := fmt.Sprint(args[2])
|
||||
return []interface{}{"RESERVED", "20260826", "4", "2", "2", token, "c", "d"}, nil
|
||||
default:
|
||||
t.Fatalf("unexpected Redis call %d", call)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
reservation, err := reserveDramaFeedPage(context.Background(), client, 7, 2, "20260826", ids)
|
||||
if err != nil {
|
||||
t.Fatalf("reserveDramaFeedPage() error = %v", err)
|
||||
}
|
||||
if reservation.Offset != 2 || reservation.Reserved != 2 ||
|
||||
fmt.Sprint(reservation.IDs) != "[c d]" {
|
||||
t.Fatalf("reservation = %+v", reservation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommitDramaFeedPageAdvancesOnlyConsumedPrefix(t *testing.T) {
|
||||
reservation := dramaFeedReservation{
|
||||
Version: "20260826", Length: 20, Offset: 5, Reserved: 10,
|
||||
Token: "token", IDs: []string{"a", "b"},
|
||||
}
|
||||
client := &fakeDramaFeedRedis{}
|
||||
client.run = func(call int, script *redisutil.Script, keys []string, args []interface{}) (interface{}, error) {
|
||||
if script != commitDramaFeedPageScript {
|
||||
t.Fatal("expected commit script")
|
||||
}
|
||||
if keys[1] != "recommend:drama:offset:20260826" || fmt.Sprint(args[5]) != "4" {
|
||||
t.Fatalf("commit keys/args mismatch: keys=%v args=%v", keys, args)
|
||||
}
|
||||
return int64(1), nil
|
||||
}
|
||||
if err := commitDramaFeedPage(context.Background(), client, 7, reservation, 4); err != nil {
|
||||
t.Fatalf("commitDramaFeedPage() error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user