204 lines
6.6 KiB
Go
204 lines
6.6 KiB
Go
// callback_test.go S4·C1:入队(URL 配置门控/载荷)+ 推送状态机
|
|
// (sent/退避/死信/重派)——httptest 模拟客户回调地址。
|
|
package callback
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"go-sms/internal/model/entity"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func newCallbackDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Discard})
|
|
if err != nil {
|
|
t.Fatalf("打开内存库失败: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&entity.CallbackTask{}, &entity.AccountConfig{}, &entity.MessageDetail{}); err != nil {
|
|
t.Fatalf("AutoMigrate 失败: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func mkDetailRow(t *testing.T, db *gorm.DB, accountID int64) int64 {
|
|
t.Helper()
|
|
d := entity.MessageDetail{TaskID: 1, AccountID: accountID, Mobile: "13800001111", Status: 2}
|
|
d.ID = time.Now().UnixNano()
|
|
if err := db.Create(&d).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return d.ID
|
|
}
|
|
|
|
// TestEnqueueReport_NoURL 未配置 report_url:零任务(多数账号零开销)。
|
|
func TestEnqueueReport_NoURL(t *testing.T) {
|
|
db := newCallbackDB(t)
|
|
s := NewService(db)
|
|
detailID := mkDetailRow(t, db, 5)
|
|
if err := s.EnqueueReport(context.Background(), detailID, 2, "SUCCESS", "", "13800001111", time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int64
|
|
db.Model(&entity.CallbackTask{}).Count(&n)
|
|
if n != 0 {
|
|
t.Fatalf("未配置 URL 应零任务, got %d", n)
|
|
}
|
|
}
|
|
|
|
// TestEnqueueReport_WithURL 配置 report_url:入队 type=report,载荷字段完整。
|
|
func TestEnqueueReport_WithURL(t *testing.T) {
|
|
db := newCallbackDB(t)
|
|
if err := db.Create(&entity.AccountConfig{
|
|
AccountID: 5, Business: "sms", ReportURL: "http://cb.example/hook",
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := NewService(db)
|
|
detailID := mkDetailRow(t, db, 5)
|
|
if err := s.EnqueueReport(context.Background(), detailID, 3, "FAILURE", "UNDELIV", "13800001111", time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var task entity.CallbackTask
|
|
if err := db.First(&task).Error; err != nil {
|
|
t.Fatalf("任务未入队: %v", err)
|
|
}
|
|
if task.Type != entity.CallbackTypeReport || task.URL != "http://cb.example/hook" || task.Status != entity.CallbackPending {
|
|
t.Fatalf("type/url/status 异常: %+v", task)
|
|
}
|
|
var p ReportPayload
|
|
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.DetailID != detailID || p.AccountID != 5 || p.Stat != "FAILURE" || p.Mobile != "13800001111" {
|
|
t.Fatalf("载荷字段异常: %+v", p)
|
|
}
|
|
}
|
|
|
|
// TestEnqueueUplink 配置 uplink_url 才入队(type=uplink)。
|
|
func TestEnqueueUplink(t *testing.T) {
|
|
db := newCallbackDB(t)
|
|
s := NewService(db)
|
|
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int64
|
|
db.Model(&entity.CallbackTask{}).Count(&n)
|
|
if n != 0 {
|
|
t.Fatalf("未配置 uplink_url 应零任务, got %d", n)
|
|
}
|
|
db.Create(&entity.AccountConfig{AccountID: 5, Business: "sms", UplinkURL: "http://cb.example/mo"})
|
|
if err := s.EnqueueUplink(context.Background(), 9, 5, "10690", "13811112222", "R", "2026-11-16T10:00:00+08:00"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var task entity.CallbackTask
|
|
if err := db.First(&task).Error; err != nil {
|
|
t.Fatalf("任务未入队: %v", err)
|
|
}
|
|
if task.Type != entity.CallbackTypeUplink {
|
|
t.Fatalf("type 期望 uplink(2), got %d", task.Type)
|
|
}
|
|
var p UplinkPayload
|
|
if err := json.Unmarshal([]byte(task.Payload), &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.UplinkID != 9 || p.SrcMobile != "13811112222" || p.Content != "R" {
|
|
t.Fatalf("上行载荷异常: %+v", p)
|
|
}
|
|
}
|
|
|
|
// TestWorkerSentToDeadLifecycle 状态机全链:500→退避重排→耗尽死信→Resend→200 送达。
|
|
func TestWorkerSentToDeadLifecycle(t *testing.T) {
|
|
db := newCallbackDB(t)
|
|
code := 500
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(code)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
task := entity.CallbackTask{
|
|
AccountID: 5, Type: entity.CallbackTypeReport, URL: srv.URL,
|
|
Payload: `{"type":"report"}`, Status: entity.CallbackPending,
|
|
NextRetryAt: time.Now(),
|
|
}
|
|
if err := db.Create(&task).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := NewPushWorker(db)
|
|
w.maxAttempt = 2 // 测试压缩(生产 10)
|
|
|
|
// 第一轮:500 → attempt=1 重排(退避后到期)。
|
|
if n := w.Tick(context.Background()); n != 1 {
|
|
t.Fatalf("第一轮应处理 1 条, got %d", n)
|
|
}
|
|
var got entity.CallbackTask
|
|
db.First(&got, task.ID)
|
|
if got.Status != entity.CallbackPending || got.Attempt != 1 || !got.NextRetryAt.After(time.Now()) {
|
|
t.Fatalf("失败后应 pending+attempt=1+未来重试: %+v", got)
|
|
}
|
|
// 未到期不重扫。
|
|
if n := w.Tick(context.Background()); n != 0 {
|
|
t.Fatalf("退避窗口内不应重扫, got %d", n)
|
|
}
|
|
// 到期重扫 → 耗尽 → 死信。
|
|
db.Model(&entity.CallbackTask{}).Where("id = ?", task.ID).
|
|
Update("next_retry_at", time.Now().Add(-time.Second))
|
|
w.Tick(context.Background())
|
|
db.First(&got, task.ID)
|
|
if got.Status != entity.CallbackDead || got.Attempt != 2 {
|
|
t.Fatalf("重试耗尽应死信: %+v", got)
|
|
}
|
|
// 死信不参与扫描。
|
|
if n := w.Tick(context.Background()); n != 0 {
|
|
t.Fatalf("死信不应被扫描, got %d", n)
|
|
}
|
|
// 重派 → 服务恢复 → 送达。
|
|
if n, err := NewService(db).Resend(context.Background(), task.ID); err != nil || n != 1 {
|
|
t.Fatalf("Resend: n=%d err=%v", n, err)
|
|
}
|
|
code = http.StatusOK
|
|
w.Tick(context.Background())
|
|
db.First(&got, task.ID)
|
|
if got.Status != entity.CallbackSent || got.SentAt == nil || got.LastCode != 200 {
|
|
t.Fatalf("重派后应送达: %+v", got)
|
|
}
|
|
// 非死信重派=幂等拒绝(0 行)。
|
|
if n, _ := NewService(db).Resend(context.Background(), task.ID); n != 0 {
|
|
t.Fatalf("非死信 Resend 应 0 行, got %d", n)
|
|
}
|
|
}
|
|
|
|
// TestWorkerConcurrentBatch 并发批次全部推进(信号量不丢任务)。
|
|
func TestWorkerConcurrentBatch(t *testing.T) {
|
|
db := newCallbackDB(t)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(200)
|
|
}))
|
|
defer srv.Close()
|
|
for i := 0; i < 20; i++ {
|
|
db.Create(&entity.CallbackTask{
|
|
AccountID: 5, Type: entity.CallbackTypeUplink, URL: srv.URL,
|
|
Payload: "{}", Status: entity.CallbackPending, NextRetryAt: time.Now(),
|
|
})
|
|
}
|
|
w := NewPushWorker(db)
|
|
w.batch = 20
|
|
w.concurrency = 1 // sqlite 内存库并发写锁:单测串行(批次不丢任务语义不变)
|
|
if n := w.Tick(context.Background()); n != 20 {
|
|
t.Fatalf("应处理 20 条, got %d", n)
|
|
}
|
|
var sent int64
|
|
db.Model(&entity.CallbackTask{}).Where("status = ?", entity.CallbackSent).Count(&sent)
|
|
if sent != 20 {
|
|
t.Fatalf("应全部送达, got %d", sent)
|
|
}
|
|
}
|