Files
magpie/internal/gateway/vision_wait_test.go
T
yetoneandump45nose 50a7031902 test: TestVisionWaitIsntTheDescribersTime gives the describer 2s, not 300ms, so a loaded run doesn't turn it red (#2444, ump45nose)
The test queues an image description behind a held request for twice
the describer's limit, then needs the description back within that
limit once the lane opens. At 300ms the fake's answer itself came in
late on a loaded machine and on CI (main f191d6423, test
(ubuntu-latest): "no answer in 300ms"). ump45nose's patch on #2444
gives it 2s; it is queued 4s, so the queue still outlasts the limit
twice over and the test still holds #2318 to its point.

Run, in my own worktree at origin/main 717b69f78 + this change:
- go test -tags nogui -count=3 -run TestVision ./internal/gateway: ok
- broken by hand: askVisionWith back on context.WithTimeout(ctx,
  visionTimeout) (the code before #2318) -> red, "context deadline
  exceeded" after the 4s queue; restored.
- go test -tags nogui -race -count=5 -cpu 1,2 -run TestVision
  ./internal/gateway: ok (151s) at load average ~400.
- gofmt -l clean; GOOS=darwin, linux and windows go vet -tags nogui
  ./internal/gateway: ok.
- Only this _test.go file changed, and no other package imports it, so
  the full suite was not run. Rebased onto e183604f0 (two claude
  commits, not in internal/gateway) before pushing.

Co-authored-by: ump45nose <52391318+ump45nose@users.noreply.github.com>
2026-10-17 20:19:07 +08:00

127 lines
4.2 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// slowEye is eyeUp with the describing model's answer held: until gate is
// closed for a request that says "hold", and by think for a description.
type slowEye struct {
eyeUp
held chan struct{} // a request that says "hold" has arrived
gate chan struct{}
think time.Duration
open func() // closes gate, once
}
func (u *slowEye) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
if strings.Contains(string(body), "hold") {
u.held <- struct{}{}
<-u.gate
} else if strings.HasSuffix(modelOf(body), "eye") {
time.Sleep(u.think)
}
r.Body = io.NopCloser(strings.NewReader(string(body)))
u.eyeUp.ServeHTTP(w, r)
}
// slowEyed is eyed with one request at a time to the provider, so a
// request waits in its lane while another is out.
func slowEyed(t *testing.T, think time.Duration) (*Server, *slowEye) {
t.Helper()
fresh(t)
u := &slowEye{held: make(chan struct{}, 1), gate: make(chan struct{}), think: think}
up := httptest.NewServer(u)
t.Cleanup(up.Close)
var once sync.Once
t.Cleanup(func() { once.Do(func() { close(u.gate) }) })
u.open = func() { once.Do(func() { close(u.gate) }) }
one := 1
if err := provider.Save(provider.Provider{ID: "probe", Name: "Probe", Chat: up.URL + "/v1", Key: "key", Models: []string{"text", "eye"}, MaxConcurrency: &one, QueueWait: 60}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("probe", up.URL+"/v1", []catalog.Model{
{ID: "text", ImageInput: imageInputBool(false)},
{ID: "eye", Images: true, ImageInput: imageInputBool(true)},
}); err != nil {
t.Fatal(err)
}
return New(), u
}
func visionLimits(t *testing.T, try, most time.Duration) {
t.Helper()
d, m := visionTimeout, visionWaitMost
visionTimeout, visionWaitMost = try, most
t.Cleanup(func() { visionTimeout, visionWaitMost = d, m })
}
const pastedImage = `{"model":"probe/text","messages":[{"role":"user","content":[{"type":"text","text":"read"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`
// #2318 (davisjiahao): the describer's time limit held the wait for its
// provider too, so a description queued behind another request had only
// what the queue left of it, and the image's turn failed. Queued for twice
// the limit, it is still described.
func TestVisionWaitIsntTheDescribersTime(t *testing.T) {
// 300ms left the describer no room once the lane opened under a loaded
// run (#2444); queued for twice a 2s limit it still proves the point.
visionLimits(t, 2*time.Second, 10*time.Second)
s, u := slowEyed(t, 0)
held := make(chan int, 1)
go func() {
code, _ := postAs(t, s, "", `{"model":"probe/eye","messages":[{"role":"user","content":"hold"}]}`)
held <- code
}()
// the held request has the provider's one slot
select {
case <-u.held:
case <-time.After(5 * time.Second):
t.Fatal("the held request never reached the provider")
}
time.AfterFunc(2*visionTimeout, u.open)
code, body := postAs(t, s, "", pastedImage)
if code != 200 {
t.Fatalf("a description that waited in its lane: %d %s", code, body)
}
if got := u.sent("text"); len(got) != 1 || !strings.Contains(got[0], "HELLO") {
t.Fatalf("text model was sent %v", got)
}
if c := <-held; c != 200 {
t.Fatalf("held request: %d", c)
}
}
// The limit still ends a describer that takes too long once asked, and the
// usage log says magpie stopped waiting, not that the agent canceled.
func TestVisionLimitEndsASlowDescriber(t *testing.T) {
visionLimits(t, 200*time.Millisecond, 10*time.Second)
s, _ := slowEyed(t, time.Second)
code, body := postAs(t, s, "", pastedImage)
if code != 502 || !strings.Contains(body, "no answer in 200ms") {
t.Fatalf("slow describer: %d %s", code, body)
}
var vision *usage.Record
for _, r := range usage.Load(time.Time{}) {
if r.Kind == "vision" {
vision = &r
}
}
if vision == nil {
t.Fatal("no vision request in the usage log")
}
if vision.Status != 499 || !strings.Contains(vision.Error, "magpie stopped waiting") || strings.Contains(vision.Error, "agent canceled") {
t.Fatalf("vision usage: %d %q", vision.Status, vision.Error)
}
}