Files
iotlcm/internal/fullsync/supply/observation_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

135 lines
4.3 KiB
Go

package supply
import (
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"time"
)
func TestSnapshotLimitPreservesSummaryAndRanking(t *testing.T) {
keys := make([]string, 700)
for i := range keys {
keys[i] = fmt.Sprintf("%04d-%s", i, strings.Repeat("x", 200))
}
start := time.Unix(100, 0)
o := New("attempt", start, keys)
for i, k := range keys {
o.Event(k, "global_capacity", 1, false, false, start)
o.Apply(k, o.Views()[k], Sample{Known: true, Eligible: true}, start.Add(time.Second))
o.Apply(k, o.Views()[k], Sample{Known: true, Eligible: true}, start.Add(time.Duration(1000+i)*time.Millisecond+time.Second))
}
body, err := o.Encode()
if err != nil {
t.Fatal(err)
}
var s Snapshot
if err = json.Unmarshal(body, &s); err != nil {
t.Fatal(err)
}
if len(body) > 256*1024 || !s.Truncated || len(s.Channels) != 100 || s.Summary.Targets != 700 || s.Channels[0].Key != keys[699] {
t.Fatalf("invalid bounded snapshot bytes=%d %+v", len(body), s.Summary)
}
restored := New("new", start.Add(time.Hour), keys)
restored.Restore(s)
r := restored.Snapshot()
if !r.Partial || !r.Truncated || r.Summary.TotalMS != s.Summary.TotalMS {
t.Fatalf("lost historical totals %+v %+v", s.Summary, r.Summary)
}
}
func TestConfirmedIntervalsAndStages(t *testing.T) {
start := time.Unix(100, 0)
o := New("attempt", start, []string{"a", "empty"})
o.Event("a", "global_capacity", 2, false, false, start)
tick := func(n int) {
v := o.Views()["a"]
o.Apply("a", v, Sample{Known: true, Eligible: true}, start.Add(time.Duration(n)*time.Second))
}
tick(1)
tick(2)
o.Event("a", "redis_write", 2, false, false, start.Add(2*time.Second))
tick(3)
s := o.Snapshot()
c := s.Channels[0]
if c.Startup.Count != 1 || c.Startup.TotalMS != 2000 || c.Stages["global_capacity"] != 1000 || c.Stages["redis_write"] != 1000 {
t.Fatalf("unexpected %+v", c)
}
o.Processing("a", 1)
tick(4)
tick(5)
if o.Snapshot().Channels[0].Startup.TotalMS != 2000 {
t.Fatal("processing counted as idle")
}
}
func TestUncertainAndGap(t *testing.T) {
start := time.Unix(100, 0)
o := New("a", start, []string{"a"})
v := o.Views()["a"]
o.Event("a", "sql_query", 0, false, false, start)
o.Apply("a", v, Sample{Known: true, Eligible: true}, start.Add(time.Second))
o.Apply("a", o.Views()["a"], Sample{Known: false}, start.Add(2*time.Second))
o.Apply("a", o.Views()["a"], Sample{Known: true, Eligible: true}, start.Add(6*time.Second))
s := o.Snapshot()
if !s.Partial || s.Channels[0].UnknownMS != 1000 || s.Channels[0].MissingSamples != 5 || s.Summary.Confirmed != 0 {
t.Fatalf("unexpected %+v", s)
}
}
func TestSupplyExclusions(t *testing.T) {
for _, tc := range []struct {
name string
sample Sample
processing bool
}{{"stock", Sample{Known: true, Eligible: true, Ready: 1}, false}, {"lease", Sample{Known: true, Eligible: true, Leases: 1}, false}, {"ended_round_or_blocked", Sample{Known: true, Eligible: false}, false}, {"consumer", Sample{Known: true, Eligible: true}, true}, {"unknown", Sample{Eligible: true}, false}} {
t.Run(tc.name, func(t *testing.T) {
start := time.Unix(100, 0)
o := New("a", start, []string{"a"})
o.Event("a", "global_capacity", 100, false, false, start)
if tc.processing {
o.Processing("a", 1)
}
for i := 1; i <= 3; i++ {
o.Apply("a", o.Views()["a"], tc.sample, start.Add(time.Duration(i)*time.Second))
}
if o.Snapshot().Summary.Confirmed != 0 {
t.Fatal("excluded sample counted as supply idle")
}
})
}
}
func TestConcurrentEvents(t *testing.T) {
start := time.Unix(100, 0)
o := New("a", start, []string{"a"})
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for n := 0; n < 100; n++ {
o.Event("a", "sql_query", 0, false, false, start)
o.Processing("a", 1)
o.Empty("a")
o.Apply("a", o.Views()["a"], Sample{Known: true, Eligible: true}, start)
o.Snapshot()
o.Processing("a", -1)
}
}()
}
wg.Wait()
}
func TestEmptyNeverConfirms(t *testing.T) {
start := time.Unix(100, 0)
o := New("a", start, []string{"a"})
for i := 1; i <= 3; i++ {
o.Apply("a", o.Views()["a"], Sample{Known: true, Eligible: true}, start.Add(time.Duration(i)*time.Second))
}
o.Event("a", "empty", 0, false, true, start.Add(3*time.Second))
s := o.Snapshot()
if s.Summary.Confirmed != 0 || s.Channels[0].Unconfirmed.TotalMS != 2000 {
t.Fatalf("unexpected %+v", s)
}
}