From 78a382fbdcf2a5f7bd8550591ddcf51a13b20fc7 Mon Sep 17 00:00:00 2001 From: William Gill Date: Tue, 1 Dec 2026 23:03:14 -0600 Subject: [PATCH] Give the daemon tests an API rate limit their polling cannot reach TestServeRestart failed on CI with a restored job and the seeded job reading rate_limited. The daemon tests poll GET /api/v1/jobs/{id} every 20 ms under the default limit of 60 API requests a minute per client. Once those are spent, every request is refused for the rest of the minute, and the test's status helper returned the refusal's code as the job's status. The restored jobs retry with a doubling delay, so the slower the start, the longer they wait once the fake printer is up: up to 1.6 s when it comes up 1.5 s after the queue starts, more than the budget lasts at that rate. TestServeUnconfirmedJobReadsUncertain and TestServeEndToEnd poll the same way and shared the risk. The test daemons now allow a million API requests a minute, set in testConfig and in TestServeEndToEnd's config file. Their job polls fail the test on any answer that is not the job, so a refusal shows as one. The limiter is unchanged: it did what it was configured to do. Starting the second run's fake printer 1.6 s late reproduced the failure here, on the 61st request, and the test now passes that way after 101 polls. Co-Authored-By: Claude Opus 5.5 --- internal/app/uberbringer/restart_test.go | 10 ++++------ internal/app/uberbringer/serve_test.go | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/internal/app/uberbringer/restart_test.go b/internal/app/uberbringer/restart_test.go index 1d458ae..7847a20 100644 --- a/internal/app/uberbringer/restart_test.go +++ b/internal/app/uberbringer/restart_test.go @@ -97,7 +97,8 @@ type jobStatus struct { Status, Error string } -// status returns the status of job id, or job_not_found's code. +// status returns the status of job id. Any other answer fails the test, so +// that a refusal never reads as the job's status. func (d *daemon) status(t *testing.T, id string) jobStatus { t.Helper() req, _ := http.NewRequest(http.MethodGet, d.base+"/api/v1/jobs/"+id, nil) @@ -111,11 +112,8 @@ func (d *daemon) status(t *testing.T, id string) jobStatus { jobStatus Code string } - if err := json.NewDecoder(resp.Body).Decode(&st); err != nil { - t.Fatal(err) - } - if st.Code != "" { - return jobStatus{Status: st.Code} + if err := json.NewDecoder(resp.Body).Decode(&st); err != nil || resp.StatusCode != http.StatusOK { + t.Fatalf("GET /api/v1/jobs/%s = %d %s, %v", id, resp.StatusCode, st.Code, err) } return st.jobStatus } diff --git a/internal/app/uberbringer/serve_test.go b/internal/app/uberbringer/serve_test.go index b50ee26..882abd5 100644 --- a/internal/app/uberbringer/serve_test.go +++ b/internal/app/uberbringer/serve_test.go @@ -99,12 +99,14 @@ func TestServeEndToEnd(t *testing.T) { body := fmt.Sprintf(` [api] secret = %q +[webserver] +rate_limit = %d [printer] address = "127.0.0.1" port = %d [log] level = "error" -`, secret, printer.addr.Port) +`, secret, testRateLimit, printer.addr.Port) if err := os.WriteFile(path, []byte(body), 0o600); err != nil { t.Fatal(err) } @@ -163,6 +165,9 @@ level = "error" return false } defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatalf("GET /api/v1/jobs/%s = %d", queued.ID, resp.StatusCode) + } var st struct{ Status string } _ = json.NewDecoder(resp.Body).Decode(&st) return st.Status == "done" @@ -229,11 +234,18 @@ func waitFor(t *testing.T, cond func() bool) { t.Fatal("condition not met within 10s") } +// testRateLimit is the API rate limit of the daemons under test. waitFor +// polls every 20 ms, so on a slow runner a wait of a second or two spends +// the default budget of 60 requests a minute, and every later request in +// that minute is refused. No test comes near this limit. +const testRateLimit = 1_000_000 + // testConfig returns a valid config for a printer at port, logging errors -// only. +// only, with an API rate limit the tests cannot reach. func testConfig(port int) *config.Config { cfg := config.Default() cfg.API.Secret = "end-to-end-secret-end-to-end-secret!" + cfg.WebServer.RateLimit = testRateLimit cfg.Printer.Address, cfg.Printer.Port = "127.0.0.1", port cfg.Log.Level = "error" return &cfg