Files
iotlcm/internal/fullsync/runtime/simple_replay_max_rounds_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

122 lines
4.4 KiB
Go

package runtime
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/fullsync/contract"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/queue/zset"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/testutil"
)
// seedReplaySource writes a completed source run with n failures into store.
func seedReplaySource(t *testing.T, redis zset.RedisClient, store *zset.Store, name string, n int) {
t.Helper()
ctx := context.Background()
key, _ := store.RunMetaKey(name)
failed, _ := store.FailedKey(name)
if _, err := redis.Do(ctx, "HSET", key, "runId", name, "operation", "usage", "status", "completed_with_failures", "producerDone", "1", "currentRound", "1", "failedCount", fmt.Sprint(n)); err != nil {
t.Fatal(err)
}
for i := 1; i <= n; i++ {
rec := zset.FailureRecord{TaskID: fmt.Sprintf("task-%d", i), CardID: int64(i), ICCID: fmt.Sprintf("iccid-%d", i), ChanKey: "ch", TempKey: "template", Operation: "usage", FailureType: "mobile_busy"}
payload, _ := json.Marshal(rec)
if _, err := redis.Do(ctx, "HSET", failed, rec.TaskID, string(payload)); err != nil {
t.Fatal(err)
}
}
}
// awaitTargetMaxRound waits for the handoff to create the target and returns
// the persisted MaxRound of the replayed Run.
func awaitTargetMaxRound(t *testing.T, svc *SimpleReplay, store *zset.Store, replayRunID string) int {
t.Helper()
ctx := context.Background()
deadline := time.Now().Add(4 * time.Second)
for time.Now().Before(deadline) {
meta, err := store.GetRunMeta(ctx, replayRunID)
if err == nil && meta.MaxRound > 0 && meta.ProducerDone {
return meta.MaxRound
}
time.Sleep(20 * time.Millisecond)
}
meta, err := store.GetRunMeta(ctx, replayRunID)
t.Fatalf("target never settled: %+v %v", meta, err)
return 0
}
func TestReplayMaxRoundsPrefersRequestThenConfig(t *testing.T) {
redis := testutil.Redis(t)
ctx := context.Background()
store, _ := zset.New(redis, "flow:")
// Use a default that differs from DefaultReplayMaxRounds so the assertion
// proves the injected config value is used, not the built-in constant.
svc, err := NewSimpleReplayWithDefault(5, store)
if err != nil {
t.Fatal(err)
}
seedReplaySource(t, redis, store, "flow:default-source", 2)
receipt, err := svc.Request(ctx, "flow:default-source", "42", 0)
if err != nil {
t.Fatal(err)
}
if got := awaitTargetMaxRound(t, svc, store, receipt.ReplayRunID); got != 5 {
t.Fatalf("unspecified request must inherit config default 5, got %d", got)
}
seedReplaySource(t, redis, store, "flow:override-source", 2)
override, err := svc.Request(ctx, "flow:override-source", "42", 7)
if err != nil {
t.Fatal(err)
}
if got := awaitTargetMaxRound(t, svc, store, override.ReplayRunID); got != 7 {
t.Fatalf("explicit request must win over config, got %d", got)
}
}
func TestReplayMaxRoundsRejectsOutOfRange(t *testing.T) {
redis := testutil.Redis(t)
ctx := context.Background()
store, _ := zset.New(redis, "flow:")
svc, _ := NewSimpleReplay(store)
seedReplaySource(t, redis, store, "flow:source", 1)
for _, requested := range []int{-1, MaxReplayRounds + 1} {
if _, err := svc.Request(ctx, "flow:source", "42", requested); err != contract.ErrInvalidArgument {
t.Fatalf("maxRounds=%d must be rejected, got %v", requested, err)
}
}
// The rejected requests must not have reserved the source.
if got, err := redis.Do(ctx, "HGET", "flow:{flow:source}:meta", "replayTargetId"); err == nil && got != nil && got.String() != "" {
t.Fatalf("rejected request must not reserve the source: %v", got.String())
}
}
// A worker restart re-enters through reconcile() without caller arguments, so
// the budget must survive on the reservation rather than come from the caller.
func TestReplayMaxRoundsSurvivesRestartRecovery(t *testing.T) {
redis := testutil.Redis(t)
ctx := context.Background()
store, _ := zset.New(redis, "flow:")
seedReplaySource(t, redis, store, "flow:source", 2)
// Reserve with an explicit budget, then simulate a restart by building a
// fresh service whose configured default differs from the reservation.
first, _ := NewSimpleReplayWithDefault(1, store)
receipt, err := first.Request(ctx, "flow:source", "42", 5)
if err != nil {
t.Fatal(err)
}
restarted, _ := NewSimpleReplayWithDefault(9, store)
restarted.reconcile(ctx)
if got := awaitTargetMaxRound(t, restarted, store, receipt.ReplayRunID); got != 5 {
t.Fatalf("recovery must use the persisted budget 5, not the new default 9: got %d", got)
}
}