2bec257289
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),属既有缺陷,本仓未引入亦未修复。
205 lines
8.5 KiB
Go
205 lines
8.5 KiB
Go
package batch
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
|
|
batchdomain "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/batch"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/do"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
|
)
|
|
|
|
func TestMarkProcessingClaimsMainAndPendingDetails(t *testing.T) {
|
|
ctx := context.Background()
|
|
repository := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repository.db, "B-103", batchdomain.Type103)
|
|
main, err := repository.FindMain(ctx, "B-103")
|
|
if err != nil || main == nil || main.BatchType != batchdomain.Type103 {
|
|
t.Fatalf("FindMain() = (%+v, %v), want batch type %d", main, err, batchdomain.Type103)
|
|
}
|
|
missing, err := repository.FindMain(ctx, "missing")
|
|
if err != nil || missing != nil {
|
|
t.Fatalf("FindMain(missing) = (%+v, %v), want nil, nil", missing, err)
|
|
}
|
|
|
|
claimed, err := repository.MarkProcessing(ctx, "B-103", batchdomain.Type103)
|
|
if err != nil {
|
|
t.Fatalf("MarkProcessing() error = %v", err)
|
|
}
|
|
if !claimed {
|
|
t.Fatal("MarkProcessing() claimed = false, want true")
|
|
}
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().BatchAsyncStatus, batchdomain.AsyncStatusProcessing)
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().RunResult, batchdomain.RunResultRunning)
|
|
main, err = repository.FindMain(ctx, "B-103")
|
|
if err != nil || main.StartTime == nil || main.StartTime.IsZero() {
|
|
t.Fatalf("processing batch start time = %v, error = %v; want current time", main.StartTime, err)
|
|
}
|
|
|
|
var details []entity.BatchDetail
|
|
if err = repository.db.Model(dao.BatchDetail.Table()).Ctx(ctx).
|
|
Order(dao.BatchDetail.Columns().Id).
|
|
Scan(&details); err != nil {
|
|
t.Fatalf("query detail statuses: %v", err)
|
|
}
|
|
if len(details) != 2 || details[0].DetailStatus != batchdomain.DetailStatusRunning || details[1].DetailStatus != 2 {
|
|
t.Fatalf("details = %+v, want statuses [1 2]", details)
|
|
}
|
|
|
|
claimed, err = repository.MarkProcessing(ctx, "B-103", batchdomain.Type103)
|
|
if err != nil {
|
|
t.Fatalf("second MarkProcessing() error = %v", err)
|
|
}
|
|
if claimed {
|
|
t.Fatal("second MarkProcessing() claimed = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestMarkProcessingRollsBackMainWhenDetailUpdateFails(t *testing.T) {
|
|
ctx := context.Background()
|
|
repository := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repository.db, "B-104", batchdomain.Type104)
|
|
if _, err := repository.db.Exec(ctx, "DROP TABLE batch_detail"); err != nil {
|
|
t.Fatalf("drop detail table: %v", err)
|
|
}
|
|
|
|
if claimed, err := repository.MarkProcessing(ctx, "B-104", batchdomain.Type104); err == nil || claimed {
|
|
t.Fatalf("MarkProcessing() = (%t, %v), want false and error", claimed, err)
|
|
}
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().BatchAsyncStatus, batchdomain.AsyncStatusPending)
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().RunResult, 0)
|
|
main, err := repository.FindMain(ctx, "B-104")
|
|
if err != nil || main.StartTime != nil {
|
|
t.Fatalf("rolled back batch start time = %v, error = %v; want nil", main.StartTime, err)
|
|
}
|
|
}
|
|
|
|
func TestMarkFailedFinalizesProcessingBatchAndDetails(t *testing.T) {
|
|
ctx := context.Background()
|
|
repository := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repository.db, "B-105", batchdomain.Type103)
|
|
if claimed, err := repository.MarkProcessing(ctx, "B-105", batchdomain.Type103); err != nil || !claimed {
|
|
t.Fatalf("MarkProcessing() = (%t, %v), want true, nil", claimed, err)
|
|
}
|
|
|
|
marked, err := repository.MarkFailed(ctx, "B-105", batchdomain.Type103, "source file is unavailable")
|
|
if err != nil || !marked {
|
|
t.Fatalf("MarkFailed() = (%t, %v), want true, nil", marked, err)
|
|
}
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().BatchAsyncStatus, batchdomain.AsyncStatusFailed)
|
|
assertStatus(t, ctx, repository.db, dao.BatchMain.Table(), dao.BatchMain.Columns().RunResult, batchdomain.RunResultFailed)
|
|
|
|
var main entity.BatchMain
|
|
if err = repository.db.Model(dao.BatchMain.Table()).Ctx(ctx).Where(dao.BatchMain.Columns().BatchNo, "B-105").Scan(&main); err != nil {
|
|
t.Fatalf("query failed batch main: %v", err)
|
|
}
|
|
if main.FailReason != "source file is unavailable" || main.EndTime == nil || main.EndTime.IsZero() {
|
|
t.Fatalf("failed batch main = %+v, want failure reason and end time", main)
|
|
}
|
|
|
|
var details []entity.BatchDetail
|
|
if err = repository.db.Model(dao.BatchDetail.Table()).Ctx(ctx).Order(dao.BatchDetail.Columns().Id).Scan(&details); err != nil {
|
|
t.Fatalf("query failed batch details: %v", err)
|
|
}
|
|
if len(details) != 2 || details[0].DetailStatus != batchdomain.DetailStatusFailed || details[0].ErrorReason != "source file is unavailable" || details[1].DetailStatus != 2 {
|
|
t.Fatalf("failed details = %+v, want statuses [3 2]", details)
|
|
}
|
|
|
|
marked, err = repository.MarkFailed(ctx, "B-105", batchdomain.Type103, "ignored")
|
|
if err != nil || marked {
|
|
t.Fatalf("second MarkFailed() = (%t, %v), want false, nil", marked, err)
|
|
}
|
|
}
|
|
|
|
func TestMarkFailedTruncatesDetailReason(t *testing.T) {
|
|
ctx := context.Background()
|
|
repository := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repository.db, "B-106", batchdomain.Type103)
|
|
if claimed, err := repository.MarkProcessing(ctx, "B-106", batchdomain.Type103); err != nil || !claimed {
|
|
t.Fatalf("MarkProcessing() = (%t, %v), want true, nil", claimed, err)
|
|
}
|
|
|
|
reason := strings.Repeat("x", batchDetailErrorReasonLimit+20)
|
|
marked, err := repository.MarkFailed(ctx, "B-106", batchdomain.Type103, reason)
|
|
if err != nil || !marked {
|
|
t.Fatalf("MarkFailed() = (%t, %v), want true, nil", marked, err)
|
|
}
|
|
|
|
var main entity.BatchMain
|
|
if err = repository.db.Model(dao.BatchMain.Table()).Ctx(ctx).Where(dao.BatchMain.Columns().BatchNo, "B-106").Scan(&main); err != nil {
|
|
t.Fatalf("query failed batch main: %v", err)
|
|
}
|
|
if main.FailReason != reason {
|
|
t.Fatalf("main failure reason length = %d, want full length %d", len([]rune(main.FailReason)), len([]rune(reason)))
|
|
}
|
|
|
|
var details []entity.BatchDetail
|
|
if err = repository.db.Model(dao.BatchDetail.Table()).Ctx(ctx).
|
|
Where(dao.BatchDetail.Columns().BatchNo, "B-106").
|
|
Order(dao.BatchDetail.Columns().Id).
|
|
Scan(&details); err != nil {
|
|
t.Fatalf("query failed batch details: %v", err)
|
|
}
|
|
if len(details) != 2 || len([]rune(details[0].ErrorReason)) != batchDetailErrorReasonLimit || details[1].ErrorReason != "" {
|
|
t.Fatalf("detail failure reasons = %+v, want first truncated to %d and second unchanged", details, batchDetailErrorReasonLimit)
|
|
}
|
|
}
|
|
|
|
func newTestRepository(t *testing.T, ctx context.Context) *Repository {
|
|
t.Helper()
|
|
db, err := gdb.New(gdb.ConfigNode{
|
|
Type: "sqlite",
|
|
Link: "sqlite::@file(:memory:)",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create sqlite database: %v", err)
|
|
}
|
|
statements := []string{
|
|
"CREATE TABLE batch_main (batch_no TEXT PRIMARY KEY, batch_type INTEGER NOT NULL, batch_async_status INTEGER NOT NULL, run_result INTEGER NOT NULL DEFAULT 0, fail_reason TEXT, start_time DATETIME, end_time DATETIME, source_channel TEXT NOT NULL DEFAULT 'legacy', execution_mode INTEGER NOT NULL DEFAULT 0)",
|
|
"CREATE TABLE batch_detail (id INTEGER PRIMARY KEY AUTOINCREMENT, batch_no TEXT NOT NULL, batch_type INTEGER NOT NULL, detail_status INTEGER NOT NULL, error_reason TEXT)",
|
|
}
|
|
for _, statement := range statements {
|
|
if _, err = db.Exec(ctx, statement); err != nil {
|
|
t.Fatalf("create test schema: %v", err)
|
|
}
|
|
}
|
|
t.Cleanup(func() { _ = db.Close(ctx) })
|
|
return &Repository{db: db}
|
|
}
|
|
|
|
func insertBatch(t *testing.T, ctx context.Context, db gdb.DB, batchNo string, batchType int) {
|
|
t.Helper()
|
|
if _, err := db.Model(dao.BatchMain.Table()).Ctx(ctx).Data(do.BatchMain{
|
|
BatchNo: batchNo,
|
|
BatchType: batchType,
|
|
BatchAsyncStatus: batchdomain.AsyncStatusPending,
|
|
}).Insert(); err != nil {
|
|
t.Fatalf("insert batch main: %v", err)
|
|
}
|
|
for _, status := range []int{batchdomain.DetailStatusPending, 2} {
|
|
if _, err := db.Model(dao.BatchDetail.Table()).Ctx(ctx).Data(do.BatchDetail{
|
|
BatchNo: batchNo,
|
|
BatchType: batchType,
|
|
DetailStatus: status,
|
|
}).Insert(); err != nil {
|
|
t.Fatalf("insert batch detail: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func assertStatus(t *testing.T, ctx context.Context, db gdb.DB, table, column string, expected int) {
|
|
t.Helper()
|
|
actual, err := db.Model(table).Ctx(ctx).Value(column)
|
|
if err != nil {
|
|
t.Fatalf("query %s status: %v", table, err)
|
|
}
|
|
if actual.Int() != expected {
|
|
t.Fatalf("%s status = %d, want %d", table, actual.Int(), expected)
|
|
}
|
|
}
|