166 lines
5.3 KiB
Go
166 lines
5.3 KiB
Go
package seckill
|
|
|
|
import (
|
|
"errors"
|
|
"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 TestProxyStandbyCount(t *testing.T) {
|
|
cases := []struct {
|
|
active int
|
|
percent int
|
|
want int
|
|
}{
|
|
{active: 1, percent: 25, want: 1},
|
|
{active: 6, percent: 25, want: 2},
|
|
{active: 10, percent: 25, want: 3},
|
|
{active: 6, percent: 1, want: 1},
|
|
}
|
|
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 TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
|
|
scheduler := &Scheduler{
|
|
client: &client.DouyuClient{},
|
|
accountClients: make(map[string]*client.DouyuClient),
|
|
}
|
|
groupClient := &client.DouyuClient{}
|
|
scheduler.replaceAccountClients(preparedProxyPool{
|
|
accountClients: map[string]*client.DouyuClient{"a": groupClient},
|
|
activeClients: []*client.DouyuClient{groupClient},
|
|
expiresAt: 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, 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")
|
|
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: 25,
|
|
})
|
|
|
|
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) != 1 {
|
|
t.Fatalf("备用代理=%d, want 1", len(scheduler.proxyStandbys))
|
|
}
|
|
|
|
standbyClient := scheduler.proxyStandbys[0].client
|
|
scheduler.UpdateProxySettings(types.Config{
|
|
ProxyEnabled: true,
|
|
ProxyURL: "http://127.0.0.1:1",
|
|
AccountsPerProxy: 2,
|
|
ProxySparePercent: 25,
|
|
})
|
|
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 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
|
|
}
|