76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package seckill
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"douyu_go/internal/activity"
|
|
"douyu_go/internal/client"
|
|
"douyu_go/internal/trace"
|
|
"douyu_go/internal/types"
|
|
)
|
|
|
|
func TestWarmupCachesCSRFBeforeRoleLookupCompletes(t *testing.T) {
|
|
roleStarted := make(chan struct{}, 1)
|
|
roleRelease := make(chan struct{})
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/bind" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
select {
|
|
case roleStarted <- struct{}{}:
|
|
default:
|
|
}
|
|
<-roleRelease
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`)
|
|
}))
|
|
defer server.Close()
|
|
|
|
act := *activity.Get("peace")
|
|
act.BindInfoURL = server.URL + "/bind"
|
|
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
|
scheduler := New(base, types.AppPaths{}, nil, 1, 1, nil, types.SeckillModeAggressive)
|
|
attempt := types.ExchangeAttempt{
|
|
Account: types.Account{Name: "a", Cookie: "cvl_csrf_token=cached-token"},
|
|
Product: types.Product{CommodityID: "goods-1", CommodityName: "测试商品"},
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
scheduler.warmupAttempts([]types.ExchangeAttempt{attempt})
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-roleStarted:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("role lookup did not start")
|
|
}
|
|
|
|
warmup, ok := scheduler.warmupFor(attempt)
|
|
if !ok {
|
|
t.Fatal("CSRF should be cached before role lookup completes")
|
|
}
|
|
if warmup.CSRF != "cached-token" {
|
|
t.Fatalf("cached CSRF=%q, want cached-token", warmup.CSRF)
|
|
}
|
|
if warmup.RoleName != "" {
|
|
t.Fatalf("role name should not be cached before role lookup completes: %q", warmup.RoleName)
|
|
}
|
|
|
|
close(roleRelease)
|
|
select {
|
|
case <-done:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("warmup did not finish after role lookup")
|
|
}
|
|
warmup, ok = scheduler.warmupFor(attempt)
|
|
if !ok || warmup.RoleName != "测试角色" {
|
|
t.Fatalf("warmup role was not updated: %+v", warmup)
|
|
}
|
|
}
|