Files
dygou_go/internal/seckill/scheduler_test.go
T
yml2213 1f76c12b48 精英手册改为锁单支付并发布0.2.2
- 精英手册兑换改为 createExchangeOrder 锁单后 payExchangeOrder 支付,不再查询商品状态与预兑
- 电竞手册保留直接兑换,标准模式重新取鉴权,激进模式复用鉴权
- csrfToken 优先复用 Cookie,校验失败自动刷新后重试一次
- 锁单支付按频控/通用退避表重试,临近过期不再重试
- 定时复校发现抢购已开始则不再改写触发时刻
- 升级 wails 至 v2.15.0
2026-09-30 20:35:44 +08:00

301 lines
10 KiB
Go

package seckill
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"douyu_go/internal/activity"
"douyu_go/internal/client"
"douyu_go/internal/trace"
"douyu_go/internal/types"
)
func makeAttempts(names ...string) []types.ExchangeAttempt {
out := make([]types.ExchangeAttempt, 0, len(names))
for i, n := range names {
out = append(out, types.ExchangeAttempt{
Account: types.Account{Name: n},
Index: i + 1,
})
}
return out
}
func TestPeacePayBackoff(t *testing.T) {
if got, ok := peacePayBackoff(0, true); !ok || got != 30*time.Second {
t.Fatalf("first rate backoff = %v ok=%v", got, ok)
}
if _, ok := peacePayBackoff(len(peaceRateLimitBackoffs), true); ok {
t.Fatal("rate backoff should stop after the configured attempts")
}
if got, ok := peacePayBackoff(1, false); !ok || got != 5*time.Second {
t.Fatalf("second generic backoff = %v ok=%v", got, ok)
}
}
func TestPeacePurchaseLocksThenPaysWithoutChecks(t *testing.T) {
oldMin, oldMax := peacePreLockMin, peacePreLockMax
oldPayMin, oldPayMax := peaceLockPayMin, peaceLockPayMax
peacePreLockMin, peacePreLockMax = 0, 0
peaceLockPayMin, peaceLockPayMax = 0, 0
t.Cleanup(func() {
peacePreLockMin, peacePreLockMax = oldMin, oldMax
peaceLockPayMin, peaceLockPayMax = oldPayMin, oldPayMax
})
var calls []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls = append(calls, r.URL.Path)
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/create":
if err := r.ParseMultipartForm(1 << 20); err != nil {
t.Errorf("ParseMultipartForm() error = %v", err)
return
}
if _, ok := r.MultipartForm.Value["orderID"]; ok {
t.Error("锁单请求不应携带支付订单号")
}
_, _ = w.Write([]byte(`{"error":0,"data":{"orderId":"order-9","expireSeconds":300}}`))
case "/pay":
if err := r.ParseMultipartForm(1 << 20); err != nil {
t.Errorf("ParseMultipartForm() error = %v", err)
return
}
if got := r.MultipartForm.Value["orderID"][0]; got != "order-9" {
t.Errorf("orderID = %q", got)
}
_, _ = w.Write([]byte(`{"error":0,"data":{"exchangeId":9,"exchangeNum":"1"}}`))
case "/bind":
_, _ = w.Write([]byte(`{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`))
default:
t.Errorf("不应请求预检或单步兑换接口: %s", r.URL.Path)
http.NotFound(w, r)
}
}))
defer server.Close()
act := *activity.Get("peace")
act.CreateOrderURL = server.URL + "/create"
act.PayOrderURL = server.URL + "/pay"
act.BindInfoURL = server.URL + "/bind"
act.CSRFURL = server.URL + "/csrf"
cli := client.New(&act, "", &trace.Recorder{})
cli.HTTPClient = server.Client()
scheduler := New(cli, types.AppPaths{}, nil, 1, 1, nil, types.SeckillModeAggressive)
result := scheduler.runPeaceLockPay(types.ExchangeAttempt{
Account: types.Account{Name: "a", Cookie: "cvl_csrf_token=tok"},
Product: types.Product{CommodityID: "goods-1", CommodityName: "测试商品"},
Index: 1,
})
if !result.OK || result.RoleName != "测试角色" {
t.Fatalf("result = %#v", result)
}
if len(calls) < 2 || calls[0] != "/create" || calls[1] != "/pay" {
t.Fatalf("calls = %v, want create then pay", calls)
}
for _, path := range calls {
switch path {
case "/create", "/pay", "/bind":
default:
t.Fatalf("unexpected call %s in %v", path, calls)
}
}
}
func TestRetryIntervalFor(t *testing.T) {
stages := []types.RetryStage{
{MaxElapsed: 4, Interval: 120},
{MaxElapsed: 12, Interval: 200},
{MaxElapsed: 30, Interval: 500},
{MaxElapsed: 75, Interval: 1000},
}
cases := []struct {
elapsed time.Duration
want time.Duration
}{
{1 * time.Second, 120 * time.Millisecond},
{5 * time.Second, 200 * time.Millisecond},
{13 * time.Second, 500 * time.Millisecond},
{31 * time.Second, 1000 * time.Millisecond},
{100 * time.Second, 900 * time.Millisecond}, // 超出所有阶段,用默认值
}
for _, c := range cases {
if got := retryIntervalFor(stages, c.elapsed); got != c.want {
t.Errorf("retryIntervalFor(%v) = %v, want %v", c.elapsed, got, c.want)
}
}
}
func TestRetryIntervalForEmpty(t *testing.T) {
// 空阶段或无效阶段都回退到默认 900ms
if got := retryIntervalFor(nil, 1*time.Second); got != 900*time.Millisecond {
t.Errorf("retryIntervalFor(nil) = %v, want 900ms", got)
}
if got := retryIntervalFor([]types.RetryStage{{MaxElapsed: 0, Interval: 0}}, 1*time.Second); got != 900*time.Millisecond {
t.Errorf("retryIntervalFor(invalid) = %v, want 900ms", got)
}
}
func TestNormalizeXiaodianRun(t *testing.T) {
attempts, delay := normalizeXiaodianRun(0, 0, 4)
if attempts != 4 || delay != xiaodianDefaultDelay {
t.Fatalf("normalizeXiaodianRun fallback = (%d,%v), want (4,%v)", attempts, delay, xiaodianDefaultDelay)
}
attempts, delay = normalizeXiaodianRun(100001, 70000, 0)
if attempts != 100000 || delay != 60*time.Second {
t.Fatalf("normalizeXiaodianRun clamp = (%d,%v), want (100000,60s)", attempts, delay)
}
attempts, delay = normalizeXiaodianRun(2, 150, 0)
if attempts != 2 || delay != 150*time.Millisecond {
t.Fatalf("normalizeXiaodianRun explicit = (%d,%v), want (2,150ms)", attempts, delay)
}
}
func TestRetryCooldownDuration(t *testing.T) {
cases := []struct {
msg string
want time.Duration
}{
{"用户行为异常,请稍后再试", exchangeRateCooldown},
{"点击太快了", exchangeRateCooldown},
{"操作频繁请稍后", exchangeRateCooldown},
{"请求过多", exchangeRateCooldown},
{"请求太频繁", exchangeRateCooldown},
{"当前活动太火爆", exchangeRateCooldown},
{"兑换成功", 0},
{"库存不足", 0},
}
for _, c := range cases {
if got := retryCooldownDuration(c.msg); got != c.want {
t.Errorf("retryCooldownDuration(%q) = %v, want %v", c.msg, got, c.want)
}
}
}
func TestClassifyRetryStop(t *testing.T) {
acc, prod, reason := classifyRetryStop("您已达本次活动兑换上限")
if !acc || prod || reason != "已达兑换上限" {
t.Errorf("classifyRetryStop(兑换上限) = (%v,%v,%q), want (true,false,'已达兑换上限')", acc, prod, reason)
}
acc, prod, reason = classifyRetryStop("未绑定账号")
if !acc || prod || reason != "账号未绑定角色" {
t.Errorf("classifyRetryStop(未绑定) = (%v,%v,%q), want (true,false,'账号未绑定角色')", acc, prod, reason)
}
acc, prod, reason = classifyRetryStop("需要开通精英手册")
if !acc || prod || reason != "账号不满足精英手册条件" {
t.Errorf("classifyRetryStop(精英手册) = (%v,%v,%q), want (true,false,'账号不满足精英手册条件')", acc, prod, reason)
}
// 库存耗尽不停止
acc, prod, reason = classifyRetryStop("商品本次活动库存已耗完")
if acc || prod || reason != "" {
t.Errorf("classifyRetryStop(库存耗完) = (%v,%v,%q), want (false,false,'')", acc, prod, reason)
}
// 普通错误不停止
acc, prod, reason = classifyRetryStop("网络超时")
if acc || prod || reason != "" {
t.Errorf("classifyRetryStop(普通错误) = (%v,%v,%q), want (false,false,'')", acc, prod, reason)
}
}
func TestClassifyXiaodianRetryStopOwnedItem(t *testing.T) {
for _, message := range []string{"您已拥有该道具!", "已经拥有该道具", "账号已拥有该商品"} {
acc, prod, reason := classifyXiaodianRetryStop(message)
if !acc || prod || reason != "账号已拥有该道具" {
t.Errorf("classifyXiaodianRetryStop(%q) = (%v,%v,%q), want (true,false,'账号已拥有该道具')", message, acc, prod, reason)
}
}
}
func TestClassifyXiaodianRetryStop(t *testing.T) {
cases := []struct {
message string
account bool
product bool
reason string
}{
{"对不起,Livelink登录态校验失败!", true, false, "和平小店绑定已失效,需重新绑定"},
{"不好意思,您的余额不足~", true, false, "点券余额不足"},
{"点券不足,请充值后重试", true, false, "点券余额不足"},
{"未绑定账号", true, false, "账号未绑定角色"},
{"商品不存在", false, true, "商品不可兑换"},
{"已达限量", false, false, ""},
{"已达本次活动兑换上限", false, false, ""},
{"库存不足", false, false, ""},
}
for _, tc := range cases {
account, product, reason := classifyXiaodianRetryStop(tc.message)
if account != tc.account || product != tc.product || reason != tc.reason {
t.Errorf("classifyXiaodianRetryStop(%q) = (%v,%v,%q), want (%v,%v,%q)",
tc.message, account, product, reason, tc.account, tc.product, tc.reason)
}
}
}
func TestFilterCooldownAttempts(t *testing.T) {
attempts := makeAttempts("a", "b", "c")
now := time.Now()
cooldown := map[string]time.Time{
"a": now.Add(5 * time.Second), // 仍在冷却
"b": now.Add(-5 * time.Second), // 已过期
}
out, nextReady := filterCooldownAttempts(attempts, cooldown, now)
if len(out) != 2 {
t.Fatalf("filterCooldownAttempts out length = %d, want 2", len(out))
}
for _, a := range out {
if a.Account.Name == "a" {
t.Errorf("account 'a' should be filtered out")
}
}
if nextReady.IsZero() {
t.Errorf("nextReady should not be zero")
}
if !nextReady.After(now) {
t.Errorf("nextReady should be after now")
}
}
func TestPickRoundAttempts(t *testing.T) {
// 账号 A 有 3 个包,账号 B 有 3 个包
attempts := []types.ExchangeAttempt{
{Account: types.Account{Name: "A"}, Index: 1},
{Account: types.Account{Name: "A"}, Index: 2},
{Account: types.Account{Name: "A"}, Index: 3},
{Account: types.Account{Name: "B"}, Index: 1},
{Account: types.Account{Name: "B"}, Index: 2},
{Account: types.Account{Name: "B"}, Index: 3},
}
// 第 1 轮每账号选第 1 包
out := pickRoundAttempts(attempts, 1, 3)
if len(out) != 2 {
t.Fatalf("pickRoundAttempts round1 length = %d, want 2", len(out))
}
for _, a := range out {
if a.Index != 1 {
t.Errorf("round1 should pick index 1, got %d for %s", a.Index, a.Account.Name)
}
}
// 第 2 轮每账号选第 2 包
out = pickRoundAttempts(attempts, 2, 3)
for _, a := range out {
if a.Index != 2 {
t.Errorf("round2 should pick index 2, got %d for %s", a.Index, a.Account.Name)
}
}
// attemptsPerAccount=0 时回退取各自首个
out = pickRoundAttempts(attempts, 1, 0)
for _, a := range out {
if a.Index != 1 {
t.Errorf("round with 0 perAccount should pick index 1, got %d", a.Index)
}
}
// 空输入
if out := pickRoundAttempts(nil, 1, 3); out != nil {
t.Errorf("pickRoundAttempts(nil) = %v, want nil", out)
}
}