46 lines
1.9 KiB
Go
46 lines
1.9 KiB
Go
package zset
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/testutil"
|
|
)
|
|
|
|
func TestProducerStateReadView(t *testing.T) {
|
|
store, err := New(testutil.Redis(t), "producer-state:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
if _, err = store.ReadProducerState(ctx, "missing", "a"); !errors.Is(err, ErrRunNotFound) {
|
|
t.Fatalf("missing run err=%v", err)
|
|
}
|
|
if err = store.CreateRun(ctx, RunMeta{RunID: "state", Status: "producing", ActiveCount: 3, InflightCount: 2}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
metaKey, _ := store.RunMetaKey("state")
|
|
_, _ = store.RedisClient().Do(ctx, "HSET", metaKey, "blocked:a", "1")
|
|
state, err := store.ReadProducerState(ctx, "state", "a")
|
|
if err != nil || state.ActiveCount != 3 || state.ProducerDone || state.CurrentRound != 1 || !state.Blocked {
|
|
t.Fatalf("state=%+v err=%v", state, err)
|
|
}
|
|
// Every state deduplicates admission, including terminal and leased tasks.
|
|
statesKey, _ := store.TaskStateKey("state")
|
|
_, _ = store.RedisClient().Do(ctx, "HSET", statesKey, "success", "succeeded", "leased", "claimed", "waiting", "waiting_next_round")
|
|
exists, err := store.ExistingRunTasks(ctx, "state", []Task{{TaskID: "success", RunID: "state"}, {TaskID: "leased", RunID: "state"}, {TaskID: "waiting", RunID: "state"}, {TaskID: "new", RunID: "state"}})
|
|
if err != nil || len(exists) != 4 || !exists[0] || !exists[1] || !exists[2] || exists[3] {
|
|
t.Fatalf("exists=%v err=%v", exists, err)
|
|
}
|
|
if _, err = store.ExistingRunTasks(ctx, "state", []Task{{RunID: "foreign", TaskID: "x"}}); err == nil {
|
|
t.Fatal("foreign task accepted")
|
|
}
|
|
for _, args := range [][]any{{"currentRound", 0}, {"currentRound", 1, "activeCount", -1}} {
|
|
_, _ = store.RedisClient().Do(ctx, "HSET", append([]any{metaKey}, args...)...)
|
|
if _, err = store.ReadProducerState(ctx, "state", "a"); err == nil {
|
|
t.Fatal("invalid run counters accepted")
|
|
}
|
|
}
|
|
}
|