166 lines
5.5 KiB
Go
166 lines
5.5 KiB
Go
// frequency_test.go Day 42:频次滑动窗口——自然日/滚动 24h/同内容维/DryRun/Redis 实现。
|
|
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"go-sms/internal/model/entity"
|
|
)
|
|
|
|
// TestFrequencyNaturalDay 验收:同号码自然日窗口 max_count=2,第 3 次拦截(ISC:032)。
|
|
func TestFrequencyNaturalDay(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := db.Create(&entity.PolicyFrequency{
|
|
AccountID: 1, Dim: 1, WindowUnit: 1, MaxCount: 2, Status: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := NewFrequency(db, nil)
|
|
ctx := context.Background()
|
|
req := &Req{AccountID: 1, Mobile: "13811112222", Content: "【测试】内容"}
|
|
for i := 1; i <= 2; i++ {
|
|
if b := f.Check(ctx, req); b != nil {
|
|
t.Fatalf("第 %d 次不应拦截", i)
|
|
}
|
|
}
|
|
b := f.Check(ctx, req)
|
|
if b == nil || b.Code != "ISC:032" || b.Interceptor != "frequency" {
|
|
t.Fatalf("第 3 次应频次拦截 ISC:032: %+v", b)
|
|
}
|
|
}
|
|
|
|
// TestFrequencyRolling24h 验收:滚动 24h 窗口过期后放行(注入时钟)。
|
|
func TestFrequencyRolling24h(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := db.Create(&entity.PolicyFrequency{
|
|
AccountID: 1, Dim: 1, WindowUnit: 2, MaxCount: 1, Status: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now()
|
|
store := NewMemoryFreqStore()
|
|
f := &Frequency{DB: db, Store: store, Now: func() time.Time { return now }}
|
|
ctx := context.Background()
|
|
req := &Req{AccountID: 1, Mobile: "13822223333", Content: "【测试】x"}
|
|
if b := f.Check(ctx, req); b != nil {
|
|
t.Fatal("第 1 次应放行")
|
|
}
|
|
if f.Check(ctx, req) == nil {
|
|
t.Fatal("第 2 次应拦截(滚动窗内)")
|
|
}
|
|
now = now.Add(25 * time.Hour) // 窗口滑出
|
|
store.now = func() time.Time { return now } // 内存 store 同步时钟
|
|
f.Now = func() time.Time { return now }
|
|
if b := f.Check(ctx, req); b != nil {
|
|
t.Fatalf("窗口过期后应放行: %+v", b)
|
|
}
|
|
}
|
|
|
|
// TestFrequencyContentDim 验收:同内容维(dim=2)不同号码同内容受限;换内容放行。
|
|
func TestFrequencyContentDim(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := db.Create(&entity.PolicyFrequency{
|
|
AccountID: 1, Dim: 2, WindowUnit: 1, MaxCount: 1, Status: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := NewFrequency(db, nil)
|
|
ctx := context.Background()
|
|
c1 := "【测试】活动 A"
|
|
if b := f.Check(ctx, &Req{AccountID: 1, Mobile: "13800000001", Content: c1}); b != nil {
|
|
t.Fatal("首条同内容应放行")
|
|
}
|
|
if f.Check(ctx, &Req{AccountID: 1, Mobile: "13800000002", Content: c1}) == nil {
|
|
t.Fatal("不同号码同内容(超限)应拦截")
|
|
}
|
|
if b := f.Check(ctx, &Req{AccountID: 1, Mobile: "13800000003", Content: "【测试】活动 B"}); b != nil {
|
|
t.Fatal("换内容应放行")
|
|
}
|
|
}
|
|
|
|
// TestFrequencyDryRun 验收:预检只读不占配额。
|
|
func TestFrequencyDryRun(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := db.Create(&entity.PolicyFrequency{
|
|
AccountID: 1, Dim: 1, WindowUnit: 1, MaxCount: 1, Status: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := NewFrequency(db, nil)
|
|
ctx := context.Background()
|
|
// 已发送 1 次(占满配额)。
|
|
_ = f.Check(ctx, &Req{AccountID: 1, Mobile: "13833334444", Content: "x"})
|
|
// DryRun:已达上限报告拦截,且不增加计数。
|
|
dry := &Req{AccountID: 1, Mobile: "13833334444", Content: "x", DryRun: true}
|
|
if f.Check(ctx, dry) == nil {
|
|
t.Fatal("DryRun 已满配额应报告拦截")
|
|
}
|
|
if f.Check(ctx, dry) == nil {
|
|
t.Fatal("DryRun 重复调用仍应拦截(未占新配额)")
|
|
}
|
|
// DryRun 不占配额:另一未用号 DryRun 不拦且之后真发仍放行。
|
|
fresh := &Req{AccountID: 1, Mobile: "13833335555", Content: "x"}
|
|
freshDry := &Req{AccountID: 1, Mobile: "13833335555", Content: "x", DryRun: true}
|
|
if f.Check(ctx, freshDry) != nil {
|
|
t.Fatal("未用号 DryRun 应放行")
|
|
}
|
|
if b := f.Check(ctx, fresh); b != nil {
|
|
t.Fatalf("DryRun 不应占配额: %+v", b)
|
|
}
|
|
}
|
|
|
|
// TestFrequencyWhitelistExempt 验收:白名单豁免频次。
|
|
func TestFrequencyWhitelistExempt(t *testing.T) {
|
|
db := newTestDB(t)
|
|
if err := db.Create(&entity.PolicyFrequency{
|
|
AccountID: 1, Dim: 1, WindowUnit: 1, MaxCount: 1, Status: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&entity.PolicyWhitelist{Mobile: "13844445555", AccountID: 1}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := NewFrequency(db, nil)
|
|
ctx := context.Background()
|
|
req := &Req{AccountID: 1, Mobile: "13844445555", Content: "x"}
|
|
for i := 0; i < 3; i++ {
|
|
if b := f.Check(ctx, req); b != nil {
|
|
t.Fatalf("白名单第 %d 次应豁免: %+v", i+1, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRedisFreqStore 验收:Redis ZSet 滑窗与内存实现行为一致。
|
|
func TestRedisFreqStore(t *testing.T) {
|
|
mr := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
defer func() { _ = rdb.Close() }()
|
|
s := NewRedisFreqStore(rdb)
|
|
ctx := context.Background()
|
|
const key = "gosms:freq:test"
|
|
for i := int64(1); i <= 3; i++ {
|
|
n, err := s.Incr(ctx, key, time.Hour)
|
|
if err != nil || n != i {
|
|
t.Fatalf("Incr 第 %d 次: n=%d err=%v", i, n, err)
|
|
}
|
|
}
|
|
// 窗口滑出:快进 61 分钟(miniredis 快进真实时间不可,直接清过期分数)。
|
|
mr.FastForward(61 * time.Minute)
|
|
n, err := s.Incr(ctx, key, time.Hour)
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("窗口过期后应重新计数: n=%d err=%v", n, err)
|
|
}
|
|
// Count 只读。
|
|
if c, err := s.Count(ctx, key, time.Hour); err != nil || c != 1 {
|
|
t.Fatalf("Count 只读应=1: c=%d err=%v", c, err)
|
|
}
|
|
if n, _ := s.Incr(ctx, key, time.Hour); n != 2 {
|
|
t.Fatal("Count 后 Incr 应=2(只读未写入)")
|
|
}
|
|
}
|