package seckill import ( "errors" "fmt" "net/http" "net/http/httptest" "sync/atomic" "testing" "time" "douyu_go/internal/activity" "douyu_go/internal/client" "douyu_go/internal/trace" "douyu_go/internal/types" ) func TestChunkAccountNames(t *testing.T) { names := []string{"k", "a", "i", "c", "b", "h", "e", "d", "g", "f", "j"} groups := chunkAccountNames(names, 5) if len(groups) != 3 { t.Fatalf("groups=%d, want 3", len(groups)) } if len(groups[0]) != 5 || len(groups[1]) != 5 || len(groups[2]) != 1 { t.Fatalf("unexpected group sizes: %d,%d,%d", len(groups[0]), len(groups[1]), len(groups[2])) } } func TestSplitProxyURLs(t *testing.T) { got := splitProxyURLs("http://a:1\nhttp://b:2; http://c:3,http://d:4") if len(got) != 4 { t.Fatalf("splitProxyURLs()=%v", got) } } func TestProxyPoolExpiresAtUsesMinuteParameter(t *testing.T) { now := time.Date(2026, 11, 19, 12, 0, 0, 0, time.Local) got := proxyPoolExpiresAt("https://example.test/extract?minute=10", now) if want := now.Add(10 * time.Minute); !got.Equal(want) { t.Fatalf("expiresAt=%s, want %s", got, want) } } func TestProxyStandbyCount(t *testing.T) { cases := []struct { active int percent int want int }{ {active: 1, percent: 50, want: 1}, {active: 6, percent: 50, want: 3}, {active: 10, percent: 50, want: 5}, {active: 6, percent: 1, want: 3}, } for _, tc := range cases { if got := proxyStandbyCount(tc.active, tc.percent); got != tc.want { t.Errorf("proxyStandbyCount(%d, %d)=%d, want %d", tc.active, tc.percent, got, tc.want) } } } func TestShouldPrepareProxyOnSchedule(t *testing.T) { cases := []struct { name string cfg proxySettingsSnapshot want bool }{ {name: "disabled", cfg: proxySettingsSnapshot{required: true}, want: false}, {name: "strict static", cfg: proxySettingsSnapshot{enabled: true, required: true}, want: true}, {name: "auto fetch api", cfg: proxySettingsSnapshot{enabled: true, autoFetchOnTimer: true, proxyAPI: "https://example.test"}, want: true}, {name: "optional api without auto fetch", cfg: proxySettingsSnapshot{enabled: true, proxyAPI: "https://example.test"}, want: false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := shouldPrepareProxyOnSchedule(tc.cfg); got != tc.want { t.Fatalf("shouldPrepareProxyOnSchedule()=%v, want %v", got, tc.want) } }) } } func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) { scheduler := &Scheduler{ client: &client.DouyuClient{}, accountClients: make(map[string]*client.DouyuClient), } groupClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, expiresAt: time.Now().Add(time.Minute), standbyTarget: 5, }) if !scheduler.accountClientsCover([]string{"a"}) { t.Fatal("备用不足不应使已覆盖账号的活动代理失效") } group.failed = true if scheduler.accountClientsCover([]string{"a"}) { t.Fatal("失败代理组不应被判定为可复用") } group.failed = false scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, AutoFetchProxyOnSchedule: true, AccountsPerProxy: 5, }) if scheduler.accountClientsCover([]string{"a"}) { t.Fatal("配置变更后代理组应标记为需要刷新") } } func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) { timeAPIURL := "http://douyu.test/time" proxyServers := make([]*httptest.Server, 0, 5) for i := 0; i < 5; i++ { proxyServers = append(proxyServers, newTimeProxyServer(t)) } var next int32 apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { index := int(atomic.AddInt32(&next, 1)-1) % len(proxyServers) fmt.Fprint(w, proxyServers[index].URL) })) defer apiServer.Close() act := *activity.Get("peace") act.StoreHomeURL = timeAPIURL base := client.New(&act, timeAPIURL, &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyAPI: apiServer.URL, AutoFetchProxyOnSchedule: true, AccountsPerProxy: 2, ProxySparePercent: 50, }) accountProducts := make(map[string]string) for _, name := range []string{"a", "b", "c", "d", "e"} { accountProducts[name] = "goods-1" } if err := scheduler.prepareProxyGroups(accountProducts, true); err != nil { t.Fatalf("prepareProxyGroups() error = %v", err) } if scheduler.clientForAccount("a") != scheduler.clientForAccount("b") { t.Fatal("a 和 b 应使用同一个代理客户端") } if scheduler.clientForAccount("b") == scheduler.clientForAccount("c") { t.Fatal("b 和 c 不应使用同一个代理客户端") } if scheduler.clientForAccount("c") != scheduler.clientForAccount("d") { t.Fatal("c 和 d 应使用同一个代理客户端") } if scheduler.clientForAccount("e") == scheduler.clientForAccount("a") { t.Fatal("e 应使用独立代理客户端") } if len(scheduler.proxyStandbys) != 2 { t.Fatalf("备用代理=%d, want 2", len(scheduler.proxyStandbys)) } standbyClient := scheduler.proxyStandbys[0].client scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyURL: "http://127.0.0.1:1", AccountsPerProxy: 2, ProxySparePercent: 50, }) proxyErr := errors.New("read tcp: connection reset by peer") scheduler.recordProxyResult(scheduler.clientForAccount("a"), proxyErr) if scheduler.clientForAccount("a") == standbyClient { t.Fatal("单次网络异常不应立即切换备用代理") } scheduler.recordProxyResult(scheduler.clientForAccount("a"), proxyErr) if scheduler.clientForAccount("a") != standbyClient || scheduler.clientForAccount("b") != standbyClient { t.Fatal("连续网络异常后整组账号应切换到备用代理") } if scheduler.clientForAccount("c") == standbyClient { t.Fatal("其他代理组不应被错误切换") } } func TestPrepareProxyGroupsFallsBackDirectAndSkipsRepeatedWait(t *testing.T) { var requests int32 apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&requests, 1) w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `{"code":-1,"status":200,"message":"secret密匙错误"}`) })) defer apiServer.Close() act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyAPI: apiServer.URL, AccountsPerProxy: 5, ProxySparePercent: 50, }) accountProducts := map[string]string{"a": "goods-1"} if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil { t.Fatalf("prepareProxyGroups() error = %v", err) } if !scheduler.proxyFallbackSnapshot() { t.Fatal("代理失败后应启用直连回退") } if scheduler.clientForAccount("a") != scheduler.directClient { t.Fatal("代理失败后账号应路由到真正的直连客户端") } firstRequests := atomic.LoadInt32(&requests) if firstRequests == 0 { t.Fatal("首次代理准备应尝试提取") } if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil { t.Fatalf("second prepareProxyGroups() error = %v", err) } if got := atomic.LoadInt32(&requests); got != firstRequests { t.Fatalf("直连回退后不应重复等待提取,requests=%d want %d", got, firstRequests) } } func TestProxyGroupSwitchesDirectWhenStandbyUnavailable(t *testing.T) { act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) groupClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ accountNames: []string{"a"}, activeURL: "http://user:pass@127.0.0.1:9000", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, allClients: []*client.DouyuClient{groupClient}, }) proxyErr := errors.New("read tcp: connection reset by peer") scheduler.recordProxyResult(groupClient, proxyErr) scheduler.recordProxyResult(groupClient, proxyErr) if scheduler.clientForAccount("a") != scheduler.directClient { t.Fatal("备用代理为空时,故障组应立即切换到直连") } } func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) { act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, AccountsPerProxy: 5, ProxySparePercent: 50, }) groupClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://user:pass@127.0.0.1:9000", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, allClients: []*client.DouyuClient{groupClient}, }) scheduler.replaceProxyGroupHistory([][]string{{"a"}}, 1, []validatedProxy{{ url: group.activeURL, client: groupClient, }}) scheduler.setProxyReadiness(true, "") proxyErr := errors.New("read tcp: connection reset by peer") scheduler.recordProxyResult(groupClient, proxyErr) scheduler.recordProxyResult(groupClient, proxyErr) if scheduler.clientForAccount("a") != nil { t.Fatal("failed strict proxy group should pause account requests") } if !group.failed { t.Fatal("failed group should remain tracked for standby refill") } if !scheduler.accountProxyPaused("a") { t.Fatal("account should be reported as paused") } if ok, _ := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); ok { t.Fatal("run gate should reject a failed proxy group") } res := scheduler.runAccountAttempt(types.ExchangeAttempt{ Account: types.Account{Name: "a"}, Product: types.Product{CommodityName: "测试商品"}, }, types.SeckillModeAggressive, nil) if !res.paused || res.result.Message != "等待备用代理" { t.Fatalf("unexpected paused attempt result: %+v", res) } } func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) { act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, AccountsPerProxy: 2, ProxySparePercent: 50, }) groupClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a", "b"}, activeURL: "http://user:pass@127.0.0.1:9000", activeIP: "127.0.0.1", activeRegion: "test", activeRTT: 45 * time.Millisecond, } standbyClient := &client.DouyuClient{} scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient, "b": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, standbys: []validatedProxy{{client: standbyClient}}, allClients: []*client.DouyuClient{groupClient, standbyClient}, standbyTarget: 1, }) scheduler.replaceProxyGroupHistory([][]string{{"a", "b"}}, 1, []validatedProxy{{ url: group.activeURL, client: groupClient, ip: group.activeIP, region: group.activeRegion, rtt: group.activeRTT, }}) scheduler.setProxyReadiness(true, "") status := scheduler.ProxyStatus() if status.State != "ready" || status.ActiveCount != 1 || len(status.Groups) != 1 { t.Fatalf("unexpected status: %+v", status) } if len(status.Accounts) != 2 || status.Accounts[0].GroupLabel != "G1" || status.Accounts[0].Transport != "proxy" { t.Fatalf("unexpected account mapping: %+v", status.Accounts) } if status.Groups[0].Proxy == "" || status.Groups[0].Proxy == group.activeURL { t.Fatalf("proxy URL should be masked, got %q", status.Groups[0].Proxy) } } func TestProxyRunGateAllowsStandbyShortageWhenActiveGroupsCoverAccounts(t *testing.T) { act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, AccountsPerProxy: 5, ProxySparePercent: 50, }) groupClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, allClients: []*client.DouyuClient{groupClient}, standbyTarget: 5, expiresAt: time.Now().Add(time.Minute), }) scheduler.setProxyReadiness(true, "") if ok, reason := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); !ok { t.Fatalf("standby shortage should not block covered active groups: %s", reason) } } func TestStandbyHealthProbeRemovesRepeatedlyFailingProxy(t *testing.T) { act := *activity.Get("peace") act.StoreHomeURL = "http://douyu.test/time" base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") badClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1") if err != nil { t.Fatal(err) } scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.proxyStandbys = []validatedProxy{{ url: "http://127.0.0.1:1", client: badClient, }} scheduler.proxyClientPool = []*client.DouyuClient{badClient} scheduler.proxyStandbyTarget = 1 for attempt := 0; attempt < proxyStandbyFailLimit; attempt++ { if !scheduler.probeProxyStandbys(scheduler.proxyPoolGeneration) { t.Fatal("standby probe should keep monitoring current generation") } } if len(scheduler.proxyStandbys) != 0 { t.Fatalf("standby count=%d, want 0", len(scheduler.proxyStandbys)) } if len(scheduler.proxyClientPool) != 0 { t.Fatalf("client pool count=%d, want 0", len(scheduler.proxyClientPool)) } } func TestStrictPrepareFailureBlocksRun(t *testing.T) { apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `{"code":-1,"status":200,"message":"secret密匙错误"}`) })) defer apiServer.Close() act := *activity.Get("peace") base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, ProxyAPI: apiServer.URL, AccountsPerProxy: 5, ProxySparePercent: 50, }) err := scheduler.prepareProxyGroupsUntil(map[string]string{"a": "goods-1"}, false, time.Now().Add(2*time.Second)) if err == nil { t.Fatal("strict proxy preparation should fail") } if scheduler.proxyFallbackSnapshot() { t.Fatal("strict mode must not activate direct fallback") } if ok, _ := scheduler.ProxyRunGate(); ok { t.Fatal("strict mode run gate should remain closed") } if status := scheduler.ProxyStatus(); status.State != "failed" { t.Fatalf("proxy status=%q, want failed", status.State) } } func newTimeProxyServer(t *testing.T) *httptest.Server { t.Helper() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品"}]}}`) })) t.Cleanup(server.Close) return server }