TestNPMInfoDeadline's fake npm held slow-one's reply on <-release, closed only
on the happy path. A check firing first under load (on the issue's machine,
the quick ones missing at npmAll = 300ms) left the handler held, and
httptest.Server.Close, run in the test's cleanup, waited for it forever:
the whole internal/plugin package hung to -timeout and lost every result.
- TestNPMInfoDeadline: a t.Cleanup registered after fakeNPM (so it runs
before the fake's Close) lets the handler go once (sync.OnceFunc). Its
timing assumptions, all listed: the 2s wall-clock bound on Info is gone
(slow-one is held and npmEach is an hour, so Info coming back at all is
npmAll at work; a minute only turns a wait into a FAIL); npmAll is 5s,
the quick ones' time to answer on a loaded machine, not 300ms; the slow
one's kept answer is polled for 30s, not 3s.
- The same pattern in its siblings, each fake handler blocked on a channel
only the happy path closed, now let go by a cleanup/defer that runs
before the server's Close: TestRestartFinishesTheReply (plugin),
TestPastedAddressAndCallbackFinishOnce, TestPartnersFetchedLastIsHeld
(provider), TestBusy (before gw.Close too, whose request waits on it),
TestLaneReleasedOnFailure (gateway), TestReadOnceInFresh (upstream; its
100ms bound on Of goes the same way as the 2s one: the page is held, so
Of coming back is its not waiting, 10s being short of the read's 15s).
- Audited, already right: grok_accounts, quota_refresh_order,
quota_history_waiter, subscription_usage, sub2api_usage, qoder, routing,
github_test, session_route, request_encoding, key_limits, vertex (quit),
plugin_usage (r.Context).
Run, in a sandbox HOME with pinned caches:
- Old TestNPMInfoDeadline with its check forced (took > 0): "httptest.Server
blocked in Close", panic at -timeout 30s. With a 500ms delay injected into
the fake's answers (a loaded machine): FAIL at the quick-ones check, then
the same hang. New test: with the delay injected, PASS (5.55s); with the
check forced, FAIL in 5.5s and the package's other tests run; with
npmAll an hour (the deadline broken), "Info waited for the slow package"
and the package ends.
- Each sibling with t.Fatal injected while its handler is held: old code,
all five httptest siblings hang to -timeout 90s ("blocked in Close"),
upstream's stalls ~56s; new code, each ends at once with FAIL.
- go test -race -count=5 of every touched test: pass.
- gofmt -l (nothing), go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: clean, before and after rebasing onto
a3f2097ed. go test -tags nogui ./... (darwin): all 49 packages ok before
the rebase; after it, all ok but internal/proc's
TestFindToolKeepsWrapperWhenMiseDoesntSay/hangs ("mise wasn't asked",
a 300ms miseWait under load; passes 20 of 20 alone), filed as #1838.
Not run: the Windows box, Linux tests.
307 lines
12 KiB
Go
307 lines
12 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// laneRig is a gateway in front of slow vendors, and a way to send it
|
|
// requests that answer on a channel.
|
|
type laneRig struct {
|
|
t *testing.T
|
|
s *Server
|
|
gw *httptest.Server
|
|
replies chan laneReply
|
|
ctx context.Context // ended when the test ends: nothing is left waiting in a lane
|
|
}
|
|
|
|
type laneReply struct {
|
|
tag, body string
|
|
status int
|
|
header http.Header
|
|
err error
|
|
}
|
|
|
|
func newLaneRig(t *testing.T) *laneRig {
|
|
s := New()
|
|
gw := httptest.NewServer(s.Handler())
|
|
t.Cleanup(gw.Close)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel) // runs before gw.Close, which waits for every request
|
|
return &laneRig{t: t, s: s, gw: gw, replies: make(chan laneReply, 20), ctx: ctx}
|
|
}
|
|
|
|
func slowUp(t *testing.T) (*slowVendor, string) {
|
|
v := &slowVendor{release: map[string]chan struct{}{}, quit: make(chan struct{})}
|
|
up := httptest.NewServer(v)
|
|
t.Cleanup(up.Close)
|
|
t.Cleanup(func() { close(v.quit) }) // runs first: a failed test leaves no handler waiting
|
|
return v, up.URL
|
|
}
|
|
|
|
// send asks for model with a chat completions stream whose message is tag,
|
|
// or with Anthropic's Messages when path says so.
|
|
func (g *laneRig) send(ctx context.Context, path, model, tag string) {
|
|
body := `{"model":"` + model + `","stream":true,"messages":[{"role":"user","content":"` + tag + `"}]}`
|
|
if path == "/v1/messages" {
|
|
body = `{"model":"` + model + `","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"` + tag + `"}]}`
|
|
}
|
|
ctx, stop := context.WithCancel(ctx)
|
|
context.AfterFunc(g.ctx, stop)
|
|
go func() {
|
|
defer stop()
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", g.gw.URL+path, strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
g.replies <- laneReply{tag: tag, err: err}
|
|
return
|
|
}
|
|
b, err := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
g.replies <- laneReply{tag, string(b), res.StatusCode, res.Header, err}
|
|
}()
|
|
}
|
|
|
|
func (g *laneRig) reply() laneReply {
|
|
g.t.Helper()
|
|
select {
|
|
case r := <-g.replies:
|
|
return r
|
|
case <-time.After(5 * time.Second):
|
|
g.t.Fatal("no reply")
|
|
}
|
|
return laneReply{}
|
|
}
|
|
|
|
// An account's or key's own limit (#892) is over the provider's, and its
|
|
// count is one whatever asks for it: a request for the model and requests
|
|
// through two routing groups, each with the key in it, wait for the one
|
|
// slot the key has, never more out at the vendor than that.
|
|
func TestAccountLimitSharedAcrossGroups(t *testing.T) {
|
|
fresh(t)
|
|
v, up := slowUp(t)
|
|
five := 5
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up + "/v1",
|
|
MaxConcurrency: &five, AccountConcurrency: map[string]int{provider.KeyID("k"): 1}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{"one", "two"} {
|
|
if err := provider.SaveGroup(provider.Group{ID: id, Name: id, Members: []string{"slow/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
g := newLaneRig(t)
|
|
lane := func() Lane { return g.s.Lanes()["slow#"+provider.KeyID("k")] }
|
|
|
|
g.send(context.Background(), "/v1/chat/completions", "group/one", "a")
|
|
within(t, "a at the vendor", func() bool { return len(v.seen()) == 1 })
|
|
g.send(context.Background(), "/v1/chat/completions", "group/two", "b")
|
|
within(t, "b queued", func() bool { return lane().Waiting == 1 })
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "c")
|
|
within(t, "c queued", func() bool { return lane().Waiting == 2 })
|
|
if l := lane(); l.Busy != 1 || l.Limit != 1 {
|
|
t.Fatalf("lane = %+v, want 1 out of a limit of 1 (the key's own, not the provider's 5)", l)
|
|
}
|
|
for i, tag := range []string{"a", "b", "c"} {
|
|
close(v.gate(tag))
|
|
if r := g.reply(); r.err != nil || r.tag != tag || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("reply %d = %+v, want %s whole", i, r, tag)
|
|
}
|
|
}
|
|
if v.most != 1 {
|
|
t.Fatalf("most out at once = %d, want 1", v.most)
|
|
}
|
|
within(t, "every slot given back", func() bool { return len(g.s.Lanes()) == 0 })
|
|
|
|
// set to none, the key takes no slot at all, the provider's 5 or not
|
|
if err := provider.SetAccountConcurrency("slow", provider.KeyID("k"), new(int)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := provider.Find("slow")
|
|
if n := p.LaneLimit(); n != 0 {
|
|
t.Fatalf("limit set to none = %d", n)
|
|
}
|
|
// and given the provider's again, it has 5
|
|
if err := provider.SetAccountConcurrency("slow", provider.KeyID("k"), nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = provider.Find("slow")
|
|
if n := p.LaneLimit(); n != 5 {
|
|
t.Fatalf("limit given back to the provider's = %d, want 5", n)
|
|
}
|
|
}
|
|
|
|
// A provider's QueueLimit turns away one more than it holds at once, and
|
|
// QueueWait one that waited that long: each with a 429 in the API's own
|
|
// shape, Retry-After, the queue left as it was, and the vendor never asked.
|
|
func TestQueueFullAndWaitedOut(t *testing.T) {
|
|
fresh(t)
|
|
v, up := slowUp(t)
|
|
one := 1
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up + "/v1", Anthropic: up,
|
|
MaxConcurrency: &one, QueueLimit: 1, QueueWait: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := newLaneRig(t)
|
|
lane := func() Lane { return g.s.Lanes()["slow#"+provider.KeyID("k")] }
|
|
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "a")
|
|
within(t, "a at the vendor", func() bool { return len(v.seen()) == 1 })
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "b")
|
|
within(t, "b queued", func() bool { return lane().Waiting == 1 })
|
|
|
|
// the queue full: c turned away at once, as OpenAI says a 429
|
|
began := time.Now()
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "c")
|
|
r := g.reply()
|
|
if r.tag != "c" || r.status != 429 || r.header.Get("Retry-After") == "" || time.Since(began) > 900*time.Millisecond {
|
|
t.Fatalf("c = %+v, want an immediate 429 with Retry-After", r)
|
|
}
|
|
var oe struct {
|
|
Error struct{ Message, Type string } `json:"error"`
|
|
}
|
|
if json.Unmarshal([]byte(r.body), &oe) != nil || !strings.Contains(oe.Error.Message, "queue is full") || oe.Error.Type == "" {
|
|
t.Fatalf("c's body = %s, want OpenAI's error saying the queue is full", r.body)
|
|
}
|
|
// in Anthropic's shape on its Messages
|
|
g.send(context.Background(), "/v1/messages", "slow/m", "d")
|
|
r = g.reply()
|
|
var ae struct {
|
|
Type string `json:"type"`
|
|
Error struct{ Type, Message string } `json:"error"`
|
|
}
|
|
if r.status != 429 || json.Unmarshal([]byte(r.body), &ae) != nil || ae.Type != "error" || ae.Error.Type != "rate_limit_error" || !strings.Contains(ae.Error.Message, "queue is full") {
|
|
t.Fatalf("d = %d %s, want Anthropic's rate_limit_error", r.status, r.body)
|
|
}
|
|
|
|
// b waits its second and is turned away, out of the queue
|
|
r = g.reply()
|
|
if r.tag != "b" || r.status != 429 || !strings.Contains(r.body, "waited") {
|
|
t.Fatalf("b = %+v, want a 429 for waiting too long", r)
|
|
}
|
|
if l := lane(); l.Busy != 1 || l.Waiting != 0 {
|
|
t.Fatalf("lane after b waited out = %+v, want a alone", l)
|
|
}
|
|
if got := v.seen(); !slices.Equal(got, []string{"a"}) {
|
|
t.Fatalf("vendor saw %v, want a alone", got)
|
|
}
|
|
close(v.gate("a"))
|
|
if r := g.reply(); r.tag != "a" || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("a = %+v", r)
|
|
}
|
|
within(t, "every slot given back", func() bool { return len(g.s.Lanes()) == 0 })
|
|
}
|
|
|
|
// A slot is given back when the vendor fails the request, and the next
|
|
// one waiting is sent.
|
|
func TestLaneReleasedOnFailure(t *testing.T) {
|
|
fresh(t)
|
|
var mu sync.Mutex
|
|
calls := 0
|
|
gate := make(chan struct{})
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
calls++
|
|
n := calls
|
|
mu.Unlock()
|
|
if n == 1 {
|
|
<-gate
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(400)
|
|
io.WriteString(w, `{"error":{"message":"bad request","type":"invalid_request_error"}}`)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, "data: {\"id\":\"x\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
// runs before up.Close, which waits for a's held request: a check that
|
|
// fails before the gate opens below doesn't leave it held (#1808)
|
|
let := sync.OnceFunc(func() { close(gate) })
|
|
t.Cleanup(let)
|
|
one := 1
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1", MaxConcurrency: &one}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := newLaneRig(t)
|
|
lane := func() Lane { return g.s.Lanes()["slow#"+provider.KeyID("k")] }
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "a")
|
|
within(t, "a out", func() bool { return lane().Busy == 1 })
|
|
g.send(context.Background(), "/v1/chat/completions", "slow/m", "b")
|
|
within(t, "b queued", func() bool { return lane().Waiting == 1 })
|
|
let()
|
|
got := map[string]laneReply{}
|
|
for range 2 {
|
|
r := g.reply()
|
|
got[r.tag] = r
|
|
}
|
|
if got["a"].status != 400 || got["b"].status != 200 || !strings.Contains(got["b"].body, "ok") {
|
|
t.Fatalf("a = %+v, b = %+v: want a's failure, then b served", got["a"], got["b"])
|
|
}
|
|
within(t, "every slot given back", func() bool { return len(g.s.Lanes()) == 0 })
|
|
}
|
|
|
|
// A full member of a routing group gives way to one with a slot free (no
|
|
// waiting while another member has room), while a provider's fallback is
|
|
// never asked for want of a slot: the request waits for its own.
|
|
func TestFullMemberGivesWay(t *testing.T) {
|
|
fresh(t)
|
|
v1, up1 := slowUp(t)
|
|
v2, up2 := slowUp(t)
|
|
one := 1
|
|
if err := provider.Save(provider.Provider{ID: "first", Name: "First", Key: "k1", Models: []string{"m"}, Chat: up1 + "/v1", MaxConcurrency: &one, Fallback: []string{"second/m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "second", Name: "Second", Key: "k2", Models: []string{"m"}, Chat: up2 + "/v1", MaxConcurrency: &one}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "pair", Name: "Pair", Members: []string{"first/m", "second/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := newLaneRig(t)
|
|
firstLane := func() Lane { return g.s.Lanes()["first#"+provider.KeyID("k1")] }
|
|
|
|
g.send(context.Background(), "/v1/chat/completions", "group/pair", "a")
|
|
within(t, "a at first", func() bool { return len(v1.seen()) == 1 })
|
|
// first is full: b goes to second at once
|
|
g.send(context.Background(), "/v1/chat/completions", "group/pair", "b")
|
|
within(t, "b at second", func() bool { return slices.Contains(v2.seen(), "b") })
|
|
if l := firstLane(); l.Waiting != 0 {
|
|
t.Fatalf("first's lane = %+v, want nobody waiting", l)
|
|
}
|
|
// both full: c waits for first, the group's first member
|
|
g.send(context.Background(), "/v1/chat/completions", "group/pair", "c")
|
|
within(t, "c queued at first", func() bool { return firstLane().Waiting == 1 })
|
|
|
|
// asked for first/m itself, d waits for first rather than going to its
|
|
// fallback
|
|
g.send(context.Background(), "/v1/chat/completions", "first/m", "d")
|
|
within(t, "d queued at first", func() bool { return firstLane().Waiting == 2 })
|
|
close(v2.gate("b"))
|
|
if r := g.reply(); r.tag != "b" || !strings.Contains(r.body, "from b") {
|
|
t.Fatalf("b = %+v", r)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
if slices.Contains(v2.seen(), "d") {
|
|
t.Fatal("d went to first's fallback for want of a slot")
|
|
}
|
|
for _, tag := range []string{"a", "c", "d"} {
|
|
close(v1.gate(tag))
|
|
if r := g.reply(); r.tag != tag || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("%s = %+v", tag, r)
|
|
}
|
|
}
|
|
within(t, "every slot given back", func() bool { return len(g.s.Lanes()) == 0 })
|
|
}
|