request's start (#2965) Cause: the test judged the second request by when it began, letting it want 0 rebuilds only within 900ms of the first, while what decides a rebuild is the hold and the stamp, not the clock from a request's start. Two ways that guess goes red: on a loaded machine a request can begin within the second and reach the build after the hold has let what was built go — "request 2 built the providers 1 times, want 0", seen in full runs at load ~280–400; and since6a050eebkeeps the catalog for the next requests, a request that begins past the 900ms builds none at all — "request 2 built the providers 0 times, want 1", which an idle machine now reproduces with a plain 1.2s pause between the requests. Fix: allProviders notes when the build it counts was asked for (AllBuiltAt, beside AllBuilt), and the test judges a rebuild by that, not by the request's start: a rebuild asked a second or more after the first build's ask is the hold letting go, rightly, and is allowed only when it is exactly one; a rebuild asked within the second, or more than one build in a request (the #2502 bug class), stays red. Nothing rests on how fast the machine gets from a request's start to its build. Red first (macOS arm64, idle, this tree): with a 1.2s pause put before the second request, the old test failed "request 2 built the providers 0 times, want 1"; the new test passed the same pause (-count=3); the old form failed it again with the hook in place (ablation: the test is what guards). The loaded-machine shape is the issue's own run at load ~280–400 and the maintainer's "request 2 built the providers 1 times, want 0" at c8c0985df; the "request 1 built 2 times" red atdee5c9d75is not the guess — two builds inside one request stays red here by design, per #2502. Run (macOS arm64, go1.26.8, load ~4–7): - go test -tags nogui -count=5 -run 'TestRequestBuildsProvidersOnce$' ./internal/gateway/: ok. - go test -tags nogui -race -count=4 -run 'TestRequestBuildsProvidersOnce$' ./internal/gateway/: ok. - go test -tags nogui -count=1 ./internal/gateway/: ok (634.7s). - go test -tags nogui -count=1 ./internal/provider/: ok (127.8s). - gofmt -l internal/gateway/ internal/provider/: clean. - go vet -tags nogui ./internal/gateway/ ./internal/provider/ for darwin, linux, windows: ok. - CGO_ENABLED=0 go build -tags nogui ./..., darwin/linux/windows: ok. Fixes #2965 Generated-by: GLM-5.3-Flash (ZCode) Review (mote, go1.26.8): go test -tags nogui -count=3 -run 'TestRequestBuildsProvidersOnce$' ./internal/gateway/ ok. A rebuild happens only when the second ask is a second or more after the build's end (h.at is set after the build), so a second or more between the asks is necessary and the new allowance excuses no wrong rebuild. Broken by hand: heldFor at 1ns makes it red ("request 1 built the providers 2 times, want 1"); a 1.2s pause before request 2 passes, which is the case the old test misjudged.
51 lines
1.8 KiB
Go
51 lines
1.8 KiB
Go
package gateway
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/plugin"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// A request builds the providers once, however many times it looks a
|
||
// model up on its way: each build reads every agent's sign-in, every
|
||
// plugin's and the settings, and three builds a request kept magpie at
|
||
// 100–350% CPU under seven agents at once (#2502). The next request,
|
||
// within a second and nothing written, builds none: one request after
|
||
// another built the providers again each time. After a write it builds
|
||
// once more. What a request within that second is judged by is when its
|
||
// build was asked, not when the request began: on a loaded machine a
|
||
// request can begin within the second what was built is held for and
|
||
// reach the build after the hold has let it go, and that build is right
|
||
// (#2965).
|
||
func TestRequestBuildsProvidersOnce(t *testing.T) {
|
||
plugin.UseCached(nil)
|
||
setup(t, provider.Chat, &fake{t: t, reply: sse(`{"choices":[{"delta":{"content":"hi"}}]}`, "[DONE]")})
|
||
var asked time.Time // when the first build was asked for
|
||
for i, want := range []int64{1, 0, 1} {
|
||
if i == 2 {
|
||
provider.Changed() // a provider saved
|
||
}
|
||
n := provider.AllBuilt.Load()
|
||
code, body := post(t, "/v1/chat/completions", `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
||
if code != 200 {
|
||
t.Fatalf("%d %s", code, body)
|
||
}
|
||
got := provider.AllBuilt.Load() - n
|
||
at := time.Unix(0, provider.AllBuiltAt.Load())
|
||
if i == 0 {
|
||
asked = at
|
||
}
|
||
if i == 1 && got == 1 && at.Sub(asked) >= time.Second {
|
||
// the request began within the second the built providers are
|
||
// held for, but its build was asked after the hold: built
|
||
// again, rightly (a loaded machine)
|
||
want = 1
|
||
}
|
||
if got != want {
|
||
t.Fatalf("request %d built the providers %d times, want %d", i+1, got, want)
|
||
}
|
||
}
|
||
}
|