Files
magpie/internal/gateway/rest_wall_test.go
T
yetone 8769a5819a gateway: a rest ends at the time it shows, after the Mac slept (plugins#1126)
A rest's end came from time.Now().Add(d), which carries Go's monotonic
reading, and restOf and Retry-After compared by it. On macOS that clock
stops while the computer sleeps, so a WorkBuddy AI rest to 00:40 still
held at 09:57 the next day: 17h22m past its end, against 17h26m of
sleep in pmset's log over the same span (Xiang-Player's measurements).

onWall drops the monotonic reading. It is applied where magpie keeps a
deadline for minutes to days: the three places a rest's end is set
(restAfterMarked, restSlow, restDrawer), Codex's held refusal, Claude's
quota shelf, a Codex window's reset and a search key's rest. Shorter
waits (a request's patience, ten-minute shuns) are left as they are; a
sleep can only stretch them by their own length.

TestRestsKeepTheWallClock records a rest through the quota and 500
paths, a slow start and a search key, and checks none counts on the
monotonic clock.

Run on mica at this tree: go vet ./internal/gateway (darwin, linux,
windows); go test ./internal/gateway -count=1 -timeout 45m: ok (883s).
Broken by hand: onWall returning t unchanged turns the test red on all
four paths.
Not tried: a real Mac sleeping past a rest; a test can't sleep the
machine.

At the pushed head (rebased on 42cc55701, which adds internal/provider/quota_shown.go): go vet ./internal/gateway ./internal/provider and go test ./internal/gateway -run "Rest|TestOneRequestDoesNotClimbTheBackoff": ok.
2026-11-14 19:44:23 +08:00

73 lines
2.7 KiB
Go

package gateway
import (
"net/http"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A rest is kept on the wall clock alone. A time from time.Now() carries
// the monotonic clock too, and Go compares two such times by it; on macOS
// it stops while the computer sleeps, so a rest to 00:40 set the day
// before still held at 09:57 after a night of sleep, 17 hours past the
// time it showed (plugins#1126). Sleep can't be had in a test, so this
// checks what makes it harmless: no rest magpie keeps carries a monotonic
// reading ("m=" in its String).
func TestRestsKeepTheWallClock(t *testing.T) {
monotonic := func(at time.Time) bool { return strings.Contains(at.String(), " m=") }
if !monotonic(time.Now().Add(time.Hour)) {
t.Fatal("time.Now() carries no monotonic reading here: the test proves nothing")
}
codexSignedIn(t)
s := New()
c := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}, model: "gpt-6-astra", rest: "codex@me@example.com"}
quota := []byte(`{"error":{"message":"usage exceeds frequency limit, your usage will reset at ` +
time.Now().Add(14*time.Hour).Format("2006-01-02 15:04:05") + `","type":"usage_limit_reached"}}`)
for _, x := range []struct {
name string
body []byte
code int
}{
{"quota", quota, http.StatusTooManyRequests},
{"500", []byte(`{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`), http.StatusInternalServerError},
} {
r := s.restAfter(c, x.code, http.Header{}, x.body)
if r.Until.IsZero() {
t.Fatalf("%s: no rest: the test proves nothing", x.name)
}
if monotonic(r.Until) {
t.Errorf("%s: the rest's end %v counts on the monotonic clock", x.name, r.Until)
}
restingUntil.Lock()
for k, until := range restingUntil.m {
if monotonic(until) {
t.Errorf("%s: %s rests to %v on the monotonic clock", x.name, k, until)
}
}
restingUntil.Unlock()
}
slow := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "slow@example.com"}}, model: "gpt-6-astra", rest: "codex@slow@example.com"}
rested := false
for i := 0; i < slowRests+1; i++ {
if r, ok := restSlow(slow, time.Now()); ok {
rested = true
if monotonic(r.Until) {
t.Errorf("a slow start's rest to %v counts on the monotonic clock", r.Until)
}
}
}
if !rested {
t.Fatal("slow starts never rested: the test proves nothing")
}
restSearchKey("tavily", "k1", time.Hour, "rate limited", time.Now())
if r, ok := searchKeyRest("tavily", "k1", time.Now()); !ok || monotonic(r.Until) {
t.Errorf("a search key's rest %v (%v) counts on the monotonic clock", r.Until, ok)
}
}