105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
// backpressure_test.go Day 48:三级水位单测——阈值判定/边缘动作(暂停恢复)/
|
|
// 水位键写入(miniredis)/ReadBPLevel 网关侧读取。
|
|
package send
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"go.uber.org/zap"
|
|
|
|
"go-sms/internal/pkg/cache"
|
|
)
|
|
|
|
// TestBPLevelOf 三级阈值(10万/100万/500万,含边界)。
|
|
func TestBPLevelOf(t *testing.T) {
|
|
cases := []struct {
|
|
lag int64
|
|
want BPLevel
|
|
}{
|
|
{0, BPNormal}, {100_000, BPNormal}, {100_001, BPAlert},
|
|
{1_000_000, BPAlert}, {1_000_001, BPTighten},
|
|
{5_000_000, BPTighten}, {5_000_001, BPPause},
|
|
}
|
|
for _, c := range cases {
|
|
if got := BPLevelOf(c.lag); got != c.want {
|
|
t.Fatalf("BPLevelOf(%d)=%d 期望 %d", c.lag, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// fakeBPStore 捕获写入。
|
|
type fakeBPStore struct {
|
|
levels []BPLevel
|
|
caps []int64
|
|
}
|
|
|
|
func (f *fakeBPStore) SetLevel(_ context.Context, l BPLevel, cap int64) error {
|
|
f.levels = append(f.levels, l)
|
|
f.caps = append(f.caps, cap)
|
|
return nil
|
|
}
|
|
|
|
// TestBPCheckEdgeActions 边缘动作:L3 升沿 pause 一次;回落 resume 一次;
|
|
// L2 持续写水位键(每拍)但动作不重复。
|
|
func TestBPCheckEdgeActions(t *testing.T) {
|
|
store := &fakeBPStore{}
|
|
paused, resumed := 0, 0
|
|
bp := NewBackpressure(store, zap.NewNop(),
|
|
func() { paused++ }, func() { resumed++ }, func() int64 { return 3000 })
|
|
ctx := context.Background()
|
|
|
|
bp.Check(ctx, 50_000) // 正常
|
|
bp.Check(ctx, 100_001) // →L1
|
|
bp.Check(ctx, 1_000_001) // →L2(收紧键带 cap)
|
|
bp.Check(ctx, 1_500_000) // L2 持续
|
|
bp.Check(ctx, 5_000_001) // →L3 pause
|
|
bp.Check(ctx, 6_000_000) // L3 持续
|
|
bp.Check(ctx, 90_000) // →正常 resume
|
|
|
|
if paused != 1 || resumed != 1 {
|
|
t.Fatalf("pause=%d resume=%d 期望各 1", paused, resumed)
|
|
}
|
|
if len(store.levels) != 7 {
|
|
t.Fatalf("水位键每拍写入: %d", len(store.levels))
|
|
}
|
|
// L2/L3 期间(第 3~6 拍)携带入口收紧预算;末拍回落不带。
|
|
for i := 2; i < len(store.caps)-1; i++ {
|
|
if store.caps[i] != 3000 {
|
|
t.Fatalf("第 %d 拍 cap=%d 期望 3000", i, store.caps[i])
|
|
}
|
|
}
|
|
if last := store.caps[len(store.caps)-1]; last != 0 {
|
|
t.Fatalf("回落拍 cap=%d 期望 0", last)
|
|
}
|
|
}
|
|
|
|
// TestRedisBPStoreRoundTrip miniredis 写读 + ReadBPLevel 缺失键=正常。
|
|
func TestRedisBPStoreRoundTrip(t *testing.T) {
|
|
mr := miniredis.RunT(t)
|
|
rc, err := cache.New(cache.Options{Addrs: []string{mr.Addr()}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := NewRedisBPStore(rc)
|
|
ctx := context.Background()
|
|
|
|
if got := ReadBPLevel(ctx, rc); got != BPNormal {
|
|
t.Fatalf("缺键应正常: %d", got)
|
|
}
|
|
if err := store.SetLevel(ctx, BPTighten, 3000); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ReadBPLevel(ctx, rc); got != BPTighten {
|
|
t.Fatalf("读水位 = %d 期望 L2", got)
|
|
}
|
|
if v, err := rc.Get(ctx, BPCapKey).Int64(); err != nil || v != 3000 {
|
|
t.Fatalf("cap 键 = %d err=%v", v, err)
|
|
}
|
|
// nil store 降级不动作。
|
|
if err := NewRedisBPStore(nil).SetLevel(ctx, BPPause, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|