204 lines
6.5 KiB
Go
204 lines
6.5 KiB
Go
// aggregator_test.go Day 47:合包聚合器单测——双阈值触发(1000 条即时/窗口到期)、
|
|
// 调用次数削减(验收口径 ≥80%)、批量失败逐号回退、分发器聚合接线。
|
|
package dispatch
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"go-sms/internal/service/send/submit"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/service/channel/pool"
|
|
"go-sms/internal/util"
|
|
)
|
|
|
|
// fakeBatchLink 批量链路桩:统计批量/逐号调用次数。
|
|
type fakeBatchLink struct {
|
|
batchCalls atomic.Int64
|
|
singleCalls atomic.Int64
|
|
failBatch atomic.Bool
|
|
mu sync.Mutex
|
|
lastMobiles []string
|
|
}
|
|
|
|
func (f *fakeBatchLink) SubmitViaLink(ctx context.Context, channelID int64, mobile, content string) ([]string, error) {
|
|
f.singleCalls.Add(1)
|
|
return []string{"1"}, nil
|
|
}
|
|
|
|
func (f *fakeBatchLink) SubmitBatchViaLink(ctx context.Context, channelID int64, mobiles []string, content string) ([]pool.BatchAck, error) {
|
|
f.batchCalls.Add(1)
|
|
f.mu.Lock()
|
|
f.lastMobiles = append([]string{}, mobiles...)
|
|
f.mu.Unlock()
|
|
if f.failBatch.Load() {
|
|
return nil, errors.New("batch failed")
|
|
}
|
|
// 单片合包回执:每号一段,msg_id=号序(可配对)。
|
|
acks := make([]pool.BatchAck, 0, 1)
|
|
acks = append(acks, pool.BatchAck{
|
|
MsgID: fmt.Sprintf("b%d", f.batchCalls.Load()), Mobiles: mobiles,
|
|
PkNumber: 1, PkTotal: 1,
|
|
})
|
|
return acks, nil
|
|
}
|
|
|
|
// TestAggregator_ThresholdFlush 满 maxBatch 立即冲洗(阈值触发实时性)。
|
|
func TestAggregator_ThresholdFlush(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(lk, time.Hour, 100) // 窗口拉长:只验证阈值路径
|
|
for i := 0; i < 99; i++ {
|
|
agg.Add(7, "hi", fmt.Sprintf("1380013%05d", i), int64(i))
|
|
}
|
|
if _, b, _ := agg.Stats(); b != 0 {
|
|
t.Fatalf("99 条不应触发: batch=%d", b)
|
|
}
|
|
agg.Add(7, "hi", "13800138099", 99) // 第 100 条:满阈值即时 flush
|
|
if _, b, s := agg.Stats(); b != 1 || s != 0 {
|
|
t.Fatalf("阈值未即时触发: batch=%d single=%d", b, s)
|
|
}
|
|
if n := lk.batchCalls.Load(); n != 1 {
|
|
t.Fatalf("批量调用=%d", n)
|
|
}
|
|
if len(lk.lastMobiles) != 100 {
|
|
t.Fatalf("批量号码数=%d", len(lk.lastMobiles))
|
|
}
|
|
}
|
|
|
|
// TestAggregator_CallReduction 验收口径:1000 条聚合后批量调用 1 次
|
|
// (逐号 1000 次 → 1 次,调用次数降 99.9% ≥ 80%)。
|
|
func TestAggregator_CallReduction(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(lk, time.Hour, 1000)
|
|
for i := 0; i < 1000; i++ {
|
|
agg.Add(7, "【签名】内容", fmt.Sprintf("1380013%05d", i), int64(i))
|
|
}
|
|
added, batch, single := agg.Stats()
|
|
if added != 1000 || batch != 1 || single != 0 {
|
|
t.Fatalf("added=%d batch=%d single=%d", added, batch, single)
|
|
}
|
|
reduction := 1 - float64(lk.batchCalls.Load())/float64(1000)
|
|
if reduction < 0.8 {
|
|
t.Fatalf("调用次数削减 %.1f%% 低于 80%%", reduction*100)
|
|
}
|
|
}
|
|
|
|
// TestAggregator_WindowFlush 窗口到期统一冲洗(未满阈值)。
|
|
func TestAggregator_WindowFlush(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(lk, 40*time.Millisecond, 1000)
|
|
agg.Add(7, "hi", "13800138000", 1)
|
|
agg.Add(8, "hi", "13800138001", 2) // 不同通道:独立分组
|
|
if _, b, _ := agg.Stats(); b != 0 {
|
|
t.Fatalf("不应提前触发: batch=%d", b)
|
|
}
|
|
time.Sleep(90 * time.Millisecond)
|
|
agg.FlushAll()
|
|
if _, b, s := agg.Stats(); b != 2 || s != 0 {
|
|
t.Fatalf("窗口冲洗错误: batch=%d single=%d", b, s)
|
|
}
|
|
}
|
|
|
|
// TestAggregator_RunTicker 常驻窗口自动冲洗(Run 路径)。
|
|
func TestAggregator_RunTicker(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(lk, 30*time.Millisecond, 1000)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go agg.Run(ctx)
|
|
agg.Add(9, "x", "13800138000", 1)
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if _, b, _ := agg.Stats(); b >= 1 {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatal("Run 窗口未触发冲洗")
|
|
}
|
|
|
|
// TestAggregator_BatchFailFallback 批量失败→逐号回退保送达。
|
|
func TestAggregator_BatchFailFallback(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
lk.failBatch.Store(true)
|
|
agg := NewSubmitAggregator(lk, time.Hour, 10)
|
|
for i := 0; i < 10; i++ {
|
|
agg.Add(7, "hi", fmt.Sprintf("1380013%05d", i), int64(i))
|
|
}
|
|
if lk.batchCalls.Load() != 1 {
|
|
t.Fatalf("批量调用=%d", lk.batchCalls.Load())
|
|
}
|
|
if n := lk.singleCalls.Load(); n != 10 {
|
|
t.Fatalf("回退逐号=%d", n)
|
|
}
|
|
}
|
|
|
|
// TestAggregator_NoBatchLinkFallback 链路无批量能力:逐号兼容路径。
|
|
func TestAggregator_NoBatchLinkFallback(t *testing.T) {
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(singleOnly{lk}, time.Hour, 5)
|
|
for i := 0; i < 5; i++ {
|
|
agg.Add(7, "hi", fmt.Sprintf("1380013%05d", i), int64(i))
|
|
}
|
|
if n := lk.singleCalls.Load(); n != 5 {
|
|
t.Fatalf("逐号调用=%d", n)
|
|
}
|
|
}
|
|
|
|
// singleOnly 只暴露单号接口(包装去批量)。
|
|
type singleOnly struct{ inner *fakeBatchLink }
|
|
|
|
func (s singleOnly) SubmitViaLink(ctx context.Context, channelID int64, mobile, content string) ([]string, error) {
|
|
return s.inner.SubmitViaLink(ctx, channelID, mobile, content)
|
|
}
|
|
|
|
// TestDispatcher_WithAggregator 分发器聚合接线:任务分发→聚合→批量冲洗→明细终态落库。
|
|
func TestDispatcher_WithAggregator(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Migrator().CreateTable(&entity.MessageTask{}, &entity.MessageDetail{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sf, _ := util.NewSnowflake(1)
|
|
task := entity.MessageTask{TaskNo: "T47", AccountID: 1, ChannelID: 7, SendType: 1}
|
|
if err := db.Create(&task).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
details := make([]entity.MessageDetail, 0, 20)
|
|
for i := 0; i < 20; i++ {
|
|
d := entity.MessageDetail{TaskID: task.ID, AccountID: 1,
|
|
Mobile: fmt.Sprintf("1380013%05d", i), Content: "hi", Status: 0}
|
|
d.ID = sf.NextID()
|
|
details = append(details, d)
|
|
}
|
|
if err := db.CreateInBatches(details, 100).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
lk := &fakeBatchLink{}
|
|
agg := NewSubmitAggregator(lk, time.Hour, 1000)
|
|
d := NewChannelDispatcher(db, nil, lk, agg) // rt=nil:task 已置 ChannelID,不走 Pick
|
|
sent, failed, derr := d.Dispatch(context.Background(), &task)
|
|
if derr != nil || failed != 0 || sent != 20 {
|
|
t.Fatalf("dispatch: sent=%d failed=%d err=%v", sent, failed, derr)
|
|
}
|
|
if n := lk.batchCalls.Load(); n != 1 {
|
|
t.Fatalf("批量调用=%d(任务级即时冲洗)", n)
|
|
}
|
|
var cnt int64
|
|
db.Model(&entity.MessageDetail{}).Where("channel_id = ? AND status = ?", 7, submit.DetailStSubmitted).Count(&cnt)
|
|
if cnt != 20 {
|
|
t.Fatalf("明细终态落库=%d/20", cnt)
|
|
}
|
|
}
|