v0.4.1:新增 CK 有效性检查并修复抢购调度
This commit is contained in:
@@ -134,6 +134,46 @@ func TestThrottleGovernorEscalatesAndRecovers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrefireProbeCountBounds 验证探活账号数不会跟着高并发设置无限放大。
|
||||
func TestPrefireProbeCountBounds(t *testing.T) {
|
||||
tests := []struct {
|
||||
configured int
|
||||
total int
|
||||
want int
|
||||
}{
|
||||
{configured: 0, total: 100, want: 1},
|
||||
{configured: 2, total: 100, want: 2},
|
||||
{configured: 8, total: 100, want: 8},
|
||||
{configured: 100, total: 100, want: 8},
|
||||
{configured: 5, total: 3, want: 3},
|
||||
{configured: 5, total: 0, want: 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := prefireProbeCount(tt.configured, tt.total); got != tt.want {
|
||||
t.Fatalf("prefireProbeCount(%d,%d)=%d, want %d", tt.configured, tt.total, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrewarmConcurrencyBounds 验证预热并发不会跟着抢购线程设置过低或过高。
|
||||
func TestPrewarmConcurrencyBounds(t *testing.T) {
|
||||
tests := []struct {
|
||||
input int
|
||||
want int
|
||||
}{
|
||||
{input: 0, want: 8},
|
||||
{input: 1, want: 8},
|
||||
{input: 5, want: 8},
|
||||
{input: 12, want: 12},
|
||||
{input: 100, want: 16},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := prewarmConcurrency(tt.input); got != tt.want {
|
||||
t.Fatalf("prewarmConcurrency(%d)=%d, want %d", tt.input, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunStateFirstWaveStagger 验证首发错峰:第 0 个账号立即发,其余按步长错开,每个账号只生效一次。
|
||||
func TestRunStateFirstWaveStagger(t *testing.T) {
|
||||
accounts := []Account{{UID: 1, Cookie: "yyuid=1"}, {UID: 2, Cookie: "yyuid=2"}, {UID: 3, Cookie: "yyuid=3"}}
|
||||
@@ -153,6 +193,294 @@ func TestRunStateFirstWaveStagger(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunStateSessionStreakResetIsScopedAndIndependentOfGovernor 验证会话失效计数
|
||||
// 只按当前账号清理,并且不依赖自适应降速开关。
|
||||
func TestRunStateSessionStreakResetIsScopedAndIndependentOfGovernor(t *testing.T) {
|
||||
for _, withGovernor := range []bool{false, true} {
|
||||
state := newRunState([]Account{{UID: 1}, {UID: 2}}, 1, 1, Config{})
|
||||
if withGovernor {
|
||||
state.governor = newThrottleGovernor()
|
||||
}
|
||||
state.bumpSessionStreak(1, 0)
|
||||
state.bumpSessionStreak(1, 0)
|
||||
|
||||
// 另一个账号拿到正常业务响应,不能重置账号 1 的连续失效计数。
|
||||
state.observeThrottle(2, CategorySoldOut)
|
||||
if got := state.accounts[1][0].SessionStreak; got != 2 {
|
||||
t.Fatalf("governor=%v: 其他账号不应重置会话失效计数,got=%d", withGovernor, got)
|
||||
}
|
||||
|
||||
// 网络错误 / 协议异常没有可信业务响应,也不能把失效计数洗掉。
|
||||
state.observeThrottle(1, CategoryError)
|
||||
state.observeThrottle(1, CategoryProtocol)
|
||||
if got := state.accounts[1][0].SessionStreak; got != 2 {
|
||||
t.Fatalf("governor=%v: 网络/协议错误不应重置会话失效计数,got=%d", withGovernor, got)
|
||||
}
|
||||
|
||||
// 账号 1 自己拿到正常业务响应后必须清零,即使降速器关闭。
|
||||
state.observeThrottle(1, CategorySoldOut)
|
||||
if got := state.accounts[1][0].SessionStreak; got != 0 {
|
||||
t.Fatalf("governor=%v: 本账号恢复后应清零,got=%d", withGovernor, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsWaitsForWindowWithoutPrefire 验证即使不需要预打,也不会提前进入窗口循环。
|
||||
func TestRunTargetsWaitsForWindowWithoutPrefire(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var first time.Time
|
||||
client := &fakeActivityClient{
|
||||
exchangeFn: func(uid int64, sid, pid int32) (*huya.ScoreExchangePrizeResp, error) {
|
||||
mu.Lock()
|
||||
if first.IsZero() {
|
||||
first = time.Now()
|
||||
}
|
||||
mu.Unlock()
|
||||
return &huya.ScoreExchangePrizeResp{Status: 500, Msg: "活动太火爆了,请稍后重试"}, nil
|
||||
},
|
||||
}
|
||||
openAt := time.Now().Add(300 * time.Millisecond)
|
||||
err := (&Runner{Client: client}).RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 1}}, Config{
|
||||
SID: 2203,
|
||||
OpenAt: openAt,
|
||||
windowOverride: 20 * time.Millisecond,
|
||||
Loops: 1,
|
||||
Delay: time.Millisecond,
|
||||
StartAhead: 100 * time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
TooFrequentBackoff: time.Millisecond,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
got := first
|
||||
mu.Unlock()
|
||||
windowStart := openAt.Add(-100 * time.Millisecond)
|
||||
if got.Before(windowStart.Add(-10 * time.Millisecond)) {
|
||||
t.Fatalf("首次请求=%v, 早于窗口起点 %v", got, windowStart)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsStopAccountAfterSuccessStopsAllTargets 验证「账号成功停」是账号级开关:
|
||||
// 成功一个商品后,同一账号不应继续兑换剩余目标。
|
||||
func TestRunTargetsStopAccountAfterSuccessStopsAllTargets(t *testing.T) {
|
||||
client := &fakeActivityClient{}
|
||||
runner := Runner{Client: client}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{PID: 1},
|
||||
{PID: 2},
|
||||
}, Config{
|
||||
SID: 2203,
|
||||
Loops: 2,
|
||||
Delay: time.Millisecond,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, exchangeCount, _ := client.counts(); exchangeCount != 1 {
|
||||
t.Fatalf("exchangeCount=%d, want 1(账号成功后应停止全部目标)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsSessionLostStopsWholeAccount 验证会话连续失效达到阈值后停止整个账号,
|
||||
// 而不是只停当前目标。
|
||||
func TestRunTargetsSessionLostStopsWholeAccount(t *testing.T) {
|
||||
client := &fakeActivityClient{
|
||||
exchangeFn: func(uid int64, sid, pid int32) (*huya.ScoreExchangePrizeResp, error) {
|
||||
return &huya.ScoreExchangePrizeResp{Status: 500, Msg: "账号未绑定!"}, nil
|
||||
},
|
||||
}
|
||||
runner := Runner{Client: client}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{PID: 1},
|
||||
{PID: 2},
|
||||
}, Config{
|
||||
SID: 2203,
|
||||
Loops: 10,
|
||||
Delay: 5 * time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
SessionLostLimit: 3,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 第 1、2 轮各打两个目标;第 3 轮第一个目标连续第 3 次失效后整账号停止。
|
||||
if _, exchangeCount, _ := client.counts(); exchangeCount != 5 {
|
||||
t.Fatalf("exchangeCount=%d, want 5(达到阈值后应停止整个账号)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunStateElapsedUsesOpenAt 验证退避阶段从开抢时刻起算,而不是从提前启动时起算。
|
||||
func TestRunStateElapsedUsesOpenAt(t *testing.T) {
|
||||
state := &runState{
|
||||
startedAt: time.Now().Add(-10 * time.Second),
|
||||
openAt: time.Now().Add(-2 * time.Second),
|
||||
}
|
||||
if got := state.elapsed(); got < 1900*time.Millisecond || got > 3*time.Second {
|
||||
t.Fatalf("elapsed=%v, want 约 2s(按 openAt 计算)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsNotFoundBacksOff 验证未命中时不会每轮刷事件和列表请求。
|
||||
func TestRunTargetsNotFoundBacksOff(t *testing.T) {
|
||||
client := &fakeActivityClient{listResp: &huya.GetActPrizeListResp{Status: 200}}
|
||||
runner := Runner{Client: client}
|
||||
var notFound int32
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{Name: "不存在的奖品"},
|
||||
}, Config{
|
||||
SID: 2203,
|
||||
Loops: 6,
|
||||
Delay: time.Millisecond,
|
||||
ErrorBackoff: 25 * time.Millisecond,
|
||||
ListBeforeExchange: true,
|
||||
}, func(result Result) {
|
||||
if result.Category == CategoryNotFound {
|
||||
atomic.AddInt32(¬Found, 1)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := atomic.LoadInt32(¬Found); got != 1 {
|
||||
t.Fatalf("not_found 事件=%d, want 1(退避期间应跳过)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsNotOpenBacksOff 验证未开放时也会退避,不会以 Delay 持续轰接口。
|
||||
func TestRunTargetsNotOpenBacksOff(t *testing.T) {
|
||||
client := &fakeActivityClient{
|
||||
exchangeFn: func(uid int64, sid, pid int32) (*huya.ScoreExchangePrizeResp, error) {
|
||||
return &huya.ScoreExchangePrizeResp{Status: 500, Msg: "奖品暂未开放兑换"}, nil
|
||||
},
|
||||
}
|
||||
runner := Runner{Client: client}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{PID: 1},
|
||||
}, Config{
|
||||
SID: 2203,
|
||||
Loops: 6,
|
||||
Delay: time.Millisecond,
|
||||
ErrorBackoff: 25 * time.Millisecond,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, exchangeCount, _ := client.counts(); exchangeCount != 1 {
|
||||
t.Fatalf("exchangeCount=%d, want 1(未开放应退避)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestExchangeTargetSkipsAfterWindow 验证排队拿到并发额度时窗口已结束,
|
||||
// 不会把兑换请求补发到窗口外。
|
||||
func TestExchangeTargetSkipsAfterWindow(t *testing.T) {
|
||||
client := &fakeActivityClient{}
|
||||
account := Account{UID: 1, Cookie: "yyuid=1"}
|
||||
state := newRunState([]Account{account}, 1, 1, Config{
|
||||
OpenAt: time.Now().Add(-2 * time.Second),
|
||||
WindowSeconds: 1,
|
||||
})
|
||||
result := (&Runner{Client: client}).exchangeTarget(
|
||||
context.Background(), client, state, account, Target{PID: 1}, 0, 1, 2203, nil,
|
||||
)
|
||||
if result.Action != actionExchangeCanceled {
|
||||
t.Fatalf("action=%q, want %q", result.Action, actionExchangeCanceled)
|
||||
}
|
||||
if _, exchangeCount, _ := client.counts(); exchangeCount != 0 {
|
||||
t.Fatalf("exchangeCount=%d, want 0(窗口外不能补发)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsDirectExchangeHonorsPriceFilter 验证设置价格门槛后不能走直兑绕过过滤。
|
||||
func TestRunTargetsDirectExchangeHonorsPriceFilter(t *testing.T) {
|
||||
client := &fakeActivityClient{
|
||||
listResp: &huya.GetActPrizeListResp{
|
||||
Status: 200,
|
||||
Prizes: []huya.PrizeInfo{{ID: 1, Name: "涨价商品", NewScore: 100}},
|
||||
},
|
||||
}
|
||||
err := (&Runner{Client: client}).RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{PID: 1, MaxPrice: 50},
|
||||
}, Config{
|
||||
SID: 2203,
|
||||
Loops: 1,
|
||||
Delay: time.Millisecond,
|
||||
DirectExchange: true,
|
||||
ListBeforeExchange: false,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listCount, exchangeCount, _ := client.counts()
|
||||
if listCount != 1 || exchangeCount != 0 {
|
||||
t.Fatalf("list=%d exchange=%d, want list=1 exchange=0(价格超限不能直兑)", listCount, exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsPrefireProbeIsBounded 验证预打探活最多只用一个并发批次,
|
||||
// 不会在账号很多时把请求拖到开抢之后。
|
||||
func TestRunTargetsPrefireProbeIsBounded(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
calls := make([]time.Time, 0, 8)
|
||||
client := &fakeActivityClient{
|
||||
exchangeFn: func(uid int64, sid, pid int32) (*huya.ScoreExchangePrizeResp, error) {
|
||||
mu.Lock()
|
||||
calls = append(calls, time.Now())
|
||||
mu.Unlock()
|
||||
return &huya.ScoreExchangePrizeResp{Status: 500, Msg: "奖品暂未开放兑换"}, nil
|
||||
},
|
||||
}
|
||||
accounts := make([]Account, 0, 6)
|
||||
for uid := int64(1); uid <= 6; uid++ {
|
||||
accounts = append(accounts, Account{UID: uid, Cookie: "yyuid=1"})
|
||||
}
|
||||
openAt := time.Now().Add(350 * time.Millisecond)
|
||||
err := (&Runner{Client: client}).RunTargets(context.Background(), accounts, []Target{{PID: 1}}, Config{
|
||||
SID: 2203,
|
||||
OpenAt: openAt,
|
||||
windowOverride: 30 * time.Millisecond,
|
||||
Loops: 1,
|
||||
Delay: time.Millisecond,
|
||||
StartAhead: 20 * time.Millisecond,
|
||||
Concurrency: 2,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
TooFrequentBackoff: time.Millisecond,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
windowStart := openAt.Add(-20 * time.Millisecond)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
prefire := 0
|
||||
for _, at := range calls {
|
||||
if at.Before(windowStart) {
|
||||
prefire++
|
||||
}
|
||||
}
|
||||
if prefire > 2 {
|
||||
t.Fatalf("预打请求=%d, want <= 2(仅一个并发批次)", prefire)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRetryDelayNeverExceedsCap 验证全局降速和抖动不能把单次退避放大到上限之外。
|
||||
func TestRetryDelayNeverExceedsCap(t *testing.T) {
|
||||
state := newRunState([]Account{{UID: 1}}, 1, 1, Config{})
|
||||
state.governor = newThrottleGovernor()
|
||||
state.governor.factor = governorMaxFactor
|
||||
for i := 0; i < 20; i++ {
|
||||
got := state.retryDelay(1, 0, Config{
|
||||
ErrorBackoff: 300 * time.Millisecond,
|
||||
TooFrequentBackoff: 300 * time.Millisecond,
|
||||
}, CategoryTooFrequent)
|
||||
if got > maxRetryDelay {
|
||||
t.Fatalf("retryDelay=%v, want <= %v", got, maxRetryDelay)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTargetsWindowModeKeepsTryingUntilDeadline 是 P1 的核心回归:
|
||||
// 账号很少时,旧实现会「20 轮 × 150ms」在开抢前把预算烧光(pc-3 instance-4 0 成功),
|
||||
// 窗口模式必须一直打到窗口结束。
|
||||
|
||||
Reference in New Issue
Block a user