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),属既有缺陷,本仓未引入亦未修复。
360 lines
12 KiB
Go
360 lines
12 KiB
Go
package trafficcutoff
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
|
batchdomain "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/batch"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/do"
|
|
)
|
|
|
|
const (
|
|
testTenantID = "000000"
|
|
testDeptID = int64(100)
|
|
)
|
|
|
|
func newTestRepository(t *testing.T) *Repository {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
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 (
|
|
source_channel TEXT NOT NULL DEFAULT 'legacy',
|
|
execution_mode INTEGER NOT NULL DEFAULT 0,
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
batch_no TEXT NOT NULL,
|
|
batch_type INTEGER NOT NULL,
|
|
batch_async_status INTEGER NOT NULL,
|
|
run_result INTEGER NOT NULL DEFAULT 0,
|
|
total_count INTEGER NOT NULL DEFAULT 0,
|
|
valid_count INTEGER NOT NULL DEFAULT 0,
|
|
success_count INTEGER NOT NULL DEFAULT 0,
|
|
fail_count INTEGER NOT NULL DEFAULT 0,
|
|
fail_reason TEXT,
|
|
start_time DATETIME,
|
|
end_time DATETIME,
|
|
dept_id INTEGER NOT NULL DEFAULT 0,
|
|
created_by INTEGER NOT NULL DEFAULT 0,
|
|
created_dept INTEGER NOT NULL DEFAULT 0,
|
|
tenant_id TEXT NOT NULL DEFAULT '',
|
|
batch_info TEXT
|
|
)`,
|
|
`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,
|
|
snapshot TEXT,
|
|
item_id TEXT,
|
|
row_no INTEGER NOT NULL DEFAULT 0,
|
|
updated_by INTEGER NOT NULL DEFAULT 0
|
|
)`,
|
|
`CREATE TABLE t_sim_card_info (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
iccid TEXT NOT NULL,
|
|
msisdn TEXT,
|
|
imsi TEXT,
|
|
boss_card_status INTEGER NOT NULL DEFAULT 0,
|
|
traffic_cutoff_status INTEGER NOT NULL DEFAULT 0,
|
|
traffic_cutoff_threshold REAL NOT NULL DEFAULT 0,
|
|
tenant_id TEXT NOT NULL DEFAULT '',
|
|
updated_by INTEGER NOT NULL DEFAULT 0
|
|
)`,
|
|
`CREATE TABLE sys_oss (
|
|
oss_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
tenant_id TEXT,
|
|
file_name TEXT,
|
|
original_name TEXT,
|
|
file_suffix TEXT,
|
|
path TEXT,
|
|
url TEXT,
|
|
created_dept INTEGER,
|
|
created_by INTEGER,
|
|
updated_by INTEGER,
|
|
service TEXT,
|
|
md5 TEXT,
|
|
file_size INTEGER
|
|
)`,
|
|
`CREATE TABLE t_resource_batch_download (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
batch_no TEXT,
|
|
task_name TEXT,
|
|
resource_type INTEGER,
|
|
export_scene INTEGER,
|
|
total_count INTEGER,
|
|
success_count INTEGER,
|
|
fail_count INTEGER,
|
|
download_status INTEGER,
|
|
oss_id INTEGER,
|
|
file_name TEXT,
|
|
file_ext TEXT,
|
|
file_url TEXT,
|
|
file_path TEXT,
|
|
file_size INTEGER,
|
|
file_md5 TEXT,
|
|
download_count INTEGER,
|
|
max_download_count INTEGER,
|
|
dept_id INTEGER,
|
|
start_time DATETIME,
|
|
finish_time DATETIME,
|
|
tenant_id TEXT,
|
|
created_dept INTEGER,
|
|
created_by INTEGER,
|
|
updated_by INTEGER
|
|
)`,
|
|
}
|
|
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, lockEnabled: false}
|
|
}
|
|
|
|
func seedProcessingBatch(t *testing.T, ctx context.Context, db gdb.DB, batchNo string, threshold float64) {
|
|
t.Helper()
|
|
info := gjson.New(map[string]any{
|
|
"operation": "dataCut",
|
|
"trafficCutoffStatus": 1,
|
|
"trafficCutoffThreshold": threshold,
|
|
"thresholdUnit": "MB",
|
|
"resultFile": map[string]any{"status": "pending"},
|
|
})
|
|
if _, err := db.Model(dao.BatchMain.Table()).Ctx(ctx).Data(do.BatchMain{
|
|
BatchNo: batchNo,
|
|
BatchType: consts.BatchTypeTrafficCutoff,
|
|
BatchAsyncStatus: consts.BatchAsyncStatusProcessing,
|
|
RunResult: batchdomain.RunResultRunning,
|
|
DeptId: testDeptID,
|
|
CreatedBy: 7,
|
|
CreatedDept: testDeptID,
|
|
TenantId: testTenantID,
|
|
BatchInfo: info,
|
|
}).Insert(); err != nil {
|
|
t.Fatalf("insert batch main: %v", err)
|
|
}
|
|
}
|
|
|
|
func seedRunningDetail(t *testing.T, ctx context.Context, db gdb.DB, batchNo string, rowNo int, iccid, msisdn, imsi string, authorized bool) {
|
|
t.Helper()
|
|
snapshot := gjson.New(map[string]any{
|
|
"ICCID": iccid,
|
|
"MSISDN": msisdn,
|
|
"IMSI": imsi,
|
|
"authorized": authorized,
|
|
})
|
|
if _, err := db.Model(dao.BatchDetail.Table()).Ctx(ctx).Data(do.BatchDetail{
|
|
BatchNo: batchNo,
|
|
BatchType: consts.BatchTypeTrafficCutoff,
|
|
DetailStatus: consts.BatchDetailStatusRunning,
|
|
RowNo: rowNo,
|
|
ItemId: iccid,
|
|
Snapshot: snapshot,
|
|
}).Insert(); err != nil {
|
|
t.Fatalf("insert batch detail: %v", err)
|
|
}
|
|
}
|
|
|
|
func seedCard(t *testing.T, ctx context.Context, db gdb.DB, iccid, msisdn, imsi string, status int) {
|
|
t.Helper()
|
|
if _, err := db.Model(dao.TSimCardInfo.Table()).Ctx(ctx).Data(do.TSimCardInfo{
|
|
Iccid: iccid,
|
|
Msisdn: msisdn,
|
|
Imsi: imsi,
|
|
BossCardStatus: status,
|
|
TrafficCutoffStatus: 0,
|
|
TrafficCutoffThreshold: 0,
|
|
TenantId: testTenantID,
|
|
}).Insert(); err != nil {
|
|
t.Fatalf("insert sim card: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReadTrafficCutoffStatusRequiresNewField(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
info *gjson.Json
|
|
want int
|
|
wantErr bool
|
|
}{
|
|
{name: "new field", info: gjson.New(map[string]any{"trafficCutoffStatus": 2}), want: 2},
|
|
{name: "old field only", info: gjson.New(map[string]any{"trafficCutoffEnabled": 1}), wantErr: true},
|
|
{name: "missing batch info", wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := readTrafficCutoffStatus(tt.info)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("readTrafficCutoffStatus() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("readTrafficCutoffStatus() = %d, want %d", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCommitBusinessHappyPathUpdatesAllCards(t *testing.T) {
|
|
ctx := context.Background()
|
|
repo := newTestRepository(t)
|
|
batchNo := "DCT-1"
|
|
seedProcessingBatch(t, ctx, repo.db, batchNo, 2048.125)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 1, "898600000000001", "13800000001", "460001000000001", true)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 2, "898600000000002", "13800000002", "460001000000002", true)
|
|
seedCard(t, ctx, repo.db, "898600000000001", "13800000001", "460001000000001", 4)
|
|
seedCard(t, ctx, repo.db, "898600000000002", "13800000002", "460001000000002", 4)
|
|
|
|
task, err := repo.Load(ctx, batchNo)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if err = repo.CommitBusiness(ctx, task); err != nil {
|
|
t.Fatalf("CommitBusiness() error = %v", err)
|
|
}
|
|
|
|
assertBatchMainStatus(t, ctx, repo.db, batchNo, consts.BatchAsyncStatusSuccess, consts.BatchRunResultSuccess)
|
|
assertDetailStatusCount(t, ctx, repo.db, batchNo, consts.BatchDetailStatusSuccess, 2)
|
|
assertTrafficCutoff(t, ctx, repo.db, "898600000000001", 1, 2048.125)
|
|
assertTrafficCutoff(t, ctx, repo.db, "898600000000002", 1, 2048.125)
|
|
}
|
|
|
|
func TestCommitBusinessFailsWholeBatchOnMixedStatusWithoutCardUpdate(t *testing.T) {
|
|
ctx := context.Background()
|
|
repo := newTestRepository(t)
|
|
batchNo := "DCT-2"
|
|
seedProcessingBatch(t, ctx, repo.db, batchNo, 1024)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 1, "898600000000001", "13800000001", "460001000000001", true)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 2, "898600000000002", "13800000002", "460001000000002", true)
|
|
seedCard(t, ctx, repo.db, "898600000000001", "13800000001", "460001000000001", 4)
|
|
seedCard(t, ctx, repo.db, "898600000000002", "13800000002", "460001000000002", 5)
|
|
|
|
task, err := repo.Load(ctx, batchNo)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if err = repo.CommitBusiness(ctx, task); err != nil {
|
|
t.Fatalf("CommitBusiness() error = %v", err)
|
|
}
|
|
|
|
assertBatchMainStatus(t, ctx, repo.db, batchNo, consts.BatchAsyncStatusFailed, consts.BatchRunResultFailed)
|
|
assertDetailStatusCount(t, ctx, repo.db, batchNo, consts.BatchDetailStatusFailed, 2)
|
|
assertTrafficCutoff(t, ctx, repo.db, "898600000000001", 0, 0)
|
|
assertTrafficCutoff(t, ctx, repo.db, "898600000000002", 0, 0)
|
|
}
|
|
|
|
func TestCommitBusinessFailsWholeBatchOnCanceledCardWithoutCardUpdate(t *testing.T) {
|
|
ctx := context.Background()
|
|
repo := newTestRepository(t)
|
|
batchNo := "DCT-3"
|
|
seedProcessingBatch(t, ctx, repo.db, batchNo, 512)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 1, "898600000000001", "13800000001", "460001000000001", true)
|
|
seedCard(t, ctx, repo.db, "898600000000001", "13800000001", "460001000000001", 9)
|
|
|
|
task, err := repo.Load(ctx, batchNo)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if err = repo.CommitBusiness(ctx, task); err != nil {
|
|
t.Fatalf("CommitBusiness() error = %v", err)
|
|
}
|
|
|
|
assertBatchMainStatus(t, ctx, repo.db, batchNo, consts.BatchAsyncStatusFailed, consts.BatchRunResultFailed)
|
|
assertTrafficCutoff(t, ctx, repo.db, "898600000000001", 0, 0)
|
|
}
|
|
|
|
func TestUpdateResultFileFailureOnlyMarksBatchInfo(t *testing.T) {
|
|
ctx := context.Background()
|
|
repo := newTestRepository(t)
|
|
batchNo := "DCT-4"
|
|
seedProcessingBatch(t, ctx, repo.db, batchNo, 512)
|
|
seedRunningDetail(t, ctx, repo.db, batchNo, 1, "898600000000001", "13800000001", "460001000000001", true)
|
|
|
|
task, err := repo.Load(ctx, batchNo)
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if err = repo.updateResultFileStatus(ctx, task, "failed", 0, 0, "disk full"); err != nil {
|
|
t.Fatalf("updateResultFileStatus() error = %v", err)
|
|
}
|
|
|
|
var row struct {
|
|
Info string `orm:"batch_info"`
|
|
}
|
|
if err = repo.db.Model(dao.BatchMain.Table()).Ctx(ctx).
|
|
Where(dao.BatchMain.Columns().BatchNo, batchNo).
|
|
Fields(dao.BatchMain.Columns().BatchInfo).
|
|
Scan(&row); err != nil {
|
|
t.Fatalf("query batch main: %v", err)
|
|
}
|
|
info := gjson.New([]byte(row.Info))
|
|
if info.Get("resultFile.status").String() != "failed" {
|
|
t.Fatalf("resultFile.status = %s, want failed", info.Get("resultFile.status").String())
|
|
}
|
|
if info.Get("resultFile.error").String() != "disk full" {
|
|
t.Fatalf("resultFile.error = %s, want disk full", info.Get("resultFile.error").String())
|
|
}
|
|
assertBatchMainStatus(t, ctx, repo.db, batchNo, consts.BatchAsyncStatusProcessing, batchdomain.RunResultRunning)
|
|
}
|
|
|
|
func assertBatchMainStatus(t *testing.T, ctx context.Context, db gdb.DB, batchNo string, wantAsync, wantRun int) {
|
|
t.Helper()
|
|
var row struct {
|
|
Async int `orm:"batch_async_status"`
|
|
Run int `orm:"run_result"`
|
|
}
|
|
if err := db.Model(dao.BatchMain.Table()).Ctx(ctx).
|
|
Where(dao.BatchMain.Columns().BatchNo, batchNo).
|
|
Fields(dao.BatchMain.Columns().BatchAsyncStatus, dao.BatchMain.Columns().RunResult).
|
|
Scan(&row); err != nil {
|
|
t.Fatalf("query batch main: %v", err)
|
|
}
|
|
if row.Async != wantAsync || row.Run != wantRun {
|
|
t.Fatalf("batch status = (async=%d, run=%d), want (async=%d, run=%d)", row.Async, row.Run, wantAsync, wantRun)
|
|
}
|
|
}
|
|
|
|
func assertDetailStatusCount(t *testing.T, ctx context.Context, db gdb.DB, batchNo string, status, want int) {
|
|
t.Helper()
|
|
count, err := db.Model(dao.BatchDetail.Table()).Ctx(ctx).
|
|
Where(dao.BatchDetail.Columns().BatchNo, batchNo).
|
|
Where(dao.BatchDetail.Columns().DetailStatus, status).
|
|
Count()
|
|
if err != nil {
|
|
t.Fatalf("count details: %v", err)
|
|
}
|
|
if count != want {
|
|
t.Fatalf("detail status %d count = %d, want %d", status, count, want)
|
|
}
|
|
}
|
|
|
|
func assertTrafficCutoff(t *testing.T, ctx context.Context, db gdb.DB, iccid string, enabled int, threshold float64) {
|
|
t.Helper()
|
|
var row struct {
|
|
Enabled int `orm:"traffic_cutoff_status"`
|
|
Threshold float64 `orm:"traffic_cutoff_threshold"`
|
|
}
|
|
if err := db.Model(dao.TSimCardInfo.Table()).Ctx(ctx).
|
|
Where(dao.TSimCardInfo.Columns().Iccid, iccid).
|
|
Fields(dao.TSimCardInfo.Columns().TrafficCutoffStatus, dao.TSimCardInfo.Columns().TrafficCutoffThreshold).
|
|
Scan(&row); err != nil {
|
|
t.Fatalf("query traffic cutoff fields: %v", err)
|
|
}
|
|
if row.Enabled != enabled || row.Threshold != threshold {
|
|
t.Fatalf("%s cutoff = (%d, %v), want (%d, %v)", iccid, row.Enabled, row.Threshold, enabled, threshold)
|
|
}
|
|
}
|