Files
sub2api/backend/internal/handler/single_account_retry_probe_unit_test.go
T
feeeei d5dbb5c36b perf(gateway): 单 antigravity 账号分组判定改为按需求值
三个入口(Gemini 原生、/v1/messages 的 Gemini 分支与 Claude 路径)
每个请求都会查询分组的 antigravity:forced 快照桶。纯 Gemini/Anthropic
分组的该桶恒为空,空快照按未命中处理,于是每个请求都回源 PG 并重新
发布快照,而判定结果只被 antigravity 账号的 503 原地重试与模型限流
预检查读取。

入口改为挂载延迟判定:antigravity 账号首次需要时才查询快照,同一请求
内只查一次;显式设置的 SingleAccountRetry 标记仍优先。其他平台的请求
不再触发该查询。
2026-10-20 23:49:34 +08:00

121 lines
4.2 KiB
Go

//go:build unit
package handler
import (
"bytes"
"context"
"net/http/httptest"
"sync"
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
middleware "github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
// bucketRecordingSchedulerCache 记录读取过的快照桶平台。
type bucketRecordingSchedulerCache struct {
*fakeSchedulerCache
mu sync.Mutex
platforms []string
}
func (c *bucketRecordingSchedulerCache) GetSnapshot(ctx context.Context, bucket service.SchedulerBucket) ([]*service.Account, bool, error) {
c.mu.Lock()
c.platforms = append(c.platforms, bucket.Platform)
c.mu.Unlock()
return c.fakeSchedulerCache.GetSnapshot(ctx, bucket)
}
func (c *bucketRecordingSchedulerCache) readPlatforms() []string {
c.mu.Lock()
defer c.mu.Unlock()
return append([]string(nil), c.platforms...)
}
// 非 antigravity 账号的请求不读取单 antigravity 账号分组判定,选号之外不查询 antigravity 快照桶。
func TestGeminiGroupRequestsDoNotReadAntigravityBucket(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
route string
path string
body string
call func(h *GatewayHandler) func(*gin.Context)
}{
{
name: "gemini native",
route: "/v1beta/models/*modelAction",
path: "/v1beta/models/gemini-2.5-flash:generateContent",
body: `{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`,
call: func(h *GatewayHandler) func(*gin.Context) { return h.GeminiV1BetaModels },
},
{
name: "messages on gemini group",
route: "/v1/messages",
path: "/v1/messages",
body: `{"model":"gemini-2.5-flash","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`,
call: func(h *GatewayHandler) func(*gin.Context) { return h.Messages },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := newGeminiClientCancelFixture(t)
f.serve(t, tt.route, tt.path, tt.body, tt.call(f.handler))
require.Equal(t, int32(1), f.upstream.calls.Load(), "请求应完成选号并到达上游")
platforms := f.schedulerCache.readPlatforms()
require.NotEmpty(t, platforms)
require.NotContains(t, platforms, service.PlatformAntigravity)
})
}
}
func TestMessagesAnthropicGroupDoesNotReadAntigravityBucket(t *testing.T) {
gin.SetMode(gin.TestMode)
groupID := int64(9400)
accountID := int64(9401)
group := &service.Group{ID: groupID, Hydrated: true, Platform: service.PlatformAnthropic, Status: service.StatusActive}
account := &service.Account{
ID: accountID,
Platform: service.PlatformAnthropic,
Type: service.AccountTypeAPIKey,
Credentials: map[string]any{"api_key": "test-key", "intercept_warmup_requests": true},
Status: service.StatusActive,
Schedulable: true,
Concurrency: 1,
Priority: 1,
AccountGroups: []service.AccountGroup{{AccountID: accountID, GroupID: groupID}},
}
schedulerCache := &bucketRecordingSchedulerCache{fakeSchedulerCache: &fakeSchedulerCache{accounts: []*service.Account{account}}}
h, cleanup := newTestGatewayHandlerWithSchedulerCache(t, group, schedulerCache)
t.Cleanup(cleanup)
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
body := `{"model":"claude-sonnet-4-5","max_tokens":256,"messages":[{"role":"user","content":[{"type":"text","text":"Warmup"}]}]}`
req := httptest.NewRequest("POST", "/v1/messages", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
c.Request = req.WithContext(context.WithValue(req.Context(), ctxkey.Group, group))
apiKey := &service.APIKey{
ID: 9402, UserID: 9403, GroupID: &groupID, Group: group, Status: service.StatusActive,
User: &service.User{ID: 9403, Concurrency: 10, Balance: 100},
}
c.Set(string(middleware.ContextKeyAPIKey), apiKey)
c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{UserID: apiKey.UserID, Concurrency: 10})
h.Messages(c)
require.Equal(t, 200, rec.Code)
selected, ok := c.Get(opsAccountIDKey)
require.True(t, ok)
require.Equal(t, accountID, selected)
platforms := schedulerCache.readPlatforms()
require.NotEmpty(t, platforms)
require.NotContains(t, platforms, service.PlatformAntigravity)
}