257 lines
7.9 KiB
Go
257 lines
7.9 KiB
Go
package monthlyusagehistory
|
|
|
|
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, "monthly-history:")
|
|
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 monthlyMessage(runID string, channels ...string) model.TrafficUsageMonthlyHistoryStartMessage {
|
|
message := model.TrafficUsageMonthlyHistoryStartMessage{
|
|
RunID: runID,
|
|
Operation: FullSyncOperation,
|
|
QueryMonth: "202609",
|
|
TriggeredAt: "2026-10-10T06:00:00+08:00",
|
|
}
|
|
if len(channels) > 0 {
|
|
message.Scope = &model.SyncScope{Channels: channels}
|
|
}
|
|
return message
|
|
}
|
|
|
|
func TestProduceCarriesQueryMonthAndPackageCode(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(), monthlyMessage("monthly-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":"202609"`) {
|
|
t.Fatalf("task member lost the monthly 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("monthly-1", FullSyncOperation, 42)
|
|
if got := zset.StableTaskID("monthly-1", FullSyncOperation, 42); got != want {
|
|
t.Fatalf("StableTaskID is not deterministic: %s vs %s", got, want)
|
|
}
|
|
if err := producer.Produce(context.Background(), monthlyMessage("monthly-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(), monthlyMessage("monthly-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["monthly-history:217"] != 2 {
|
|
t.Fatalf("enqueue batches = %+v, want 2 tasks on monthly: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(), monthlyMessage("monthly-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(), monthlyMessage("monthly-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 := monthlyMessage("monthly-bad")
|
|
message.QueryMonth = ""
|
|
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(), monthlyMessage("monthly-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")
|
|
}
|
|
}
|