Files
magpie/internal/gateway/group_test.go
T
yetone 04e38e6045 gateway: a reorder moves only its own provider's conversations, and tests forget it (#6249, #6262)
b70563ec6 kept one process-wide "accounts reordered at" stamp. affine
dropped any conversation's stick older than that stamp, so reordering
Claude's accounts in the GUI cost a running Codex (or any other
provider's) conversation its account and its prompt cache on the next
turn. In tests, any earlier SetAccountOrder set the stamp, and later
tests that backdate a stick to check an hour's cache saw it dropped:
TestAffinityKeepsAnHourCachePastFiveMinutes,
TestChatGPTConversationStaysPastFiveMinutes and
TestClaudeConversationKeepsItsAccountsCache went red in package and
subset runs (#6249, #6262, one cause).

The stamp is now per provider (provider.NoteReordered(id),
ReorderedAt(id)), and affine compares the stick only with the
reorder of the provider whose account it holds. fresh(t) and the
tests that reorder call provider.ForgetReorders.

Run (mica, darwin):
- go test -run 'Codex|Draw|Image|Keep|Relay|Reorder|Affinity|ChatGPTConversation|ClaudeConversation|AccountOrder|AccountRank' ./internal/gateway:
  ok 253s; the same run at origin/main FAILs in the three tests above.
- go test ./internal/provider ok 128s; ./internal/cli ok 46s.
- go vet darwin (provider, gateway, cli, gui), GOOS=windows (same),
  GOOS=linux -tags nogui (provider, gateway, cli): ok. gofmt -l: clean.
- Broken by hand: the new case "another provider reordered keeps it"
  on the old affinity.go answers "k1 k2 k2, want k1 k1 k1"; green now.

Not tried: the full suite, the GUI suite, a reorder with real accounts.
Not changed: `magpie accounts order` (internal/cli) notes the reorder
in its own process, so a running gateway never sees it and keeps the
old stick until it expires; that was so before this change too.
2026-11-12 12:28:56 +08:00

540 lines
20 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/routes"
)
// keyed is a vendor that says which key each request came with, and
// answers as told for each.
type keyed struct {
tried []string
fail map[string]int // key → status to fail with
}
func (k *keyed) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
k.tried = append(k.tried, key)
w.Header().Set("Content-Type", "application/json")
if c := k.fail[key]; c != 0 {
w.WriteHeader(c)
io.WriteString(w, `{"error":{"message":"slow down"}}`)
return
}
io.WriteString(w, `{"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":"from `+key+`"},"finish_reason":"stop"}],`+
`"usage":{"prompt_tokens":3000,"completion_tokens":5,"prompt_tokens_details":{"cached_tokens":2500}}}`)
}
func fresh(t *testing.T) {
t.Helper()
home := t.TempDir() // no agent signed in
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home) // Windows finds the home there
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
restingUntil.Lock()
restingUntil.m = map[string]time.Time{}
restingUntil.Unlock()
codexHolds.Lock()
codexHolds.m = nil
codexHolds.Unlock()
sticks.Lock()
sticks.m = map[string]stick{}
sticks.Unlock()
provider.ForgetReorders()
t.Cleanup(provider.ForgetReorders)
refusedSeals.Lock()
refusedSeals.m = map[string]sealsRefused{}
refusedSeals.Unlock()
overflowLearned.Lock()
overflowLearned.m = map[string]map[string]refusedLong{}
overflowLearned.Unlock()
turnRules.Lock()
turnRules.m = map[string]turnRule{}
turnRules.Unlock()
classified.Lock()
classified.m, classified.failed = map[string]classifiedAs{}, map[string]classifyFailure{}
classified.Unlock()
levelled.Lock()
levelled.m = map[string]levelling{}
levelled.Unlock()
provider.ForgetCopilotForTest()
provider.ForgetRemoteCardsForTest()
provider.ForgetKeptCardsForTest()
provider.ForgetCodexClientsForTest()
remoteRefreshes.Lock()
remoteRefreshes.at = map[string]time.Time{}
remoteRefreshes.Unlock()
resetAuthThrottle()
forgetRouting()
}
// forgetRouting clears what routing keeps for the gateway's life — each
// provider's turn, each candidate's failures and tokens, each weighted
// round — so a test run again (-count) starts where it did the first time
// rather than a turn on, or with a failure already counted.
func forgetRouting() {
routed.Lock()
routed.used, routed.failures, routed.slow, routed.answered = map[string]tokenUse{}, map[string]int{}, map[string]slowRun{}, map[string]time.Time{}
routed.Unlock()
routes.Forget()
}
// setHome makes dir the home, where Windows (USERPROFILE) finds it too.
func setHome(t *testing.T, dir string) {
t.Helper()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
}
func serveOn(t *testing.T, id, key string, models []string, v http.Handler, keys ...string) {
t.Helper()
up := httptest.NewServer(v)
t.Cleanup(up.Close)
p := provider.Provider{ID: id, Name: strings.ToUpper(id), Key: key, Models: models, Chat: up.URL + "/v1"}
for _, k := range keys {
p.Keys = append(p.Keys, provider.KeyAccount{Key: k})
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
func postAs(t *testing.T, s *Server, session, body string) (int, string) {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
if session != "" {
req.Header.Set("x-session-id", session)
}
s.Handler().ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}
// openRouterFreeLimit answers one free model with an OpenRouter limit and its
// sibling successfully.
type openRouterFreeLimit struct {
tried []string
body string
}
func (f *openRouterFreeLimit) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
model := modelOf(body)
f.tried = append(f.tried, model)
w.Header().Set("Content-Type", "application/json")
if model == "x/a:free" {
w.WriteHeader(http.StatusTooManyRequests)
io.WriteString(w, f.body)
return
}
io.WriteString(w, `{"id":"ok","choices":[{"message":{"role":"assistant","content":"from b"}}]}`)
}
// A shared upstream pool limit on one OpenRouter free model rests it alone:
// the next group request starts with its sibling, even after that sibling's
// successful answer cleared the provider-level rest.
func TestOpenRouterSharedPoolRateLimitRestsOneModel(t *testing.T) {
fresh(t)
f := &openRouterFreeLimit{body: `{"error":{"message":"Provider returned error","code":429,"metadata":{"limit_source":"upstream_provider_shared_pool"}}}`}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{
ID: "orf", Name: "OpenRouter Free", Preset: "openrouter", Key: "k",
Models: []string{"x/a:free", "x/b:free"}, Chat: up.URL + "/v1",
}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{
Name: "Free", Members: []string{"orf/x/a:free", "orf/x/b:free"}, Routing: provider.Ordered,
}); err != nil {
t.Fatal(err)
}
s := New()
body := `{"model":"group/free","messages":[{"role":"user","content":"hi"}]}`
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "from b") {
t.Fatalf("group reply: %d %s", rec.Code, rec.Body.String())
}
if rec.Header().Get("X-Magpie-OpenRouter-Limit-Source") != "" {
t.Fatal("internal OpenRouter routing detail reached the client")
}
if first := s.trace.routes[len(s.trace.routes)-1].Tries[0]; first.Rest == nil || first.Rest.Key != "orf/x/a:free" {
t.Fatalf("shared-pool rest key: %+v", first)
}
if code, reply := postAs(t, s, "", body); code != http.StatusOK || !strings.Contains(reply, "from b") {
t.Fatalf("second group reply: %d %s", code, reply)
}
if got := strings.Join(f.tried, " "); got != "x/a:free x/b:free x/b:free" {
t.Fatalf("models tried %q, want a b b", got)
}
if !s.Unrest("orf/x/a:free") || s.resting("orf/x/a:free") {
t.Fatal("model rest was not lifted by its key")
}
}
// The routing hint is internal even when this is the only candidate, so the
// gateway passes the upstream error through to the client.
func TestOpenRouterSharedPoolDirectReplyHidesRoutingHeader(t *testing.T) {
fresh(t)
f := &openRouterFreeLimit{body: `{"error":{"message":"Provider returned error","code":429,"metadata":{"limit_source":"upstream_provider_shared_pool"}}}`}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{
ID: "orf", Name: "OpenRouter Free", Preset: "openrouter", Key: "k",
Models: []string{"x/a:free"}, Chat: up.URL + "/v1",
}); err != nil {
t.Fatal(err)
}
s := New()
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"orf/x/a:free","messages":[{"role":"user","content":"hi"}]}`)))
if rec.Code != http.StatusTooManyRequests {
t.Fatalf("direct reply: %d %s", rec.Code, rec.Body.String())
}
if got := rec.Header().Get("X-Magpie-OpenRouter-Limit-Source"); got != "" {
t.Fatalf("internal routing header leaked: %q", got)
}
}
// OpenRouter's free-models-per-min limit is account-wide. It must retain the
// provider rest key, rather than suggesting that another free model is ready.
func TestOpenRouterFreeAccountRateLimitKeepsProviderRest(t *testing.T) {
fresh(t)
f := &openRouterFreeLimit{body: `{"error":{"message":"Rate limit exceeded: free-models-per-min"}}`}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{
ID: "orf", Name: "OpenRouter Free", Preset: "openrouter", Key: "k",
Models: []string{"x/a:free", "x/b:free"}, Chat: up.URL + "/v1",
}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{
Name: "Free", Members: []string{"orf/x/a:free", "orf/x/b:free"}, Routing: provider.Ordered,
}); err != nil {
t.Fatal(err)
}
s := New()
if code, body := postAs(t, s, "", `{"model":"group/free","messages":[{"role":"user","content":"hi"}]}`); code != http.StatusOK || !strings.Contains(body, "from b") {
t.Fatalf("group reply: %d %s", code, body)
}
first := s.trace.routes[len(s.trace.routes)-1].Tries[0]
if first.Fail != failRate || first.Rest == nil || first.Rest.Key != "orf" {
t.Fatalf("account rate limit rest: %+v", first)
}
}
// A model two providers serve is a group magpie finds; a group the user
// makes routes over its members in the order it names them, the next one
// taking a request the one before can't.
func TestRoutingGroups(t *testing.T) {
fresh(t)
a, b := &keyed{}, &keyed{fail: map[string]int{"kb": 429}}
serveOn(t, "a", "ka", []string{"m", "only-a"}, a)
serveOn(t, "b", "kb", []string{"vendor/m"}, b)
var ids []string
for _, e := range provider.Catalog() {
if e.Provider.Account == nil { // signed-in agents the machine may have aside
ids = append(ids, e.ID)
}
}
if got := strings.Join(ids, " "); got != "auto/m a/m a/only-a b/vendor/m" {
t.Fatalf("catalog: %s", got)
}
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"b/vendor/m", "a/only-a"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
if p, m, ok := provider.Resolve("group/mine"); !ok || p.ID != "b" || m != "vendor/m" {
t.Fatalf("resolve: %v %s %s", ok, p.ID, m)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/mine","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from ka") || strings.Join(b.tried, ",") != "kb" {
t.Fatalf("group: %d %s, b tried %v", code, body, b.tried)
}
r := s.trace.routes[len(s.trace.routes)-1]
if r.Group == nil || r.Group.ID != "mine" || len(r.Order) != 2 || r.Order[0].Provider != "b" || r.Order[1].Model != "only-a" {
t.Fatalf("trace: %+v", r)
}
// renamed, the group still takes requests on its old id (xinvxueyuan
// on Discord); renamed back, it is its own id again
if err := provider.RenameGroup("mine", "ours"); err != nil {
t.Fatal(err)
}
if code, body := postAs(t, s, "", `{"model":"group/mine","messages":[{"role":"user","content":"hi"}]}`); code != 200 || !strings.Contains(body, "from ka") {
t.Fatalf("old id: %d %s", code, body)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Group == nil || r.Group.ID != "ours" {
t.Fatalf("old id trace: %+v", r)
}
if err := provider.RenameGroup("ours", "mine"); err != nil {
t.Fatal(err)
}
// removing one magpie found hides it; showing it brings it back
if err := provider.DeleteGroup("auto-m"); err != nil {
t.Fatal(err)
}
if _, _, ok := provider.Resolve("group/auto-m"); ok {
t.Fatal("hidden group still served")
}
provider.ShowGroup("auto-m")
if _, _, ok := provider.Resolve("group/auto-m"); !ok {
t.Fatal("shown group not served")
}
// one serving only through its groups leaves the list, not the groups
pb, err := provider.Find("b")
if err != nil {
t.Fatal(err)
}
pb.Unlisted = true
if err := provider.Save(*pb); err != nil {
t.Fatal(err)
}
for _, e := range provider.Catalog() {
if e.Provider.ID == "b" && !strings.HasPrefix(e.ID, provider.GroupPrefix) {
t.Fatalf("unlisted b listed as %s", e.ID)
}
}
if p, m, ok := provider.Resolve("group/mine"); !ok || p.ID != "b" || m != "vendor/m" {
t.Fatalf("unlisted member: %v %s %s", ok, p.ID, m)
}
if _, _, ok := provider.Resolve("b/vendor/m"); !ok {
t.Fatal("unlisted b not served by its id")
}
}
// A conversation stays with the key that answered it: within a turn
// always, across turns while the vendor read enough of it from its cache,
// or not at all when its affinity is off — rotating otherwise.
func TestAffinity(t *testing.T) {
const (
first = `{"model":"aff/m","messages":[{"role":"user","content":"hi"}]}`
again = `{"model":"aff/m","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"yo"},{"role":"user","content":"more"}]}`
tool = `{"model":"aff/m","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"ls","arguments":"{}"}}]},{"role":"tool","tool_call_id":"c1","content":"a.go"}]}`
)
for _, c := range []struct {
routing string
affinity string
body string
want string // the key the second request goes to
why string
}{
{"", "", again, "k1", "cache"},
{provider.Rotate, "", again, "k2", "new-turn"}, // in turn spreads the turns
{provider.Rotate, "", tool, "k1", "turn"},
{provider.Rotate, provider.AffinityTurn, again, "k2", "new-turn"},
{provider.Rotate, provider.AffinityTurn, tool, "k1", "turn"},
{provider.Rotate, provider.AffinitySession, again, "k1", "session"},
{provider.Rotate, provider.AffinityOff, again, "k2", "off"},
} {
fresh(t)
v := &keyed{}
serveOn(t, "aff", "k1", []string{"m"}, v, "k2")
provider.SetRouting("aff", c.routing)
provider.SetAffinity("aff", c.affinity)
routes.Forget("aff")
s := New()
postAs(t, s, "s1", first)
postAs(t, s, "s1", c.body)
r := s.trace.routes[len(s.trace.routes)-1]
if got := v.tried[len(v.tried)-1]; got != c.want || r.Affinity == nil || r.Affinity.Why != c.why {
t.Errorf("%s %q: went to %s (%+v), want %s by %s", c.routing, c.affinity, got, r.Affinity, c.want, c.why)
}
}
// in turn, each turn goes to the one after the last turn's, whatever
// came between: the tool results within it, another model's requests
fresh(t)
v0 := &keyed{}
serveOn(t, "aff", "k1", []string{"m", "small"}, v0, "k2", "k3")
provider.SetRouting("aff", provider.Rotate)
s0 := New()
var turns []string
msgs := `{"role":"user","content":"hi"}`
for i := range 4 {
if i > 0 {
msgs += `,{"role":"assistant","content":"yo"},{"role":"user","content":"more"}`
}
postAs(t, s0, "s1", `{"model":"aff/m","messages":[`+msgs+`]}`)
turns = append(turns, v0.tried[len(v0.tried)-1])
postAs(t, s0, "s1", `{"model":"aff/small","messages":[{"role":"user","content":"title?"}]}`)
postAs(t, s0, "s1", `{"model":"aff/m","messages":[`+msgs+`,{"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"ls","arguments":"{}"}}]},{"role":"tool","tool_call_id":"c1","content":"a.go"}]}`)
if got := v0.tried[len(v0.tried)-1]; got != turns[i] {
t.Fatalf("turn %d's tool result went to %s, not %s", i, got, turns[i])
}
}
for i := 1; i < len(turns); i++ {
if want := map[string]string{"k1": "k2", "k2": "k3", "k3": "k1"}[turns[i-1]]; turns[i] != want {
t.Fatalf("turns went %v", turns)
}
}
// another session starts fresh, and one resting is left
fresh(t)
v := &keyed{}
serveOn(t, "aff", "k1", []string{"m"}, v, "k2")
provider.SetAffinity("aff", provider.AffinitySession)
s := New()
postAs(t, s, "s1", first)
restingUntil.Lock()
restingUntil.m["aff#"+provider.KeyID("k1")] = time.Now().Add(time.Minute)
restingUntil.Unlock()
postAs(t, s, "s1", again)
if r := s.trace.routes[len(s.trace.routes)-1]; v.tried[1] != "k2" || r.Affinity.Why != "resting" {
t.Fatalf("resting: %v %+v", v.tried, r.Affinity)
}
postAs(t, s, "s2", first)
if r := s.trace.routes[len(s.trace.routes)-1]; r.Affinity.Why != "first" {
t.Fatalf("new session: %+v", r.Affinity)
}
}
// Keys made for different protocols are not one pool: in turn goes round
// the keys made for the protocol that suits the request, and a key made for
// another is tried only after them.
func TestKeyPoolsByProtocol(t *testing.T) {
fresh(t)
v := &keyed{}
up := httptest.NewServer(v)
t.Cleanup(up.Close)
p := provider.Provider{ID: "mix", Name: "MIX", Key: "k1", KeyProtocol: provider.Chat, Models: []string{"m"}, Chat: up.URL + "/v1",
Keys: []provider.KeyAccount{{Key: "k2"}, {Key: "k3", Protocol: provider.Chat}}, Routing: provider.Rotate}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
for range 4 {
postAs(t, s, "", `{"model":"mix/m","messages":[{"role":"user","content":"hi"}]}`)
}
if got := strings.Join(v.tried, ","); got != "k1,k3,k1,k3" {
t.Fatalf("in turn went %s", got)
}
r := s.trace.routes[len(s.trace.routes)-1]
if last := r.Order[len(r.Order)-1]; !last.Aside || last.Who == "" || r.Order[0].Aside {
t.Fatalf("trace: %+v", r.Order)
}
v.tried, v.fail = nil, map[string]int{"k1": 429, "k3": 429}
code, body := postAs(t, s, "", `{"model":"mix/m","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from k2") {
t.Fatalf("aside key: %d %s, tried %v", code, body, v.tried)
}
}
// A Gemini client without a recognized session header keeps its provider key
// across turns even when the request's contents, model and instructions grow.
func TestGeminiFallbackSessionAffinity(t *testing.T) {
fresh(t)
v := &geminiKeyed{}
serveOn(t, "aff", "k1", []string{"m"}, v, "k2")
provider.SetAffinity("aff", provider.AffinitySession)
s := New()
for _, body := range []string{
`{"contents":[{"role":"user","parts":[{"text":"fix the bug"}]}]}`,
`{"systemInstruction":{"parts":[{"text":"help"}]},"contents":[{"role":"user","parts":[{"text":"fix the bug"}]},{"role":"model","parts":[{"text":"done"}]},{"role":"user","parts":[{"text":"thanks"}]}]}`,
} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1beta/models/aff/m:generateContent", strings.NewReader(body))
s.Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("Gemini request: %d %s", rec.Code, rec.Body.String())
}
}
if len(v.tried) != 2 || v.tried[0] != v.tried[1] {
t.Fatalf("Gemini turns changed key: %v", v.tried)
}
r := s.trace.routes[len(s.trace.routes)-1]
if r.Affinity == nil || !r.Affinity.Kept || r.Affinity.Why != "session" {
t.Fatalf("Gemini turn did not keep session affinity: %+v", r.Affinity)
}
}
// Gemini is translated to a streamed Chat response by the gateway.
type geminiKeyed struct{ tried []string }
func (k *geminiKeyed) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
k.tried = append(k.tried, key)
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"id":"x","choices":[{"delta":{"content":"from `+key+`"}}]}`,
`data: {"id":"x","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: {"id":"x","choices":[],"usage":{"prompt_tokens":3000,"completion_tokens":5,"prompt_tokens_details":{"cached_tokens":2500}}}`,
`data: [DONE]`,
))
}
// A group in turn keeping each session where it began: a second session
// goes to the model the first isn't on, however many requests the first
// sent before it (#946); one resting is still passed over.
func TestRotateSpreadsSessions(t *testing.T) {
pair := func() (*keyed, *keyed, *Server) {
fresh(t)
a, b := &keyed{}, &keyed{}
serveOn(t, "ga", "ka", []string{"m1"}, a)
serveOn(t, "gb", "kb", []string{"m2"}, b)
if err := provider.SaveGroup(provider.Group{Name: "Pair", Members: []string{"ga/m1", "gb/m2"}, Routing: provider.Rotate, Affinity: provider.AffinitySession}); err != nil {
t.Fatal(err)
}
routes.Forget(provider.GroupPrefix + "pair")
return a, b, New()
}
// which of the two the request went to
went := func(a, b *keyed, post func()) string {
na, nb := len(a.tried), len(b.tried)
post()
switch {
case len(a.tried) > na && len(b.tried) == nb:
return "m1"
case len(b.tried) > nb && len(a.tried) == na:
return "m2"
}
t.Fatalf("tried %v and %v", a.tried, b.tried)
return ""
}
for n := 1; n <= 4; n++ {
a, b, s := pair()
msgs := `{"role":"user","content":"hi"}`
var first string
for i := range n {
if i > 0 {
msgs += `,{"role":"assistant","content":"yo"},{"role":"user","content":"more"}`
}
first = went(a, b, func() { postAs(t, s, "s1", `{"model":"group/pair","messages":[`+msgs+`]}`) })
}
second := went(a, b, func() { postAs(t, s, "s2", `{"model":"group/pair","messages":[{"role":"user","content":"other"}]}`) })
if first == second {
t.Errorf("after %d requests of s1 on %s, s2 went there too", n, first)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Affinity == nil || r.Affinity.Why != "first" {
t.Errorf("s2's affinity: %+v", r.Affinity)
}
}
// the one fewest are on is resting: routing's order stands
a, b, s := pair()
first := went(a, b, func() { postAs(t, s, "s1", `{"model":"group/pair","messages":[{"role":"user","content":"hi"}]}`) })
other := map[string]string{"m1": "gb", "m2": "ga"}[first]
restingUntil.Lock()
restingUntil.m[other] = time.Now().Add(time.Minute)
restingUntil.Unlock()
if got := went(a, b, func() { postAs(t, s, "s2", `{"model":"group/pair","messages":[{"role":"user","content":"other"}]}`) }); got != first {
t.Fatalf("s2 went to %s, where %s rests", got, other)
}
}