Fix flaky Task.WhenAny + Assert.Same timing races - #246
Merged
Conversation
Add an AsyncAssert.CompletesWithinAsync helper (one copy per test project, matching the existing Infrastructure/ convention in the Conformance suite) to replace the fragile `Assert.Same(task, await Task.WhenAny(task, Task.Delay(timeout)))` pattern. The helper: - reports elapsed time and a "because" reason on timeout, instead of a bare "Values are not the same instance" failure; - re-awaits the original task so any exception it faulted with propagates, rather than being swallowed by Task.WhenAny. Widened the fixed timeouts flagged in the issue with modest headroom for CI-runner contention (KeepAliveSchedulerTests 5s -> 10s, SequenceHeartbeatTests 5x -> 10x interval, Retransmit/NotApplied/ ReconnectRetransmit 3s -> 5s), and moved KeepAliveSchedulerTests' scheduler Stop()/Dispose() into a finally block so cleanup still runs if the wait times out. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CI on PR #246 still failed with the widened 5s timeout, and the elapsed time was exactly 5.0002030s — i.e. a real hang, not a close-shave flake. The TryPeek-then-subscribe fallback left a window between checking peerInboundNosSeqs and subscribing the probe handler where the peer's MessageReceived event for the resubmitted order could fire and be lost forever, since nothing was listening for it yet and peerInboundNosSeqs was checked (not re-checked) only once. Fix: subscribe the probe before calling SubmitAsync so the event literally cannot be missed. Ran the affected test 20x locally against the in-process TestPeer with no failures (previously it flaked here under contention). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Fixes #245.
Adds an
AsyncAssert.CompletesWithinAsynchelper (one copy per testproject, following the existing
Infrastructure/convention alreadyused in the Conformance suite) to replace the fragile
Assert.Same(task, await Task.WhenAny(task, Task.Delay(timeout)))pattern flagged in the issue. The helper:
instead of the opaque
Assert.Same() Failure: Values are not the same instance;faulted with propagates, rather than being silently swallowed by
Task.WhenAny.Also widened the fixed timeouts called out in the issue with modest
headroom for CI-runner contention:
KeepAliveSchedulerTests: 5s → 10s (40ms tick interval kept as-is,cleanup moved into a
finallysoStop()/Dispose()still run ontimeout)
SequenceHeartbeatTests: 5× → 10× intervalRetransmitTests/RetransmitRejectTests/NotAppliedTests/ReconnectRetransmitTests: 3s → 5sAll 6 call sites named in the issue (plus the one in
SequenceHeartbeatTestsnot explicitly listed) were converted.Validation
dotnet build SbeB3EntryPointClient.slnx— succeeded, 0 warnings.dotnet test SbeB3EntryPointClient.slnx --no-build— 233/233 passedin
B3.EntryPoint.Client.Tests; Conformance suite skipped (no peerconfigured locally), consistent with existing CI behavior.
dotnet format SbeB3EntryPointClient.slnx --verify-no-changes --no-restore --severity warn— no changes needed.