Skip to content

test(e2e): wait for the stack to reach a state, not for the clock - #794

Merged
frahlg merged 1 commit into
masterfrom
harvest-e2e-condition-polls
Aug 4, 2026
Merged

frahlg merged 1 commit into
masterfrom
harvest-e2e-condition-polls

Conversation

@frahlg

@frahlg frahlg commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Test-only. One file, go/test/e2e/stack_test.go. No production code, no changeset (no-changeset label).

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: waitForPV polled /api/status against a deadline.

The change

waitForPV becomes waitForStatus(what, timeout, want): poll /api/status until the condition holds, return the status that satisfied it. Two things come with it.

  • Every successful wait logs what it cost (waited 5.01s for batteries to charge …). When the stack gets slower, the numbers move there first, long before anything times out.
  • A timeout prints mode, grid, target, PV, battery, load and the dispatch targets last seen, so a stuck wait says what the controller was doing, not only that it never finished.

Each sleep becomes the condition its assertion was already checking:

Was Now waits for
sleep 6s then bat_w >= 300 bat_w >= 300 W under grid_target_w=+3000, deadline 30 s
sleep 12s then bat_w < prevBat-500 bat_w < prevBat-500 under grid_target_w=-3000, deadline 60 s
sleep 2s then mode == "idle" mode == "idle" and dispatch targets empty, deadline 20 s

No 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 — ComputeDispatch short-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 e2e gives 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.

  • Before, 4 runs: 25.58, 25.62, 25.69, 26.05 s.
  • After, 11 runs: 16.26, 16.29, 16.30, 16.30, 16.34, 16.36, 16.41, 16.57, 16.60, 16.71, 16.77 s, plus longer ones at 18.38, 21.56, 21.89 s and one 26.58 s in an earlier round. All passed.

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 verify clean.
  • 11 runs of the e2e suite, all passing.
  • Failure path checked by hand: a deliberately impossible condition produced
    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.
  • CI agrees. The full stack job 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.

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]>
@frahlg frahlg added no-changeset PR intentionally exempt from the changeset requirement (dev tooling / non-shipping) and removed no-changeset PR intentionally exempt from the changeset requirement (dev tooling / non-shipping) labels Aug 4, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread go/test/e2e/stack_test.go
Comment on lines +402 to +403
start := time.Now()
deadline := start.Add(timeout)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@frahlg
frahlg merged commit a23b5e9 into master Aug 4, 2026
14 of 17 checks passed
@frahlg
frahlg deleted the harvest-e2e-condition-polls branch August 7, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changeset PR intentionally exempt from the changeset requirement (dev tooling / non-shipping)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant