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),属既有缺陷,本仓未引入亦未修复。
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
package dailyusage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/queue/zset"
|
||||
)
|
||||
|
||||
type pageScanner struct {
|
||||
cards []Card
|
||||
}
|
||||
|
||||
func (s *pageScanner) SnapshotMaxID(context.Context) (int64, error) {
|
||||
if len(s.cards) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return s.cards[len(s.cards)-1].ID, nil
|
||||
}
|
||||
|
||||
func (s *pageScanner) Scan(_ context.Context, lastID, _ int64, _ int) ([]Card, error) {
|
||||
if lastID != 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return s.cards, nil
|
||||
}
|
||||
|
||||
// producerRedis records the enqueued members so a test can assert both the
|
||||
// batch shape and the exact task payload (period and package code).
|
||||
type producerRedis struct {
|
||||
mu sync.Mutex
|
||||
members []string
|
||||
batches map[string]int
|
||||
}
|
||||
|
||||
func (r *producerRedis) Do(_ context.Context, command string, _ ...any) (*gvar.Var, error) {
|
||||
switch strings.ToUpper(command) {
|
||||
case "SET":
|
||||
return gvar.New("OK"), nil
|
||||
case "SMEMBERS":
|
||||
return gvar.New([]string{}), nil
|
||||
case "HGETALL":
|
||||
// No stored run metadata: the producer treats this as a first delivery.
|
||||
return gvar.New(map[string]string{}), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected command %s", command)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *producerRedis) Eval(_ context.Context, _ string, numKeys int64, keys []string, args []any) (*gvar.Var, error) {
|
||||
if numKeys == 1 {
|
||||
return gvar.New(int64(1)), nil
|
||||
}
|
||||
if numKeys != 3 {
|
||||
return nil, fmt.Errorf("unexpected key count %d", numKeys)
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.batches == nil {
|
||||
r.batches = make(map[string]int)
|
||||
}
|
||||
count := len(args) - 3
|
||||
r.batches[keys[0]] += count
|
||||
for _, arg := range args[3:] {
|
||||
if member, ok := arg.(string); ok {
|
||||
r.members = append(r.members, member)
|
||||
}
|
||||
}
|
||||
return gvar.New(count), nil
|
||||
}
|
||||
|
||||
func newProducer(t *testing.T, cards []Card) (*Producer, *producerRedis) {
|
||||
t.Helper()
|
||||
redis := &producerRedis{batches: make(map[string]int)}
|
||||
store, err := zset.New(redis, "daily:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
producer, err := New(&pageScanner{cards: cards}, store, Config{PageSize: 100})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return producer, redis
|
||||
}
|
||||
|
||||
func dailyMessage(runID string, channels ...string) model.TrafficUsageDailyStartMessage {
|
||||
message := model.TrafficUsageDailyStartMessage{
|
||||
RunID: runID,
|
||||
Operation: FullSyncOperation,
|
||||
QueryDate: "20261009",
|
||||
TriggeredAt: "2026-10-10T06:00:00+08:00",
|
||||
}
|
||||
if len(channels) > 0 {
|
||||
message.Scope = &model.SyncScope{Channels: channels}
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
func TestProduceCarriesQueryDateAndPackageCode(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "89860001", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "PKG-A", Operator: 1},
|
||||
})
|
||||
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.members) != 1 {
|
||||
t.Fatalf("members = %+v, want exactly one task", redis.members)
|
||||
}
|
||||
member := redis.members[0]
|
||||
// The period rides in queryMonth because the queue only copies that field.
|
||||
if !strings.Contains(member, `"queryMonth":"20261009"`) {
|
||||
t.Fatalf("task member lost the daily period: %s", member)
|
||||
}
|
||||
// The package code must reach the TDengine child table via the task.
|
||||
if !strings.Contains(member, `"packageCode":"PKG-A"`) {
|
||||
t.Fatalf("task member lost the package code: %s", member)
|
||||
}
|
||||
if !strings.Contains(member, `"operation":"`+FullSyncOperation+`"`) {
|
||||
t.Fatalf("task member lost the operation: %s", member)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceUsesStableTaskIDForIdempotency(t *testing.T) {
|
||||
producer, _ := newProducer(t, []Card{
|
||||
{ID: 42, ICCID: "89860042", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "PKG-A"},
|
||||
})
|
||||
want := zset.StableTaskID("daily-1", FullSyncOperation, 42)
|
||||
if got := zset.StableTaskID("daily-1", FullSyncOperation, 42); got != want {
|
||||
t.Fatalf("StableTaskID is not deterministic: %s vs %s", got, want)
|
||||
}
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceScopeEnqueuesOnlySelectedChannels(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
{ID: 2, ICCID: "2", ChanKey: "173", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
{ID: 3, ICCID: "3", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
{ID: 4, ICCID: "4", ChanKey: "189", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
})
|
||||
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-scoped", "217")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.batches) != 1 {
|
||||
t.Fatalf("enqueue batches = %+v, want only the 217 channel", redis.batches)
|
||||
}
|
||||
if redis.batches["daily:217"] != 2 {
|
||||
t.Fatalf("enqueue batches = %+v, want 2 tasks on daily:217", redis.batches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceFullScopeKeepsEveryChannel(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
{ID: 2, ICCID: "2", ChanKey: "173", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
})
|
||||
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-full")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.batches) != 2 {
|
||||
t.Fatalf("enqueue batches = %+v, want both channels for a full sync", redis.batches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceScopeDoesNotOverrideSyncDisabled(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
{ID: 2, ICCID: "2", ChanKey: "173", TempKey: "sim_yd_v5", PackageCode: "P", SyncEnabled: true},
|
||||
})
|
||||
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-scoped", "173")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.batches) != 0 {
|
||||
t.Fatalf("enqueue batches = %+v, want none (selected channel is sync-disabled)", redis.batches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceRejectsMissingPeriod(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
})
|
||||
message := dailyMessage("daily-bad")
|
||||
message.QueryDate = ""
|
||||
if err := producer.Produce(context.Background(), message); err == nil {
|
||||
t.Fatal("expected a missing queryDate to be rejected")
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.batches) != 0 {
|
||||
t.Fatalf("rejected message must not enqueue: %+v", redis.batches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceRejectsInvalidScope(t *testing.T) {
|
||||
producer, redis := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
})
|
||||
if err := producer.Produce(context.Background(), dailyMessage("daily-bad", "invalid/chan")); err == nil {
|
||||
t.Fatal("expected invalid chanKey to be rejected")
|
||||
}
|
||||
redis.mu.Lock()
|
||||
defer redis.mu.Unlock()
|
||||
if len(redis.batches) != 0 {
|
||||
t.Fatalf("rejected scope must not enqueue: %+v", redis.batches)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProduceScopeDigestIsOrderIndependent(t *testing.T) {
|
||||
first, err := model.NormalizeScope(&model.SyncScope{Channels: []string{"173", "217"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := model.NormalizeScope(&model.SyncScope{Channels: []string{"217", "173"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if scopeDigest(first) != scopeDigest(second) {
|
||||
t.Fatalf("digest depends on channel order: %s vs %s", scopeDigest(first), scopeDigest(second))
|
||||
}
|
||||
if scopeDigest(nil) != "" {
|
||||
t.Fatal("a full sync must have an empty digest")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunAlreadyFinishedTreatsMissingRunAsFirstDelivery(t *testing.T) {
|
||||
producer, _ := newProducer(t, []Card{
|
||||
{ID: 1, ICCID: "1", ChanKey: "217", TempKey: "sim_yd_v5", PackageCode: "P"},
|
||||
})
|
||||
finished, err := producer.runAlreadyFinished(context.Background(), "fresh")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if finished {
|
||||
t.Fatal("a run without stored metadata must not be reported as finished")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user