Summary
ContinuousStream_SurvivesRapidTopologyChurn in TurbulenceTests.cs can fail with a 1-off assertion error when the test is cancelled while the writer is in its inter-message delay. This is a test-only ordering bug — no product code changes needed.
Root cause
Volatile.Write(ref lastSeqWritten, seq) was placed after Task.Delay(50):
// Bug: delay then record — cancellation during delay loses the last seq
await Task.Delay(50, streamCts.Token);
Volatile.Write(ref lastSeqWritten, seq);
When streamCts.Cancel() fires during the 50ms delay, the WriteAsync for seq already completed successfully but lastSeqWritten was never updated. The assertion:
long totalWritten = Volatile.Read(ref lastSeqWritten) + 1;
Assert.Equal(totalWritten, sorted.Count);
fails by 1 — sorted.Count (which includes the already-written-and-delivered message) exceeds lastSeqWritten + 1.
This bug was masked by #553 (buffer exhaustion at 256 messages) because the test always exited early before cancellation could race. After fixing #553 the 1-off became reproducible.
Fix
Move Volatile.Write before the delay so lastSeqWritten is always ≥ the last written seq:
// Fixed: record immediately after write, then delay
Volatile.Write(ref lastSeqWritten, seq);
await Task.Delay(50, streamCts.Token);
Related
Summary
ContinuousStream_SurvivesRapidTopologyChurninTurbulenceTests.cscan fail with a 1-off assertion error when the test is cancelled while the writer is in its inter-message delay. This is a test-only ordering bug — no product code changes needed.Root cause
Volatile.Write(ref lastSeqWritten, seq)was placed afterTask.Delay(50):When
streamCts.Cancel()fires during the 50ms delay, theWriteAsyncforseqalready completed successfully butlastSeqWrittenwas never updated. The assertion:fails by 1 —
sorted.Count(which includes the already-written-and-delivered message) exceedslastSeqWritten + 1.This bug was masked by #553 (buffer exhaustion at 256 messages) because the test always exited early before cancellation could race. After fixing #553 the 1-off became reproducible.
Fix
Move
Volatile.Writebefore the delay solastSeqWrittenis always ≥ the last written seq:Related