Files
iotlcm/internal/fullsync/contract/keys_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

117 lines
4.3 KiB
Go

package contract_test
import (
"context"
"errors"
"strings"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/fullsync/contract"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/queue/zset"
"github.com/gogf/gf/v2/container/gvar"
)
type unusedRedis struct{}
func (unusedRedis) Do(context.Context, string, ...any) (*gvar.Var, error) {
panic("naming contracts must not access Redis")
}
func (unusedRedis) Eval(context.Context, string, int64, []string, []any) (*gvar.Var, error) {
panic("naming contracts must not access Redis")
}
func TestKeysMatchExistingQueue(t *testing.T) {
for _, prefix := range []string{"", "flow:", "sim-status:", "custom-prefix"} {
keys, err := contract.NewKeys(prefix)
if err != nil {
t.Fatal(err)
}
store, err := zset.New(unusedRedis{}, prefix)
if err != nil {
t.Fatal(err)
}
for _, pair := range []struct {
old, next func(string) (string, error)
}{
{store.RunMetaKey, keys.RunMetaKey}, {store.RunChannelsKey, keys.RunChannelsKey},
{store.TaskStateKey, keys.TaskStateKey}, {store.FailedKey, keys.FailedKey},
} {
old, oldErr := pair.old("run:1")
next, nextErr := pair.next("run:1")
if oldErr != nil || nextErr != nil || old != next || !strings.Contains(next, "{run:1}") {
t.Errorf("run naming changed: old=%q new=%q errors=%v/%v", old, next, oldErr, nextErr)
}
}
for _, pair := range []struct {
old, next func(string, int, string) (string, error)
}{
{store.RoundActiveKey, keys.RoundActiveKey}, {store.RoundWaitingKey, keys.RoundWaitingKey},
{store.RoundLeaseKey, keys.RoundLeaseKey},
} {
old, oldErr := pair.old("run:1", 3, "channel")
next, nextErr := pair.next("run:1", 3, "channel")
if oldErr != nil || nextErr != nil || old != next {
t.Errorf("round naming changed: old=%q new=%q errors=%v/%v", old, next, oldErr, nextErr)
}
}
if keys.ActiveRunsKey() != store.ActiveRunsKey() || keys.FailedRunsKey() != store.FailedRunsKey() {
t.Fatal("existing global index names changed")
}
}
}
func TestNewKeysAndClusterBoundaries(t *testing.T) {
keys, _ := contract.NewKeys("flow:")
stats, _ := keys.RoundStatsKey("run", 2)
archive, _ := keys.ArchiveStateKey("run")
if stats != "flow:{run}:round:2:stats" || archive != "flow:{run}:archive" {
t.Fatalf("new run keys changed: %s, %s", stats, archive)
}
action, _ := keys.ReplayActionKey("a")
lease, _ := keys.ReplayLeaseKey("a")
idempotency, _ := keys.ReplayIdempotencyKey("source", "a{different-slot}:key")
for _, key := range []string{action, lease, idempotency, keys.PendingReplayActionsKey()} {
if !strings.HasPrefix(key, "flow:{replay}:") || strings.Count(key, "{") != 1 {
t.Fatalf("replay key escaped action slot: %s", key)
}
}
if action != "flow:{replay}:action:a:meta" || lease != "flow:{replay}:action:a:lease" {
t.Fatal("action naming changed")
}
for _, key := range []string{keys.ActiveRunsKey(), keys.FailedRunsKey(), keys.AllRunsKey(), keys.PendingArchivesKey()} {
if strings.ContainsAny(key, "{}") {
t.Fatalf("global index unexpectedly shares run slot: %s", key)
}
}
first, _ := keys.ReplayIdempotencyKey("a:b", "c")
second, _ := keys.ReplayIdempotencyKey("a", "b:c")
repeat, _ := keys.ReplayIdempotencyKey("a:b", "c")
otherSource, _ := keys.ReplayIdempotencyKey("other", "c")
if first == second || first == otherSource || first != repeat {
t.Fatal("idempotency scope is ambiguous or unstable")
}
}
func TestInvalidKeyParts(t *testing.T) {
keys := contract.Keys{}
for _, id := range []string{"", " ", "trim ", "{run}", "run}", "run/path", "run\\path", "run\n", "invalid\xff"} {
if _, err := keys.RunMetaKey(id); !errors.Is(err, contract.ErrInvalidArgument) {
t.Errorf("invalid ID %q returned %v", id, err)
}
}
if _, err := contract.NewKeys("prefix:{wrong}:"); !errors.Is(err, contract.ErrInvalidArgument) {
t.Fatalf("invalid prefix returned %v", err)
}
if _, err := keys.RoundStatsKey("run", 0); !errors.Is(err, contract.ErrInvalidArgument) {
t.Fatalf("invalid round returned %v", err)
}
if _, err := keys.RoundActiveKey("run", 1, " "); !errors.Is(err, contract.ErrInvalidArgument) {
t.Fatalf("invalid channel returned %v", err)
}
if key, err := keys.RunMetaKey("run"); err != nil || key != "flow:{run}:meta" {
t.Fatalf("zero-value keys differ from legacy default: %s %v", key, err)
}
}