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 <noreply@anthropic.com>
This commit is contained in:
1 parent
188230b579
commit
78a382fbdc
2 files changed
+18
-8
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user