package seckill import ( "errors" "fmt" "net/http" "net/http/httptest" "strings" "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 act.BatchLimitURL = timeAPIURL + "/batchExchangeLimit" base := client.New(&act, timeAPIURL, &trace.Recorder{}, "") paths := writeAccountFile(t, "a", "b", "c", "d", "e") scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.LoadAccounts() scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: 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 TestActivityHotSwitchesProxyButClickTooFastDoesNot(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{} standbyClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", activeIP: "1.1.1.1", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, standbys: []validatedProxy{{ url: "http://127.0.0.1:9100", client: standbyClient, ip: "2.2.2.2", }}, allClients: []*client.DouyuClient{groupClient, standbyClient}, standbyTarget: 1, }) scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{ Kind: "锁定兑换商品失败", Code: apiClickTooFast, Msg: "您点击太快,请稍等再试", }) if scheduler.clientForAccount("a") != groupClient { t.Fatal("click-too-fast should not switch the proxy") } scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{ Kind: "锁定兑换商品失败", Code: apiActivityHot, Msg: "活动火爆,请稍后再试", }) if scheduler.clientForAccount("a") != standbyClient { t.Fatal("activity-hot should immediately switch to standby") } } func TestProxyRiskBusinessErrorMatchesCodeOrMessage(t *testing.T) { cases := []struct { name string err error want bool }{ { name: "code only", err: &client.OrderError{Code: apiActivityHot}, want: true, }, { name: "message only", err: errors.New("锁定兑换商品失败: 活动火爆,请稍后再试"), want: true, }, { name: "numeric code only", err: errors.New("接口失败 error=1644400007"), want: true, }, { name: "click too fast", err: &client.OrderError{Code: apiClickTooFast, Msg: "您点击太快,请稍等再试"}, want: false, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := isProxyRiskBusinessError(tc.err); got != tc.want { t.Fatalf("isProxyRiskBusinessError(%v)=%v, want %v", tc.err, got, tc.want) } }) } } func TestDirectActivityHotDoesNotSwitchProxy(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, }) directClient := &client.DouyuClient{} standbyClient := &client.DouyuClient{} group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"direct"}, activeURL: "http://127.0.0.1:9000", activeIP: "1.1.1.1", } scheduler.directClient = directClient scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"direct": directClient}, activeClients: []*client.DouyuClient{directClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{directClient: group}, standbys: []validatedProxy{{ url: "http://127.0.0.1:9100", client: standbyClient, ip: "2.2.2.2", }}, allClients: []*client.DouyuClient{directClient, standbyClient}, standbyTarget: 1, }) scheduler.handleProxyBusinessRisk("direct", directClient, &client.OrderError{ Kind: "锁定兑换商品失败", Code: apiActivityHot, Msg: "活动火爆,请稍后再试", }) if scheduler.clientForAccount("direct") != directClient { t.Fatal("direct activity-hot must remain direct") } scheduler.clientMu.RLock() standbyCount := len(scheduler.proxyStandbys) scheduler.clientMu.RUnlock() if standbyCount != 1 { t.Fatalf("direct activity-hot consumed standby proxy: remaining=%d", standbyCount) } } func TestActivityHotSwitchesAllGroupsSharingEgressIP(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, }) g1, g2 := &client.DouyuClient{}, &client.DouyuClient{} s1, s2 := &client.DouyuClient{}, &client.DouyuClient{} group1 := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1"} group2 := &proxyRuntimeGroup{groupIndex: 2, accountNames: []string{"b"}, activeURL: "http://127.0.0.1:9002", activeIP: "1.1.1.1"} scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": g1, "b": g2}, activeClients: []*client.DouyuClient{g1, g2}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{ g1: group1, g2: group2, }, standbys: []validatedProxy{ {url: "http://127.0.0.1:9101", client: s1, ip: "2.2.2.2"}, {url: "http://127.0.0.1:9102", client: s2, ip: "3.3.3.3"}, }, allClients: []*client.DouyuClient{g1, g2, s1, s2}, standbyTarget: 2, }) scheduler.handleProxyBusinessRisk("a", g1, &client.OrderError{ Kind: "锁定兑换商品失败", Code: apiActivityHot, Msg: "活动火爆,请稍后再试", }) if scheduler.clientForAccount("a") == g1 || scheduler.clientForAccount("b") == g2 { t.Fatal("all groups sharing the risky egress IP should switch") } } func TestProxyPreflightSwitchesOnActivityHot(t *testing.T) { var requests int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if atomic.AddInt32(&requests, 1) == 1 { fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`) return } fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`) })) defer server.Close() act := *activity.Get("peace") act.BaseURL = server.URL act.BatchLimitURL = server.URL + "/limit" base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") paths := writeAccountFile(t, "a") scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.LoadAccounts() scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, AccountsPerProxy: 5, ProxySparePercent: 50, }) proxyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") standbyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") group := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", activeIP: "1.1.1.1"} scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": proxyClient}, activeClients: []*client.DouyuClient{proxyClient}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{proxyClient: group}, standbys: []validatedProxy{{ url: "http://127.0.0.1:9100", client: standbyClient, ip: "2.2.2.2", }}, allClients: []*client.DouyuClient{proxyClient, standbyClient}, standbyTarget: 1, }) if err := scheduler.preflightProxyBusinessRiskUntil( map[string]string{"a": "goods-1"}, time.Now().Add(3*time.Second), ); err != nil { t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err) } if scheduler.clientForAccount("a") != standbyClient { t.Fatal("preflight activity-hot should switch to a different-IP standby") } if !scheduler.proxyBusinessRiskVerifiedSnapshot() { t.Fatal("switched proxy should be rechecked and marked verified") } if got := atomic.LoadInt32(&requests); got != 2 { t.Fatalf("batch limit requests=%d, want 2", got) } } func TestProxyPreflightRechecksEveryGroupAfterSwitch(t *testing.T) { var requests int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if atomic.AddInt32(&requests, 1) <= 2 { fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`) return } fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`) })) defer server.Close() act := *activity.Get("peace") act.BaseURL = server.URL act.BatchLimitURL = server.URL + "/limit" base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") paths := writeAccountFile(t, "a", "b") scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.LoadAccounts() scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, AccountsPerProxy: 1, ProxySparePercent: 50, }) active1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") active2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") standby1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") standby2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "") group1 := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1", } group2 := &proxyRuntimeGroup{ groupIndex: 2, accountNames: []string{"b"}, activeURL: "http://127.0.0.1:9002", activeIP: "2.2.2.2", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": active1, "b": active2}, activeClients: []*client.DouyuClient{active1, active2}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{ active1: group1, active2: group2, }, standbys: []validatedProxy{ {url: "http://127.0.0.1:9101", client: standby1, ip: "3.3.3.3"}, {url: "http://127.0.0.1:9102", client: standby2, ip: "4.4.4.4"}, }, allClients: []*client.DouyuClient{active1, active2, standby1, standby2}, standbyTarget: 2, }) if err := scheduler.preflightProxyBusinessRiskUntil( map[string]string{"a": "goods-1", "b": "goods-1"}, time.Now().Add(3*time.Second), ); err != nil { t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err) } if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("b") == active2 { t.Fatal("both activity-hot groups should switch before startup") } if !scheduler.proxyBusinessRiskVerifiedSnapshot() { t.Fatal("both replacement groups should be rechecked and verified") } if got := atomic.LoadInt32(&requests); got != 4 { t.Fatalf("batch limit requests=%d, want 4", got) } } func TestProxyPreflightExtractsReplacementUntilHealthy(t *testing.T) { safeProxyServers := make([]*httptest.Server, 0, 2) for i := 0; i < 2; i++ { safeProxyServers = append(safeProxyServers, httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if strings.Contains(r.URL.Path, "batchExchangeLimit") { fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`) return } fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`) }, ))) } defer func() { for _, server := range safeProxyServers { server.Close() } }() hotProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if strings.Contains(r.URL.Path, "batchExchangeLimit") { fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`) return } fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`) })) defer hotProxy.Close() var apiCalls int32 apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { index := int(atomic.AddInt32(&apiCalls, 1)-1) % len(safeProxyServers) fmt.Fprint(w, safeProxyServers[index].URL) })) defer apiServer.Close() act := *activity.Get("peace") act.StoreHomeURL = "http://douyu.test/product" act.BatchLimitURL = "http://douyu.test/batchExchangeLimit" base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") paths := writeAccountFile(t, "a") scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.LoadAccounts() scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, ProxyRequired: true, ProxyAPI: apiServer.URL, AccountsPerProxy: 1, ProxySparePercent: 50, }) active1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL) if err != nil { t.Fatal(err) } standby1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL) if err != nil { t.Fatal(err) } group := &proxyRuntimeGroup{ groupIndex: 1, accountNames: []string{"a"}, activeURL: hotProxy.URL, activeIP: "1.1.1.1", } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": active1}, activeClients: []*client.DouyuClient{active1}, groups: map[*client.DouyuClient]*proxyRuntimeGroup{active1: group}, standbys: []validatedProxy{{ url: hotProxy.URL, client: standby1, ip: "2.2.2.2", }}, allClients: []*client.DouyuClient{active1, standby1}, standbyTarget: 1, }) if err := scheduler.preflightProxyBusinessRiskUntil( map[string]string{"a": "goods-1"}, time.Now().Add(10*time.Second), ); err != nil { t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err) } if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("a") == standby1 { t.Fatal("preflight should keep replacing activity-hot proxies until a healthy proxy is active") } if !scheduler.proxyBusinessRiskVerifiedSnapshot() { t.Fatal("replacement proxy should be verified before startup") } if got := atomic.LoadInt32(&apiCalls); got < 2 { t.Fatalf("proxy extraction calls=%d, want at least 2", got) } } 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, "") scheduler.setProxyBusinessRiskVerified(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 TestProxyRunGateRequiresBusinessRiskPreflight(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, }) scheduler.replaceAccountClients(preparedProxyPool{}) scheduler.setProxyReadiness(true, "") if ok, reason := scheduler.ProxyRunGate(); ok { t.Fatalf("unverified proxy pool should block startup, reason=%q", reason) } scheduler.setProxyBusinessRiskVerified(true) if ok, reason := scheduler.ProxyRunGate(); !ok { t.Fatalf("verified proxy pool should pass startup gate: %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") if strings.Contains(r.URL.Path, "batchExchangeLimit") { fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`) return } fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品"}]}}`) })) t.Cleanup(server.Close) return server }