144 lines
4.0 KiB
Go
144 lines
4.0 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/fullsync/contract"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/queue/zset"
|
|
)
|
|
|
|
type runtimeRedisStub struct {
|
|
responses []*gvar.Var
|
|
}
|
|
|
|
func (s *runtimeRedisStub) Do(context.Context, string, ...any) (*gvar.Var, error) {
|
|
if len(s.responses) == 0 {
|
|
return gvar.New(nil), nil
|
|
}
|
|
response := s.responses[0]
|
|
s.responses = s.responses[1:]
|
|
return response, nil
|
|
}
|
|
|
|
func (s *runtimeRedisStub) Eval(context.Context, string, int64, []string, []any) (*gvar.Var, error) {
|
|
return gvar.New(int64(1)), nil
|
|
}
|
|
|
|
func newRuntimeRepository(t *testing.T, responses ...*gvar.Var) *Repository {
|
|
t.Helper()
|
|
store, err := zset.New(&runtimeRedisStub{responses: responses}, "flow:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repository, err := New(store, t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return repository
|
|
}
|
|
|
|
func TestListArchiveWorkParsesNestedRedisScanResult(t *testing.T) {
|
|
repository := newRuntimeRepository(t, gvar.New([]any{
|
|
"0",
|
|
[]any{"run-1", "17", "run-2", "18"},
|
|
}))
|
|
|
|
page, err := repository.ListArchiveWork(context.Background(), contract.PageRequest{Limit: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(page.Items) != 2 {
|
|
t.Fatalf("archive work count = %d, want 2", len(page.Items))
|
|
}
|
|
if page.Items[0].RunID != "run-1" || page.Items[0].Version != "17" {
|
|
t.Fatalf("first archive work = %+v", page.Items[0])
|
|
}
|
|
if page.Items[1].RunID != "run-2" || page.Items[1].Version != "18" {
|
|
t.Fatalf("second archive work = %+v", page.Items[1])
|
|
}
|
|
}
|
|
|
|
func TestGetRoundStatsReadsRunHashJSON(t *testing.T) {
|
|
raw, err := json.Marshal([]zset.RoundStats{{RunID: "run-1", Round: 2, EnqueuedCount: 3, SuccessCount: 2, FailedCount: 1}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repository := newRuntimeRepository(t, gvar.New([]any{
|
|
"runId", "run-1", "status", "completed", "roundStats", string(raw),
|
|
}))
|
|
|
|
got, err := repository.GetRoundStats(context.Background(), "run-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].Round != 2 || got[0].SuccessCount != 2 || got[0].FailedCount != 1 {
|
|
t.Fatalf("unexpected round stats: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestValidateRoundAccountingRejectsUnbalancedRound(t *testing.T) {
|
|
err := validateRoundAccounting(contract.RunMeta{RunID: "run-1", SuccessCount: 1, FailedCount: 0}, []contract.RoundStats{{RunID: "run-1", Round: 1, EnqueuedCount: 2, SuccessCount: 1}})
|
|
if err == nil {
|
|
t.Fatal("expected unbalanced round error")
|
|
}
|
|
}
|
|
|
|
func TestListArchiveWorkHandlesEmptyRedisScanResult(t *testing.T) {
|
|
repository := newRuntimeRepository(t, gvar.New([]any{"0", []any{}}))
|
|
|
|
page, err := repository.ListArchiveWork(context.Background(), contract.PageRequest{Limit: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(page.Items) != 0 {
|
|
t.Fatalf("archive work = %+v, want empty", page.Items)
|
|
}
|
|
}
|
|
|
|
func TestListFailuresParsesNestedRedisScanResult(t *testing.T) {
|
|
failedAt := time.Date(2026, 9, 21, 6, 0, 0, 0, time.UTC)
|
|
record := zset.FailureRecord{
|
|
TaskID: "task-1",
|
|
CardID: 42,
|
|
FailureType: "business",
|
|
Message: "upstream rejected request",
|
|
Round: 3,
|
|
LastFailedAt: failedAt,
|
|
}
|
|
payload, err := json.Marshal(record)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repository := newRuntimeRepository(t, gvar.New([]any{
|
|
"0",
|
|
[]any{"task-1", string(payload)},
|
|
}))
|
|
|
|
page, err := repository.ListFailures(context.Background(), "run-1", contract.PageRequest{Limit: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(page.Items) != 1 {
|
|
t.Fatalf("failure count = %d, want 1", len(page.Items))
|
|
}
|
|
if page.Items[0].TaskID != "task-1" || page.Items[0].CardID != 42 || page.Items[0].Round != 3 {
|
|
t.Fatalf("failure = %+v", page.Items[0])
|
|
}
|
|
}
|
|
|
|
func TestListArchiveWorkRejectsOddRedisScanEntries(t *testing.T) {
|
|
repository := newRuntimeRepository(t, gvar.New([]any{
|
|
"0",
|
|
[]any{"run-without-score"},
|
|
}))
|
|
|
|
if _, err := repository.ListArchiveWork(context.Background(), contract.PageRequest{Limit: 10}); err == nil {
|
|
t.Fatal("ListArchiveWork() error = nil, want malformed response error")
|
|
}
|
|
}
|