Repository navigation
test(e2e): wait for the stack to reach a state, not for the clock - #794
Conversation
Three literal sleeps cost the full-stack suite 20 s of its 25.7 s. Each one waited for the PI controller, the slew limiter and the smoothed driver readings to catch up with a command, sized for the slowest machine that had ever run it — so it burned that time on every run and would still have failed as a bare wrong value on a slower one. waitForPV already had the right shape. Generalise it into waitForStatus, which polls /api/status until a condition holds, logs what the wait cost, and on timeout prints the mode, the readings and the dispatch targets it last saw. Each of the three sleeps becomes the condition its assertion was already checking: batteries charging under a positive grid target, the battery moving 500 W away from that charge under a negative one, and idle actually clearing the dispatch targets — the last is stronger than the mode read-back it replaces, since the handler sets the mode before the control tick does the work. The 200 ms sleep in setup stays: it runs before any driver, telemetry or API exists, so there is nothing to poll. Co-Authored-By: Claude Opus 5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fa59e7ea20
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| start := time.Now() | ||
| deadline := start.Add(timeout) |
There was a problem hiding this comment.
Apply the deadline to each status request
If /api/status accepts the connection but its handler blocks—for example while waiting on a stuck mutex—http.Get uses http.DefaultClient, whose zero timeout permits the request and body read to wait indefinitely. The loop therefore never reaches its deadline check and the test hangs until the package-level 180 s timeout instead of producing the new status summary; tie each request to the remaining deadline with a context or a client timeout.
Useful? React with 👍 / 👎.
Test-only. One file,
go/test/e2e/stack_test.go. No production code, no changeset (no-changesetlabel).The problem
The full-stack suite spent 20 s of its 25.7 s inside three literal sleeps — 6 s, 12 s and 2 s. Each waited for the PI controller, the slew limiter and the smoothed driver readings to catch up with a command that had just been posted. A sleep has to be sized for the slowest machine anyone has run the suite on, so it costs that much on every run and still fails as a bare wrong value once a runner is slower than the guess.
The file already had the right pattern:
waitForPVpolled/api/statusagainst a deadline.The change
waitForPVbecomeswaitForStatus(what, timeout, want): poll/api/statusuntil the condition holds, return the status that satisfied it. Two things come with it.waited 5.01s for batteries to charge …). When the stack gets slower, the numbers move there first, long before anything times out.Each sleep becomes the condition its assertion was already checking:
sleep 6sthenbat_w >= 300bat_w >= 300 Wundergrid_target_w=+3000, deadline 30 ssleep 12sthenbat_w < prevBat-500bat_w < prevBat-500undergrid_target_w=-3000, deadline 60 ssleep 2sthenmode == "idle"mode == "idle"and dispatch targets empty, deadline 20 sNo threshold moved. The idle wait is the one assertion that got stronger, and deliberately: the API handler sets the mode before it returns, so reading the mode back proved nothing about dispatch. Clearing the targets is the next control tick's work —
ComputeDispatchshort-circuits on idle and leaves no targets behind unless the fuse-saver overrides it. That is what the comment above the sleep always claimed, and what the sleep was really waiting for, so the wait now checks it.The deadlines are backstops set well above what the stack needs (6x, 4x and 20x the observed waits). The worst case they can add up to is 120 s, inside the 180 s
make e2egives the suite, so a real regression fails with the message above rather than a harness timeout panic. The header comment still said-timeout 120s; corrected to match the Makefile.The sleep that stays
The 200 ms in
setupStack, before the drivers are added. It gives the simulators one physics tick so the first Modbus read and the first MQTT message have a site behind them. There is no driver, no telemetry and no API to ask at that point, so there is nothing to poll. It now carries a comment saying so.Measured
Same machine,
FTW_E2E=1 go test ./test/e2e -count=1, back to back.Typical run: 25.7 s → 16.3 s.
The spread is worth naming. The reversal assertion measures 500 W down from whatever the battery reading was when the charge wait returned, and the driver poll interval is 5 s, so which point of the charge ramp that sample lands on varies. Catch it at 1.1 kW instead of 2.4 kW and the reversal has to reach 500 W below a lower number, which takes the PI loop longer to unwind to. Same assertion the sleep checked; the variance was always there, and is now visible. The slow tail is still no slower than the old fixed cost.
Related, and left alone: with the sleeps gone, the suite's floor is the 5 s default driver poll cadence — every wait lands within about 100 ms of a multiple of it. That is the next thing to look at if this suite has to get faster again.
Verification
make verifyclean.timed out after 4s waiting for idle mode to clear the dispatch targets; last status: mode=idle grid=-1157W target=-3000W pv=-3500W bat=129W load=2214W dispatch=[]and was then reverted.
full stackjob on this PR ran the suite in 16.31 s, with the same per-wait numbers as the laptop (5.09 s, 4.98 s, 4.98 s, 1.02 s), so the deadlines are nowhere near being reached on a GitHub runner either.