Repository navigation
fix(tui): make the stream-manager client tests deterministic - #578
Merged
Merged
Conversation
`rpc::grpc_client::tests::subscribe_stream_event_bookkeeping` and its
siblings were flaky (about one full-suite run in eight). Two causes, both in
the test scaffolding:
1. The mock's `stream_events` handed its single event channel to the *first*
subscription and answered every resubscribe with an empty `pending()`
stream — while the original receiver was dropped with the abandoned
stream. A poke that landed while the stream manager was subscribing
therefore left the client on a dead stream, and the test's next send
panicked with `SendError { .. }` (reproduced deterministically by a new
regression test).
2. The tests slept (100-200 ms) to let the client subscribe, then assumed the
subscription they had waited for was the live one. A poke that lands while
the manager is subscribing makes it return `Poked` at the top of its loop
*before* it ever reads a message, so "a subscription exists" is no proof
that events will be delivered: they are dropped with the abandoned stream.
The mock now serves every subscription a live stream of its own, like the
real agent, and delivers each event to all live subscriptions. Tests attach
through a helper that probes with a `ping` and requires two round trips in a
row — the first proves the subscription is reading, the second that it
survived the poke still in flight behind it — instead of sleeping. Assertions
that followed a sleep now wait for the event itself, and every wait is
bounded and fails with a message rather than hanging.
This also drops `spin_until_bool` (superseded by the `recv_until` helper) and
removes two sleeps whose only purpose was timing the client.
Verified by running the full suite 110 times (including under heavy parallel
load and with two suites at once) with no failures; the pre-fix state failed
within ~17 runs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
tui rpc::grpc_client::tests::subscribe_stream_event_bookkeepingwas flaky — roughly one full-suite run in eight — and five of its siblings shared the same root cause. Both causes are in the test scaffolding, not in the client.1. The mock killed the stream on every resubscribe
EventfulMock::stream_eventshanded its single event channel to the first subscription and answered every later one with an emptypending()stream, while the original receiver was dropped along with the abandoned stream. So a poke that landed while the stream manager was subscribing left the client reading a stream that would never yield anything, and the test's nexttx.send(..).unwrap()panicked with:That is now reproduced deterministically by a new regression test before the fix (
poke_during_live_subscription_resubscribes_without_losing_events) and passes after it.2. "A subscription exists" is not proof of delivery
The tests slept 100–200 ms to let the client subscribe, then assumed the subscription they waited for was the live one. A poke that lands while the manager is subscribing makes it return
Pokedat the top of its loop before it ever reads a message, so events sent to that subscription are dropped with it. Sleeping longer cannot fix that — only proving delivery can.What changed (test code only,
tui/src/rpc/grpc_client.rs)EventSourcereplaces the single-shot channel: everystream_eventscall gets a live stream of its own (like the real agent), and a send goes to every live subscription, pruning the ones the client dropped. It also counts subscriptions and notifies on each, so tests can wait for the client's own (re)subscription.attach()replaces the sleep-then-guess setup: it sets the session, pokes once, then probes with apingand requires two round trips in a row — the first proves the subscription is reading, the second that it survived the poke still in flight behind it. Nothing pokes afterwards, so a subscription that answers both keeps receiving for the rest of the test.recv_until()waits for the event an assertion depends on (the client forwards an event only after its run bookkeeping, so a received event proves the state it implies). That removes the sleeps that were timing the client, including the one in front of a negative assertion (agent_endfor a different run must keep the run active), which now sends a sentinelpingbehind it instead.senderrors when nothing is subscribed rather than dropping the event silently.spin_until_boolis gone (superseded byrecv_until), along with its self-test.Evidence
SendErrorabove).cargo fmt -p future-tui --check,cargo clippy -p future-tui --all-targets -- -D warnings,cargo test -p future-tuiall clean (809 lib + 13 integration tests).No product code changed: the client, the stream manager and the mock's contract are as before — the tests now synchronize on delivery instead of on wall-clock guesses.