Files
iotlcm/internal/fullsync/runtime/runtime_test.go
T
MiFaZhan 2bec257289 chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。

- Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件)
- 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、
  库名 xiujie_admin → iotlcm、哨兵名占位
- 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异,
  不破坏「校验 MSISDN 格式」等测试断言)
- 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像
  digest 记录
- Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23),
  移除写死的 git 凭据
- README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0
  LICENSE,LF 行尾 + .gitattributes

已知:3 个测试包在源仓同一提交上同样失败(client/upstream、
service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
2026-10-22 16:37:56 +08:00

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")
}
}