Files
dygou_go/internal/seckill/proxy_groups_test.go
T

123 lines
3.8 KiB
Go

package seckill
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"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 TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
scheduler := &Scheduler{
client: &client.DouyuClient{},
accountClients: make(map[string]*client.DouyuClient),
}
groupClient := &client.DouyuClient{}
scheduler.replaceAccountClients(
map[string]*client.DouyuClient{"a": groupClient},
[]*client.DouyuClient{groupClient},
time.Now().Add(time.Minute),
)
if !scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("未过期代理组应可复用")
}
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, 3)
for i := 0; i < 3; 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")
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,
})
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 应使用独立代理客户端")
}
}
func newTimeProxyServer(t *testing.T) *httptest.Server {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := strconv.FormatInt(time.Now().Unix(), 10)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"error":0,"data":{"preExchange":{"currentTime":%q}}}`, now)
}))
t.Cleanup(server.Close)
return server
}