Files
go-sms-V2/internal/service/archive/archive_test.go
T
Tory 01c8874831
ci / test (push) Canceled after 0s
ci / web-test (push) Canceled after 0s
ci / build (push) Canceled after 0s
ci / deploy-dev (push) Canceled after 0s
S4-D3: 明细按天分区热5天+ClickHouse冷热切换——S4泳道收官(冷查16ms<1s验收✅): ①分区=迁移v22 PARTITION BY RANGE(id)(雪花时间位当天零点边界,主键即分区键零唯一键改造;热5天+未来3天缓冲+pmax兜底;MySQL守卫sqlite跳过)+archive/partition.go EnsurePartitions幂等滚动(REORGANIZE pmax拆分;过期分区归档校验后才DROP)+真机9分区存量无损;②归档=archive域(CH表toDate分区/ArchiveJob 1h:CH当日分区重写幂等→分批2000迁出→count校验相等才DROP MySQL分区或DELETE兜底=零丢失方向;consumer装配启动即首轮)+metrics archive_rows/errors;③冷查=GET /record/history(CH查询/task_no MySQL转译回填/D26契约直连)真机16ms+mobile筛选✓,导出挂账D29下载中心;④settle-check热窗感知=月中对账DetailPartial=true明细↔汇总退化两方基线(日汇总↔结算单,reconcile头注预声明落地)+settle.Service时钟注入nowFn+测试相对时间/period动态;⑤真机5连坑全修存档:MySQL8.0 information_schema非分区表也返回NULL行(判据加PARTITION_NAME IS NOT NULL)/parseTime下DATE() Scan []string带RFC3339尾巴(DISTINCT时刻Scan []time.Time+Go去重双方言)/CH24.3无DROP PARTITION IF EXISTS(先查system.parts)/CH count()返回UInt64(*int64不支持+count错误不可吞)/CH绑定time.Time UTC歧义(预格式化墙钟串)——techContext存档;⑥门禁=build/vet/lint 0/-race 41包全绿/迁移v16..v22两遍幂等(v15断言随窗口右移移除);archive单测5件+settle热窗外基线用例;验收单s4-d3-partition-archive.md+CHANGELOG+memory-bank(S4收官快照/S5下一步/挂账清单)
2026-11-16 12:11:31 +08:00

181 lines
5.4 KiB
Go

