抢购后自动核对积分余额(基线在导入账号时获取),发布0.3.5
兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据—— 2026-09-30 出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。 基线的获取时机刻意放在抢购链路之外:轮次/预热期间任何额外请求都会挤占锁单, 账号越多越明显。基线只在三个时机更新: - 导入账号后自动查一次(后台执行,不阻塞导入返回) - 用户手动点"查询积分"时 - 每次抢购核对之后把最新余额存回,下一次抢购的基线就是上一次的终值,差值才准 账号被编辑(改名/换 Cookie)时清掉旧基线,删除账号时一并清理。 - 新增 ScoreBaselineStore(scores.json,与账号库同目录,原子写盘): 记录每个账号上次已知的积分余额与时间;账号库路径为空时退化为纯内存,不写工作目录 - 核对输出示例: "积分已扣减,兑换确认: 账号=a 抢购前=1000 抢购后=800 扣减=200" 并把"|积分-200"附到结果集,账号表里也能直接看到确认信息 - 没有基线时只记录余额并提示"下次抢购即可比对",不会误报"未扣分"; 积分接口失败只记警告,不改变"抢到"这个结论 - 抢购进行中调用导入不会去查询积分(避免与锁单抢连接),提示稍后手动查询 - 预热日志措辞修正:"开始预热: N 个请求" → "N 个账号"(每账号实际不止一个请求) - 新增测试:TestVerifyRunResultsConfirmsScoreDeduction、TestVerifyRunResultsWithoutBaseline、 TestVerifyRunResultsIgnoresScoreFailure、TestScoreBaselineStorePersists、 TestFetchScoreBaselinesRecordsValues
This commit is contained in:
@@ -55,6 +55,8 @@ func newOrderTestScheduler(t *testing.T, handler http.HandlerFunc) *Scheduler {
|
||||
act.PayOrderURL = server.URL + "/pay"
|
||||
act.BindInfoURL = server.URL + "/bind"
|
||||
act.CSRFURL = server.URL + "/csrf"
|
||||
act.ScoreURL = server.URL + "/score"
|
||||
act.ScoreMethod = "POST"
|
||||
cli := client.New(&act, "", &trace.Recorder{})
|
||||
cli.HTTPClient = server.Client()
|
||||
return New(cli, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
@@ -694,10 +696,13 @@ func TestRunNowEndToEnd(t *testing.T) {
|
||||
scheduler := newOrderTestScheduler(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
name := ""
|
||||
if r.URL.Path != "/bind" {
|
||||
if r.URL.Path == "/create" || r.URL.Path == "/pay" {
|
||||
name = requestAccount(t, r)
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case "/score":
|
||||
// 抢购结束后的积分核对(JSON 请求,不走 multipart)
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"balance":900}}`))
|
||||
case "/create":
|
||||
if name == "winner" {
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"orderId":"order-w","expireSeconds":300}}`))
|
||||
@@ -1008,3 +1013,235 @@ func dumpMessages(store *logger.MemoryLogStore) string {
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// TestVerifyRunResultsConfirmsScoreDeduction 抢购结束后用"积分扣减"确认兑换:
|
||||
// 只看接口返回不够——2026-09-30 出现过订单实际已支付却被报成失败。
|
||||
// 基线来自账号导入/上次查积分时记录的余额(这里预置 1000),抢购后查到 800 → 扣 200。
|
||||
func TestVerifyRunResultsConfirmsScoreDeduction(t *testing.T) {
|
||||
speedUpPeace(t)
|
||||
seckillMaxDuration = 1500 * time.Millisecond
|
||||
|
||||
var scoreCalls int32
|
||||
scheduler := newOrderTestScheduler(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/create":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"orderId":"order-1","expireSeconds":300}}`))
|
||||
case "/pay":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"exchangeId":9,"exchangeNum":"1"}}`))
|
||||
case "/bind":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`))
|
||||
case "/score":
|
||||
// 抢购后的余额:比基线少 200
|
||||
atomic.AddInt32(&scoreCalls, 1)
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"balance":800}}`))
|
||||
}
|
||||
})
|
||||
// 预置抢购前基线(真实流程里由"导入账号"或手动查积分写入)
|
||||
scheduler.RecordScoreBaseline("a", 1000)
|
||||
scheduler.paths = writeAccountFile(t, "a")
|
||||
scheduler.accounts = NewAccountStore(scheduler.paths.AccountFile)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{CommodityID: "goods-1", CommodityName: "测试商品"}})
|
||||
|
||||
store := logger.NewMemoryLogStore(4000)
|
||||
cleanup := logger.InitLogging(store, t.TempDir(), "测试实例", "/tmp/douyu-go-test")
|
||||
defer cleanup()
|
||||
|
||||
scheduler.RunNow(map[string]string{"a": "goods-1"})
|
||||
|
||||
var deducted, done, result string
|
||||
for _, entry := range store.Since(0) {
|
||||
switch {
|
||||
case strings.Contains(entry.Message, "积分已扣减,兑换确认"):
|
||||
deducted = entry.Message
|
||||
case strings.Contains(entry.Message, "积分核对完成"):
|
||||
done = entry.Message
|
||||
}
|
||||
}
|
||||
key := resultKey(types.ExchangeAttempt{Account: types.Account{Name: "a"}})
|
||||
result = scheduler.Results()[key]
|
||||
|
||||
if deducted == "" {
|
||||
t.Fatalf("缺少积分扣减确认;实际日志:\n%s", dumpMessages(store))
|
||||
}
|
||||
if !strings.Contains(deducted, "抢购前=1000") || !strings.Contains(deducted, "抢购后=800") || !strings.Contains(deducted, "扣减=200") {
|
||||
t.Fatalf("扣分确认信息不完整: %q", deducted)
|
||||
}
|
||||
if !strings.Contains(done, "1/1") {
|
||||
t.Fatalf("核对汇总 = %q, want 1/1", done)
|
||||
}
|
||||
if !strings.HasSuffix(result, "|积分-200") {
|
||||
t.Fatalf("结果集应附带扣分信息, got %q", result)
|
||||
}
|
||||
if got := atomic.LoadInt32(&scoreCalls); got != 1 {
|
||||
t.Fatalf("积分查询次数 = %d, want 1(只在抢购结束后核对一次)", got)
|
||||
}
|
||||
// 最新余额要存回基线,下一次抢购才能算对差值
|
||||
if after, ok, _ := scheduler.ScoreBaseline("a"); !ok || after != 800 {
|
||||
t.Fatalf("核对后应把 800 存回基线, got %d ok=%v", after, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestVerifyRunResultsWithoutBaseline 没有基线时先记录余额,不误报"未扣分"。
|
||||
func TestVerifyRunResultsWithoutBaseline(t *testing.T) {
|
||||
speedUpPeace(t)
|
||||
seckillMaxDuration = 1200 * time.Millisecond
|
||||
|
||||
scheduler := newOrderTestScheduler(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/create":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"orderId":"order-1","expireSeconds":300}}`))
|
||||
case "/pay":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"exchangeId":9,"exchangeNum":"1"}}`))
|
||||
case "/bind":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`))
|
||||
case "/score":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"balance":1234}}`))
|
||||
}
|
||||
})
|
||||
scheduler.paths = writeAccountFile(t, "a")
|
||||
scheduler.accounts = NewAccountStore(scheduler.paths.AccountFile)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{CommodityID: "goods-1", CommodityName: "测试商品"}})
|
||||
|
||||
store := logger.NewMemoryLogStore(4000)
|
||||
cleanup := logger.InitLogging(store, t.TempDir(), "测试实例", "/tmp/douyu-go-test")
|
||||
defer cleanup()
|
||||
|
||||
scheduler.RunNow(map[string]string{"a": "goods-1"})
|
||||
|
||||
var recorded, confirmed string
|
||||
for _, entry := range store.Since(0) {
|
||||
if strings.Contains(entry.Message, "还没有基线") {
|
||||
recorded = entry.Message
|
||||
}
|
||||
if strings.Contains(entry.Message, "确认扣分") {
|
||||
confirmed = entry.Message
|
||||
}
|
||||
}
|
||||
if recorded == "" {
|
||||
t.Fatalf("缺基线时应记录余额而不是误报未扣分;实际日志:\n%s", dumpMessages(store))
|
||||
}
|
||||
if !strings.Contains(confirmed, "0/1") {
|
||||
t.Fatalf("无基线时不应算作确认扣分: %q", confirmed)
|
||||
}
|
||||
if score, ok, _ := scheduler.ScoreBaseline("a"); !ok || score != 1234 {
|
||||
t.Fatalf("应把查到的余额记成基线, got %d ok=%v", score, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreBaselineStorePersists 基线要落盘,重启后仍能用来比对。
|
||||
func TestScoreBaselineStorePersists(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), scoreBaselineFileName)
|
||||
store := NewScoreBaselineStore(path)
|
||||
store.Set("a", 1000)
|
||||
store.SetMany(map[string]int{"b": 2000, "c": 3000})
|
||||
|
||||
reloaded := NewScoreBaselineStore(path)
|
||||
reloaded.Load()
|
||||
for name, want := range map[string]int{"a": 1000, "b": 2000, "c": 3000} {
|
||||
got, ok, at := reloaded.Get(name)
|
||||
if !ok || got != want {
|
||||
t.Fatalf("%s 基线 = %d ok=%v, want %d", name, got, ok, want)
|
||||
}
|
||||
if at.IsZero() {
|
||||
t.Fatalf("%s 基线时间缺失", name)
|
||||
}
|
||||
}
|
||||
reloaded.Forget([]string{"b"})
|
||||
if _, ok, _ := reloaded.Get("b"); ok {
|
||||
t.Fatal("Forget 后不应还有 b 的基线")
|
||||
}
|
||||
if names := reloaded.Names(); len(names) != 2 || names[0] != "a" || names[1] != "c" {
|
||||
t.Fatalf("Names = %v, want [a c]", names)
|
||||
}
|
||||
}
|
||||
|
||||
// TestVerifyRunResultsIgnoresScoreFailure 积分核对失败不能影响"抢到"这个结论。
|
||||
func TestVerifyRunResultsIgnoresScoreFailure(t *testing.T) {
|
||||
speedUpPeace(t)
|
||||
seckillMaxDuration = 1200 * time.Millisecond
|
||||
|
||||
scheduler := newOrderTestScheduler(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/create":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"orderId":"order-1","expireSeconds":300}}`))
|
||||
case "/pay":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"exchangeId":9,"exchangeNum":"1"}}`))
|
||||
case "/bind":
|
||||
_, _ = w.Write([]byte(`{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`))
|
||||
case "/score":
|
||||
_, _ = w.Write([]byte(`{"error":9,"msg":"积分接口炸了"}`))
|
||||
}
|
||||
})
|
||||
scheduler.paths = writeAccountFile(t, "a")
|
||||
scheduler.accounts = NewAccountStore(scheduler.paths.AccountFile)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{CommodityID: "goods-1", CommodityName: "测试商品"}})
|
||||
|
||||
store := logger.NewMemoryLogStore(4000)
|
||||
cleanup := logger.InitLogging(store, t.TempDir(), "测试实例", "/tmp/douyu-go-test")
|
||||
defer cleanup()
|
||||
|
||||
scheduler.RunNow(map[string]string{"a": "goods-1"})
|
||||
|
||||
var summarize, warn string
|
||||
for _, entry := range store.Since(0) {
|
||||
if strings.Contains(entry.Message, "抢购任务结束: 成功=1/1") {
|
||||
summarize = entry.Message
|
||||
}
|
||||
if strings.Contains(entry.Message, "积分核对失败") {
|
||||
warn = entry.Message
|
||||
}
|
||||
}
|
||||
if summarize == "" {
|
||||
t.Fatalf("核对失败不应影响成功结论;实际日志:\n%s", dumpMessages(store))
|
||||
}
|
||||
if warn == "" {
|
||||
t.Fatalf("积分核对失败应记警告;实际日志:\n%s", dumpMessages(store))
|
||||
}
|
||||
}
|
||||
|
||||
// TestFetchScoreBaselinesRecordsValues 导入账号时自动记一次积分作为基线:
|
||||
// 这批请求刻意放在抢购之外,账号多也不会挤占锁单。
|
||||
func TestFetchScoreBaselinesRecordsValues(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
balances := map[string]int{"a": 1111, "b": 2222}
|
||||
scheduler := newOrderTestScheduler(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.URL.Path != "/score" {
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Errorf("ParseForm: %v", err)
|
||||
}
|
||||
// 用 Cookie 里的账号名区分(测试账号的 Cookie 就是 cvl_csrf_token=<name>)
|
||||
name := ""
|
||||
if c, err := r.Cookie("cvl_csrf_token"); err == nil {
|
||||
name = c.Value
|
||||
}
|
||||
mu.Lock()
|
||||
balance := balances[name]
|
||||
mu.Unlock()
|
||||
_, _ = w.Write([]byte(fmt.Sprintf(`{"error":0,"data":{"balance":%d}}`, balance)))
|
||||
})
|
||||
|
||||
scheduler.FetchScoreBaselines([]types.Account{
|
||||
{Name: "a", Cookie: "cvl_csrf_token=a"},
|
||||
{Name: "b", Cookie: "cvl_csrf_token=b"},
|
||||
{Name: "empty", Cookie: ""},
|
||||
})
|
||||
|
||||
if got, ok, _ := scheduler.ScoreBaseline("a"); !ok || got != 1111 {
|
||||
t.Fatalf("a 基线 = %d ok=%v, want 1111", got, ok)
|
||||
}
|
||||
if got, ok, _ := scheduler.ScoreBaseline("b"); !ok || got != 2222 {
|
||||
t.Fatalf("b 基线 = %d ok=%v, want 2222", got, ok)
|
||||
}
|
||||
if _, ok, _ := scheduler.ScoreBaseline("empty"); ok {
|
||||
t.Fatal("空 Cookie 的账号不应有基线")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user