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:
MiFaZhan
2026-10-22 16:37:56 +08:00
commit 2bec257289
361 changed files with 71524 additions and 0 deletions
+198
View File
@@ -0,0 +1,198 @@
package balance
import (
"context"
"errors"
"io"
"path/filepath"
"testing"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
type tradeQueryerStub struct {
result *TradeQueryResult
err error
calls int
}
func (s *tradeQueryerStub) Query(_ context.Context, _ string) (*TradeQueryResult, error) {
s.calls++
return s.result, s.err
}
func TestCheckRechargeTimeout(t *testing.T) {
g.Log().SetWriter(io.Discard)
ctx := context.Background()
if err := gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{{Type: "sqlite", Name: filepath.Join(t.TempDir(), "balance.db")}},
}); err != nil {
t.Fatalf("configure test database: %v", err)
}
defer g.DB().Close(ctx)
createBalanceTables(t, ctx)
t.Run("paid recharge is credited exactly once", func(t *testing.T) {
insertRechargeDetail(t, ctx, "BAL-PAID", 10)
insertAgentBalance(t, ctx, 11, "tenant-1", 5)
queryer := &tradeQueryerStub{result: &TradeQueryResult{TradeStatus: "TRADE_SUCCESS", TotalAmount: "10.00", TradeNo: "ALI-1"}}
service := NewWithQuery(queryer)
if err := service.CheckRechargeTimeout(ctx, "BAL-PAID"); err != nil {
t.Fatalf("first check failed: %v", err)
}
if status := rechargeStatus(t, ctx, "BAL-PAID"); status != rechargeStatusPaid {
t.Fatalf("expected paid status, got %d", status)
}
if balance := agentBalanceValue(t, ctx, 11, "tenant-1"); balance != 15 {
t.Fatalf("expected balance 15, got %v", balance)
}
if err := service.CheckRechargeTimeout(ctx, "BAL-PAID"); err != nil {
t.Fatalf("duplicate check failed: %v", err)
}
if queryer.calls != 1 {
t.Fatalf("expected one Alipay query, got %d", queryer.calls)
}
if balance := agentBalanceValue(t, ctx, 11, "tenant-1"); balance != 15 {
t.Fatalf("duplicate message changed balance to %v", balance)
}
})
t.Run("unpaid recharge is expired", func(t *testing.T) {
insertRechargeDetail(t, ctx, "BAL-EXPIRED", 10)
service := NewWithQuery(&tradeQueryerStub{result: &TradeQueryResult{TradeStatus: "WAIT_BUYER_PAY"}})
if err := service.CheckRechargeTimeout(ctx, "BAL-EXPIRED"); err != nil {
t.Fatalf("timeout check failed: %v", err)
}
if status := rechargeStatus(t, ctx, "BAL-EXPIRED"); status != rechargeStatusExpired {
t.Fatalf("expected expired status, got %d", status)
}
})
t.Run("missing recharge is ignored", func(t *testing.T) {
queryer := &tradeQueryerStub{err: errors.New("must not be called")}
if err := NewWithQuery(queryer).CheckRechargeTimeout(ctx, "BAL-MISSING"); err != nil {
t.Fatalf("missing recharge returned error: %v", err)
}
if queryer.calls != 0 {
t.Fatalf("missing recharge queried Alipay %d times", queryer.calls)
}
})
t.Run("amount mismatch returns an error without crediting", func(t *testing.T) {
insertRechargeDetail(t, ctx, "BAL-MISMATCH", 10)
insertAgentBalance(t, ctx, 11, "tenant-1", 15)
service := NewWithQuery(&tradeQueryerStub{result: &TradeQueryResult{TradeStatus: "TRADE_SUCCESS", TotalAmount: "9.00"}})
if err := service.CheckRechargeTimeout(ctx, "BAL-MISMATCH"); err == nil {
t.Fatal("expected amount mismatch error")
}
if status := rechargeStatus(t, ctx, "BAL-MISMATCH"); status != rechargeStatusPending {
t.Fatalf("mismatch changed status to %d", status)
}
if balance := agentBalanceValue(t, ctx, 11, "tenant-1"); balance != 15 {
t.Fatalf("mismatch changed balance to %v", balance)
}
})
t.Run("Alipay failure is returned for delayed retry", func(t *testing.T) {
insertRechargeDetail(t, ctx, "BAL-RETRY", 10)
service := NewWithQuery(&tradeQueryerStub{err: errors.New("Alipay unavailable")})
if err := service.CheckRechargeTimeout(ctx, "BAL-RETRY"); err == nil {
t.Fatal("expected query error")
}
if status := rechargeStatus(t, ctx, "BAL-RETRY"); status != rechargeStatusPending {
t.Fatalf("retryable failure changed status to %d", status)
}
})
}
func createBalanceTables(t *testing.T, ctx context.Context) {
t.Helper()
statements := []string{
`CREATE TABLE t_balance_detail (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_id INTEGER NOT NULL,
transaction_status INTEGER NOT NULL,
transaction_channel INTEGER NOT NULL,
amount REAL NOT NULL,
order_sn TEXT NOT NULL,
tenant_id TEXT NOT NULL,
created_dept INTEGER NOT NULL,
created_by INTEGER NOT NULL,
transaction_time DATETIME,
before_balance REAL,
balance_after REAL,
channel_serial_no TEXT,
remark TEXT,
updated_by INTEGER,
updated_at DATETIME
)`,
`CREATE TABLE t_agent_balance (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_id INTEGER NOT NULL,
balance REAL NOT NULL,
withdraw_amount REAL,
withdraw_amount_status INTEGER,
invoice_amount REAL,
can_invoice_amount REAL,
credit_limit REAL,
used_credit REAL,
pending_repayment REAL,
status INTEGER,
tenant_id TEXT NOT NULL,
created_dept INTEGER,
created_by INTEGER,
created_at DATETIME,
updated_by INTEGER,
updated_at DATETIME
)`,
}
for _, statement := range statements {
if _, err := g.DB().Exec(ctx, statement); err != nil {
t.Fatalf("create table: %v", err)
}
}
}
func insertRechargeDetail(t *testing.T, ctx context.Context, orderSN string, amount float64) {
t.Helper()
if _, err := g.DB().Model("t_balance_detail").Ctx(ctx).Data(g.Map{
"dept_id": 11,
"transaction_status": rechargeStatusPending,
"transaction_channel": rechargeChannelAlipay,
"amount": amount,
"order_sn": orderSN,
"tenant_id": "tenant-1",
"created_dept": 11,
"created_by": 7,
}).Insert(); err != nil {
t.Fatalf("insert recharge detail: %v", err)
}
}
func insertAgentBalance(t *testing.T, ctx context.Context, deptID int64, tenantID string, balance float64) {
t.Helper()
if _, err := g.DB().Model("t_agent_balance").Ctx(ctx).Data(g.Map{"dept_id": deptID, "tenant_id": tenantID, "balance": balance}).Insert(); err != nil {
t.Fatalf("insert agent balance: %v", err)
}
}
func rechargeStatus(t *testing.T, ctx context.Context, orderSN string) int {
t.Helper()
value, err := g.DB().Model("t_balance_detail").Ctx(ctx).Where("order_sn", orderSN).Value("transaction_status")
if err != nil {
t.Fatalf("get recharge status: %v", err)
}
return value.Int()
}
func agentBalanceValue(t *testing.T, ctx context.Context, deptID int64, tenantID string) float64 {
t.Helper()
value, err := g.DB().Model("t_agent_balance").Ctx(ctx).Where("dept_id", deptID).Where("tenant_id", tenantID).Value("balance")
if err != nil {
t.Fatalf("get agent balance: %v", err)
}
return value.Float64()
}