// archive_test.go S4·D3:分区边界纯函数+归档全链(sqlite+mock CH)+冷查条件。
package archive
import (
"context"
"strings"
"testing"
"time"
"go-sms/internal/model/entity"
"go-sms/internal/repository/clickhouse"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func newArchiveDB(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.MessageDetail{}); err != nil {
t.Fatalf("AutoMigrate 失败: %v", err)
}
return db
}
// mockRows 查询游标打桩(仅支撑 count() 单列场景)。
type mockRows struct {
vals []int64
idx int
}
func (r *mockRows) Next() bool { r.idx++; return r.idx-1 < len(r.vals) }
func (r *mockRows) Scan(dest ...any) error {
if r.idx-1 < len(r.vals) {
v := r.vals[r.idx-1]
switch p := dest[0].(type) {
case *int64:
*p = v
case *uint64: // CH count() UInt64 口径
*p = uint64(v)
}
}
return nil
}
func (r *mockRows) Close() error { return nil }
// mockCH ClickHouse Conn 打桩:记录写入/DDL;count 应答按调用序注入。
type mockCH struct {
inserted [][]any
execs []string
countReplies []int64
}
func (m *mockCH) Ping(context.Context) error { return nil }
func (m *mockCH) Close() error { return nil }
func (m *mockCH) Exec(_ context.Context, q string, _ ...any) error {
m.execs = append(m.execs, q)
return nil
}
func (m *mockCH) BatchInsert(_ context.Context, _ string, _ []string, rows [][]any) error {
m.inserted = append(m.inserted, rows...)
return nil
}
func (m *mockCH) Query(_ context.Context, q string, _ ...any) (clickhouse.Rows, error) {
if strings.Contains(q, "system.parts") {
return &mockRows{vals: []int64{1}}, nil // dropCHPartition 探测:分区存在
}
v := int64(0)
if strings.HasPrefix(q, "SELECT count()") && len(m.countReplies) > 0 {
v = m.countReplies[0]
m.countReplies = m.countReplies[1:]
}
return &mockRows{vals: []int64{v}}, nil
}
var _ clickhouse.Conn = (*mockCH)(nil)
// TestDayFloorSnowflakeMonotonic 边界单调+PartName/HotFloor 口径。
func TestDayFloorSnowflakeMonotonic(t *testing.T) {
base := time.Date(2026, 11, 16, 15, 30, 0, 0, time.Local)
if PartName(base) != "p20261116" {
t.Fatalf("PartName=%s 期望 p20261116", PartName(base))
}
prev := DayFloorSnowflake(base)
for i := 1; i <= 3; i++ {
next := DayFloorSnowflake(base.AddDate(0, 0, i))
if next <= prev {
t.Fatalf("边界非单调: day+%d", i)
}
prev = next
}
if got := HotFloor(base); !got.Equal(time.Date(2026, 11, 12, 0, 0, 0, 0, time.Local)) {
t.Fatalf("HotFloor=%v 期望 2026-11-12 零点", got)
}
}
// TestArchiveOnceFullCycle 归档全链:7 天前 2 行迁出(CH 收到+MySQL 清退,
// sqlite 无分区=DELETE 兜底分支),今天行保留;再跑幂等零动作。
func TestArchiveOnceFullCycle(t *testing.T) {
db := newArchiveDB(t)
old := time.Now().AddDate(0, 0, -7)
mk := func(id int64, at time.Time, mobile string) {
d := entity.MessageDetail{TaskID: 1, AccountID: 5, ChannelID: 7,
Mobile: mobile, Status: 2, Fee: 1}
d.ID = id
d.CreatedAt = at
if err := db.Create(&d).Error; err != nil {
t.Fatal(err)
}
}
mk(73021000001, old, "13800001111")
mk(73021000002, old.Add(time.Hour), "13800002222")
mk(73021000003, time.Now(), "13800003333") // 热窗内:不迁
ch := &mockCH{countReplies: []int64{2}}
s := NewService(db, ch)
if done := s.ArchiveOnce(context.Background()); done != 1 {
t.Fatalf("应归档 1 天, got %d", done)
}
if len(ch.inserted) != 2 {
t.Fatalf("CH 应收到 2 行, got %d", len(ch.inserted))
}
var remainOld, remainNew int64
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800001111").Count(&remainOld)
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800003333").Count(&remainNew)
if remainOld != 0 || remainNew != 1 {
t.Fatalf("清退异常: old=%d new=%d 期望 0/1", remainOld, remainNew)
}
if done := s.ArchiveOnce(context.Background()); done != 0 {
t.Fatalf("再跑应 0 天(幂等), got %d", done)
}
}
// TestArchiveVerifyMismatchKeepsMySQL 校验不匹配:CH count≠MySQL count →
// MySQL 保留(零丢失方向),不计完成。
func TestArchiveVerifyMismatchKeepsMySQL(t *testing.T) {
db := newArchiveDB(t)
old := time.Now().AddDate(0, 0, -7)
d := entity.MessageDetail{TaskID: 1, AccountID: 5, Mobile: "13800009999", Status: 2}
d.ID = 73021000009
d.CreatedAt = old
if err := db.Create(&d).Error; err != nil {
t.Fatal(err)
}
ch := &mockCH{countReplies: []int64{0}}
s := NewService(db, ch)
if done := s.ArchiveOnce(context.Background()); done != 0 {
t.Fatalf("校验不匹配不应计完成, got %d", done)
}
var n int64
db.Model(&entity.MessageDetail{}).Count(&n)
if n != 1 {
t.Fatalf("MySQL 应保留(零丢失方向), got %d", n)
}
}
// TestQueryHistoryWhere 冷查条件构造(六筛选→where 子句与参数序)。
func TestQueryHistoryWhere(t *testing.T) {
st := int8(2)
lo := time.Date(2026, 11, 1, 0, 0, 0, 0, time.Local)
hi := lo.AddDate(0, 0, 5)
w, args := historyWhere(HistoryQuery{
AccountID: 5, Mobile: "138", Status: &st, Start: &lo, End: &hi, TaskID: 9,
})
for _, want := range []string{
"account_id = ?", "mobile = ?", "status = ?",
"created_at >= ?", "created_at < ?", "task_id = ?",
} {
if !strings.Contains(w, want) {
t.Fatalf("where 缺 %q: %s", want, w)
}
}
if len(args) != 6 {
t.Fatalf("参数数 %d 期望 6", len(args))
}
}