262 lines
11 KiB
Go
262 lines
11 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/library/batchfields"
|
|
"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 TestMarkFailedKeepsMainReasonWithinTextColumn(t *testing.T) {
|
|
ctx := context.Background()
|
|
repository := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repository.db, "B-LONG", batchdomain.Type103)
|
|
if claimed, err := repository.MarkProcessing(ctx, "B-LONG", batchdomain.Type103); err != nil || !claimed {
|
|
t.Fatalf("claim batch = (%t, %v)", claimed, err)
|
|
}
|
|
reason := "database error: INSERT INTO batch_detail VALUES (" + strings.Repeat("错", 30000) + ")"
|
|
if marked, err := repository.MarkFailed(ctx, "B-LONG", batchdomain.Type103, reason); err != nil || !marked {
|
|
t.Fatalf("mark failed = (%t, %v)", marked, err)
|
|
}
|
|
var main entity.BatchMain
|
|
if err := repository.db.Model(dao.BatchMain.Table()).Ctx(ctx).Where(dao.BatchMain.Columns().BatchNo, "B-LONG").Scan(&main); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(main.FailReason) > batchfields.FailureReasonByteLimit || strings.Contains(main.FailReason, "VALUES") {
|
|
t.Fatalf("unsafe failure reason: %q", main.FailReason)
|
|
}
|
|
}
|
|
|
|
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 TestMarkProcessingAPNMarkerIsAtomic(t *testing.T) {
|
|
for _, batchType := range []int{107, 109, 110} {
|
|
t.Run(string(rune('A'+batchType-107)), func(t *testing.T) {
|
|
ctx := context.Background()
|
|
repo := newTestRepository(t, ctx)
|
|
insertBatch(t, ctx, repo.db, "APN", batchType)
|
|
claimed, err := repo.MarkProcessing(ctx, "APN", batchType)
|
|
if err == nil || claimed {
|
|
t.Fatalf("缺少迁移必须安全失败: claimed=%t err=%v", claimed, err)
|
|
}
|
|
assertStatus(t, ctx, repo.db, "batch_main", "batch_async_status", batchdomain.AsyncStatusPending)
|
|
main, err := repo.FindMain(ctx, "APN")
|
|
if err != nil || main.StartTime != nil {
|
|
t.Fatalf("失败未回滚首发时间: %+v %v", main, err)
|
|
}
|
|
pending, err := repo.db.Model("batch_detail").Ctx(ctx).Where("detail_status", batchdomain.DetailStatusPending).Count()
|
|
if err != nil || pending != 1 {
|
|
t.Fatalf("明细没有安全回滚: %d %v", pending, err)
|
|
}
|
|
if _, err := repo.db.Exec(ctx, "CREATE TABLE data_operation_parent(batch_no TEXT PRIMARY KEY,ready INTEGER DEFAULT 0)"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if claimed, err := repo.MarkProcessing(ctx, "APN", batchType); err != nil || !claimed {
|
|
t.Fatalf("新批领取: %t %v", claimed, err)
|
|
}
|
|
n, err := repo.db.Model("data_operation_parent").Ctx(ctx).Where("batch_no", "APN").Where("ready", 0).Count()
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("新批缺少原子凭据: %d %v", n, err)
|
|
}
|
|
if claimed, err := repo.MarkProcessing(ctx, "APN", batchType); err != nil || claimed {
|
|
t.Fatalf("重投不应重复创建凭据: %t %v", claimed, 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)
|
|
}
|
|
}
|