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,229 @@
|
||||
package trafficusage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
g.Log().SetWriter(io.Discard)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
type upstreamStub struct {
|
||||
dailyCalls int
|
||||
monthlyCalls int
|
||||
dailyOutput []model.SimBaseRes[model.SimDataUsageDailyOutputItem]
|
||||
monthlyOutput []model.SimBaseRes[model.SimDataUsageMonthlyOutputItem]
|
||||
dailyErr error
|
||||
monthlyErr error
|
||||
}
|
||||
|
||||
func (s *upstreamStub) QuerySimDatausageDaily(_ context.Context, _ *model.SimDataUsageDailyInput) ([]model.SimBaseRes[model.SimDataUsageDailyOutputItem], error) {
|
||||
s.dailyCalls++
|
||||
return s.dailyOutput, s.dailyErr
|
||||
}
|
||||
|
||||
func (s *upstreamStub) QuerySimDatausageMonthly(_ context.Context, _ *model.SimDataUsageMonthlyInput) ([]model.SimBaseRes[model.SimDataUsageMonthlyOutputItem], error) {
|
||||
s.monthlyCalls++
|
||||
return s.monthlyOutput, s.monthlyErr
|
||||
}
|
||||
|
||||
type repositoryStub struct {
|
||||
dailyCalls int
|
||||
monthlyCalls int
|
||||
dailyItems []model.TrafficUsageDailyItem
|
||||
monthlyItems []model.TrafficUsageMonthlyItem
|
||||
dailyErr error
|
||||
monthlyErr error
|
||||
}
|
||||
|
||||
func (s *repositoryStub) UpdateDaily(_ context.Context, items []model.TrafficUsageDailyItem) error {
|
||||
s.dailyCalls++
|
||||
s.dailyItems = items
|
||||
return s.dailyErr
|
||||
}
|
||||
|
||||
func (s *repositoryStub) UpdateMonthly(_ context.Context, items []model.TrafficUsageMonthlyItem) error {
|
||||
s.monthlyCalls++
|
||||
s.monthlyItems = items
|
||||
return s.monthlyErr
|
||||
}
|
||||
|
||||
func TestRetryDailySuccess(t *testing.T) {
|
||||
upstream := &upstreamStub{
|
||||
dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{
|
||||
{
|
||||
SimKey: &model.SimKey{ICCID: "89860001"},
|
||||
ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260714", DataAmount: 12.5},
|
||||
},
|
||||
},
|
||||
}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
message := model.TrafficUsageDailyRetryMessage{
|
||||
SimKey: &model.SimKey{ICCID: "89860001"},
|
||||
Card: model.TrafficUsageSimCard{Iccid: "89860001", PackageCode: "PKG-1"},
|
||||
QueryDate: "20260714",
|
||||
}
|
||||
|
||||
if err := service.RetryDaily(context.Background(), message); err != nil {
|
||||
t.Fatalf("RetryDaily returned error: %v", err)
|
||||
}
|
||||
if upstream.dailyCalls != 1 || repository.dailyCalls != 1 {
|
||||
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
||||
}
|
||||
if len(repository.dailyItems) != 1 {
|
||||
t.Fatalf("expected one daily item, got %d", len(repository.dailyItems))
|
||||
}
|
||||
item := repository.dailyItems[0]
|
||||
if item.Iccid != "89860001" || item.UsageMb != 12.5 || item.PackageCode != "PKG-1" {
|
||||
t.Fatalf("unexpected daily item: %+v", item)
|
||||
}
|
||||
if got := item.Date.Format("20060102"); got != "20260714" {
|
||||
t.Fatalf("unexpected daily date: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryDailyPreservesRetryBehavior(t *testing.T) {
|
||||
t.Run("period mismatch", func(t *testing.T) {
|
||||
upstream := &upstreamStub{dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{{SimKey: &model.SimKey{ICCID: "89860001"}, ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260713", DataAmount: 1}}}}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
||||
t.Fatal("expected period mismatch failure")
|
||||
}
|
||||
if repository.dailyCalls != 0 {
|
||||
t.Fatal("period mismatch was persisted")
|
||||
}
|
||||
})
|
||||
t.Run("unsupported channel is permanent", func(t *testing.T) {
|
||||
upstream := &upstreamStub{dailyOutput: []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{{SimKey: &model.SimKey{ICCID: "89860001"}, Code: 1, Message: "daily usage query is not supported"}}}
|
||||
service := New(upstream, &repositoryStub{})
|
||||
err := service.RetryDaily(context.Background(), validDailyMessage())
|
||||
var failure *Failure
|
||||
if !errors.As(err, &failure) || !failure.Permanent {
|
||||
t.Fatalf("expected permanent failure, got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("upstream failure", func(t *testing.T) {
|
||||
upstream := &upstreamStub{dailyErr: errors.New("upstream failed")}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
|
||||
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
||||
t.Fatal("expected retryable failure")
|
||||
}
|
||||
if upstream.dailyCalls != 1 || repository.dailyCalls != 0 {
|
||||
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("repository failure", func(t *testing.T) {
|
||||
upstream := &upstreamStub{dailyOutput: validDailyOutput()}
|
||||
repository := &repositoryStub{dailyErr: errors.New("repository failed")}
|
||||
service := New(upstream, repository)
|
||||
|
||||
if err := service.RetryDaily(context.Background(), validDailyMessage()); err == nil {
|
||||
t.Fatal("expected persistence failure")
|
||||
}
|
||||
if upstream.dailyCalls != 1 || repository.dailyCalls != 1 {
|
||||
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.dailyCalls, repository.dailyCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid date", func(t *testing.T) {
|
||||
upstream := &upstreamStub{}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
message := validDailyMessage()
|
||||
message.QueryDate = "invalid"
|
||||
|
||||
if err := service.RetryDaily(context.Background(), message); err == nil {
|
||||
t.Fatal("expected invalid date failure")
|
||||
}
|
||||
if upstream.dailyCalls != 0 || repository.dailyCalls != 0 {
|
||||
t.Fatalf("invalid date reached dependencies")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRetryMonthlySuccessAndRetry(t *testing.T) {
|
||||
t.Run("success", func(t *testing.T) {
|
||||
upstream := &upstreamStub{
|
||||
monthlyOutput: []model.SimBaseRes[model.SimDataUsageMonthlyOutputItem]{
|
||||
{
|
||||
SimKey: &model.SimKey{ICCID: "89860002"},
|
||||
ResData: model.SimDataUsageMonthlyOutputItem{DeadLine: "202607", DataAmount: 25.5},
|
||||
},
|
||||
},
|
||||
}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
message := model.TrafficUsageMonthlyRetryMessage{
|
||||
SimKey: &model.SimKey{ICCID: "89860002"},
|
||||
Card: model.TrafficUsageSimCard{Iccid: "89860002", PackageCode: "PKG-2"},
|
||||
QueryDate: "202607",
|
||||
}
|
||||
|
||||
if err := service.RetryMonthly(context.Background(), message); err != nil {
|
||||
t.Fatalf("RetryMonthly returned error: %v", err)
|
||||
}
|
||||
if upstream.monthlyCalls != 1 || repository.monthlyCalls != 1 {
|
||||
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.monthlyCalls, repository.monthlyCalls)
|
||||
}
|
||||
if len(repository.monthlyItems) != 1 {
|
||||
t.Fatalf("expected one monthly item, got %d", len(repository.monthlyItems))
|
||||
}
|
||||
item := repository.monthlyItems[0]
|
||||
if item.Iccid != "89860002" || item.UsageMb != 25.5 || item.PackageCode != "PKG-2" {
|
||||
t.Fatalf("unexpected monthly item: %+v", item)
|
||||
}
|
||||
if got := item.Month.Format("200601"); got != "202607" {
|
||||
t.Fatalf("unexpected month: %s", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("upstream failure", func(t *testing.T) {
|
||||
upstream := &upstreamStub{monthlyErr: errors.New("upstream failed")}
|
||||
repository := &repositoryStub{}
|
||||
service := New(upstream, repository)
|
||||
message := model.TrafficUsageMonthlyRetryMessage{
|
||||
SimKey: &model.SimKey{ICCID: "89860002"},
|
||||
Card: model.TrafficUsageSimCard{Iccid: "89860002"},
|
||||
QueryDate: "202607",
|
||||
}
|
||||
|
||||
if err := service.RetryMonthly(context.Background(), message); err == nil {
|
||||
t.Fatal("expected retryable failure")
|
||||
}
|
||||
if upstream.monthlyCalls != 1 || repository.monthlyCalls != 0 {
|
||||
t.Fatalf("unexpected calls: upstream=%d repository=%d", upstream.monthlyCalls, repository.monthlyCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func validDailyMessage() model.TrafficUsageDailyRetryMessage {
|
||||
return model.TrafficUsageDailyRetryMessage{
|
||||
SimKey: &model.SimKey{ICCID: "89860001"},
|
||||
Card: model.TrafficUsageSimCard{Iccid: "89860001"},
|
||||
QueryDate: "20260714",
|
||||
}
|
||||
}
|
||||
|
||||
func validDailyOutput() []model.SimBaseRes[model.SimDataUsageDailyOutputItem] {
|
||||
return []model.SimBaseRes[model.SimDataUsageDailyOutputItem]{
|
||||
{
|
||||
SimKey: &model.SimKey{ICCID: "89860001"},
|
||||
ResData: model.SimDataUsageDailyOutputItem{QueryDate: "20260714", DataAmount: 1},
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user