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,125 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/fullsync/contract"
|
||||
)
|
||||
|
||||
type liveFunc func(context.Context, string) (contract.RunMeta, error)
|
||||
|
||||
func (f liveFunc) GetRunMeta(ctx context.Context, id string) (contract.RunMeta, error) {
|
||||
return f(ctx, id)
|
||||
}
|
||||
|
||||
type archiveFunc func(context.Context, string) (contract.RunArchive, error)
|
||||
|
||||
func (f archiveFunc) GetArchivedRun(ctx context.Context, id string) (contract.RunArchive, error) {
|
||||
return f(ctx, id)
|
||||
}
|
||||
|
||||
type roundsFunc func(context.Context, string) ([]contract.RoundStats, error)
|
||||
|
||||
func (f roundsFunc) GetRoundStats(ctx context.Context, id string) ([]contract.RoundStats, error) {
|
||||
return f(ctx, id)
|
||||
}
|
||||
|
||||
func TestRunLookupLiveFirstAndArchiveFallback(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
liveErr error
|
||||
roundsErr error
|
||||
archiveErr error
|
||||
wantStatus int
|
||||
wantSource contract.RunSource
|
||||
wantArchives int
|
||||
wantRounds int
|
||||
}{
|
||||
{name: "live", wantStatus: 200, wantSource: contract.SourceLive, wantRounds: 1},
|
||||
{name: "archive", liveErr: fmt.Errorf("expired: %w", contract.ErrNotFound), wantStatus: 200, wantSource: contract.SourceArchive, wantArchives: 1},
|
||||
{name: "unknown", liveErr: contract.ErrNotFound, archiveErr: contract.ErrNotFound, wantStatus: 404, wantArchives: 1},
|
||||
{name: "live failure never falls back", liveErr: fmt.Errorf("live offline"), wantStatus: 500},
|
||||
{name: "round error never falls back", roundsErr: fmt.Errorf("rounds offline"), wantStatus: 500, wantRounds: 1},
|
||||
{name: "missing rounds never fall back", roundsErr: contract.ErrNotFound, wantStatus: 404, wantRounds: 1},
|
||||
{name: "archive error", liveErr: contract.ErrNotFound, archiveErr: fmt.Errorf("archive offline"), wantStatus: 500, wantArchives: 1},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var liveCalls, archiveCalls, roundsCalls int
|
||||
reader, err := NewRunReader(
|
||||
liveFunc(func(_ context.Context, id string) (contract.RunMeta, error) {
|
||||
liveCalls++
|
||||
if id != "run-1" {
|
||||
t.Fatalf("live ID = %q", id)
|
||||
}
|
||||
return contract.RunMeta{RunID: id, CurrentRound: 2}, test.liveErr
|
||||
}),
|
||||
archiveFunc(func(_ context.Context, id string) (contract.RunArchive, error) {
|
||||
archiveCalls++
|
||||
if id != "run-1" {
|
||||
t.Fatalf("archive ID = %q", id)
|
||||
}
|
||||
return contract.RunArchive{Run: contract.RunMeta{RunID: id, CurrentRound: 3}, Rounds: []contract.RoundStats{{RunID: id, Round: 3}}}, test.archiveErr
|
||||
}),
|
||||
roundsFunc(func(_ context.Context, id string) ([]contract.RoundStats, error) {
|
||||
roundsCalls++
|
||||
if id != "run-1" {
|
||||
t.Fatalf("rounds ID = %q", id)
|
||||
}
|
||||
return []contract.RoundStats{{RunID: id, Round: 2}}, test.roundsErr
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deps := testDependencies()
|
||||
deps.Runs = reader
|
||||
response := request(testHandler(t, deps), http.MethodGet, runPath, "")
|
||||
if response.Code != test.wantStatus {
|
||||
t.Fatalf("response = %d %s", response.Code, response.Body)
|
||||
}
|
||||
if liveCalls != 1 || archiveCalls != test.wantArchives || roundsCalls != test.wantRounds {
|
||||
t.Fatalf("calls: live=%d archive=%d rounds=%d", liveCalls, archiveCalls, roundsCalls)
|
||||
}
|
||||
if response.Code == 200 {
|
||||
var view contract.RunView
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &view); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if view.Source != test.wantSource || view.Run.RunID != "run-1" || len(view.Rounds) != 1 || view.Rounds[0].Round != view.Run.CurrentRound {
|
||||
t.Fatalf("view = %+v", view)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReaderOptionalServicesAndValidation(t *testing.T) {
|
||||
if _, err := NewRunReader(nil, nil, nil); err == nil {
|
||||
t.Fatal("accepted nil live reader")
|
||||
}
|
||||
calls := 0
|
||||
reader, err := NewRunReader(liveFunc(func(_ context.Context, id string) (contract.RunMeta, error) {
|
||||
calls++
|
||||
if id == "missing" {
|
||||
return contract.RunMeta{}, contract.ErrNotFound
|
||||
}
|
||||
return contract.RunMeta{RunID: id}, nil
|
||||
}), nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := reader.GetRun(context.Background(), "bad/id"); err == nil || calls != 0 {
|
||||
t.Fatal("invalid ID reached live reader")
|
||||
}
|
||||
view, err := reader.GetRun(context.Background(), "run-1")
|
||||
if err != nil || view.Source != contract.SourceLive || view.Rounds != nil {
|
||||
t.Fatalf("view = %+v, err = %v", view, err)
|
||||
}
|
||||
if _, err := reader.GetRun(context.Background(), "missing"); err != contract.ErrNotFound {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user