@@ -29,12 +29,6 @@ public sealed class ExpireCoordinatorTests
2929 /// <summary>The values forwarded by the active-source re-arming test.</summary>
3030 private static readonly int [ ] ExpectedActiveValues = [ 0 , 1 , 2 , 3 , 4 ] ;
3131
32- /// <summary>Timeout used while waiting for background work in this test.</summary>
33- private static readonly TimeSpan WaitTimeout = TimeSpan . FromSeconds ( 5 ) ;
34-
35- /// <summary>How long the superseded timeout is given to reach the observer before the invariant is checked.</summary>
36- private static readonly TimeSpan RaceSettleDelay = TimeSpan . FromMilliseconds ( 50 ) ;
37-
3832 /// <summary>Verifies the timeout re-arms on each value so an active source never expires.</summary>
3933 /// <returns>A task representing the asynchronous operation.</returns>
4034 [ Test ]
@@ -169,63 +163,23 @@ public async Task ValueArrivingInsideTheWindowIsForwardedWhileTheTimeoutIsStillU
169163 await Assert . That ( errors . Count ) . IsEqualTo ( 0 ) ;
170164 }
171165
172- /// <summary>Verifies an in-flight value wins the race and suppresses the superseded timeout .</summary>
166+ /// <summary>Verifies a timeout that becomes due while a value is in flight is suppressed by that value .</summary>
173167 /// <returns>A task representing the asynchronous operation.</returns>
174168 [ Test ]
175169 public async Task TimeoutDoesNotEnterObserverWhileOnNextIsInFlight ( )
176170 {
177171 VirtualClock clock = new ( DateTimeOffset . UnixEpoch ) ;
178172 Signal < int > source = new ( ) ;
179- BlockingObserver observer = new ( ) ;
173+ ReentrantTimeoutObserver observer = new ( clock , TimeSpan . FromTicks ( One ) ) ;
180174 using var subscription = source . Expire ( TimeSpan . FromTicks ( One ) , clock ) . Subscribe ( observer ) ;
181175
182- // Dedicated threads rather than the pool: the observer parks its caller inside OnNext until this
183- // test releases it, so on the pool that notification holds a worker while the timeout waits behind
184- // it in the queue. A saturated pool then starves the very interleaving under test.
185- var onNextFinished = RunOnDedicatedThread ( ( ) => source . OnNext ( One ) ) ;
186- await observer . OnNextEntered . Task . WaitAsync ( WaitTimeout ) . ConfigureAwait ( false ) ;
187-
188- var timeoutFinished = RunOnDedicatedThread ( ( ) => clock . AdvanceBy ( TimeSpan . FromTicks ( One ) ) ) ;
189- await Task . Delay ( RaceSettleDelay ) . ConfigureAwait ( false ) ;
176+ source . OnNext ( One ) ;
190177
191178 await Assert . That ( observer . ErrorEnteredDuringOnNext ) . IsFalse ( ) ;
192-
193- observer . ReleaseOnNext . Set ( ) ;
194- await onNextFinished . WaitAsync ( WaitTimeout ) . ConfigureAwait ( false ) ;
195- await timeoutFinished . WaitAsync ( WaitTimeout ) . ConfigureAwait ( false ) ;
196-
197- // Timeout may be observed after OnNext exits depending on scheduler timing.
198- // The invariant required here is that OnError never re-enters while OnNext is active.
199- await Assert . That ( observer . Errors ) . IsLessThanOrEqualTo ( One ) ;
179+ await Assert . That ( observer . Errors ) . IsEqualTo ( 0 ) ;
200180 await Assert . That ( observer . Values ) . IsEqualTo ( One ) ;
201181 }
202182
203- /// <summary>
204- /// Runs work on its own thread and reports when it finished. Used where the work blocks for the duration of
205- /// the scenario, which the thread pool cannot absorb without the risk of the work never being given a thread.
206- /// </summary>
207- /// <param name="work">The work to run.</param>
208- /// <returns>A task that completes when the work has returned.</returns>
209- private static Task RunOnDedicatedThread ( Action work )
210- {
211- TaskCompletionSource finished = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
212- Thread thread = new ( ( ) =>
213- {
214- try
215- {
216- work ( ) ;
217- _ = finished . TrySetResult ( ) ;
218- }
219- catch ( Exception error )
220- {
221- _ = finished . TrySetException ( error ) ;
222- }
223- } ) { IsBackground = true } ;
224-
225- thread . Start ( ) ;
226- return finished . Task ;
227- }
228-
229183 /// <summary>
230184 /// A sequencer that accepts scheduled work and never dispatches it, modelling a thread-pool sequencer whose pool
231185 /// is saturated: the timer becomes due on the clock, but no thread is free to run the callback. Its clock is
@@ -264,44 +218,23 @@ private sealed class UndispatchedSequencer(DateTimeOffset start) : ISequencer
264218 public void Schedule ( IWorkItem item , long dueTimestamp ) => Pending ++ ;
265219 }
266220
267- /// <summary>Observer that blocks source value handling so timeout serialization can be observed.</summary>
268- [ System . Diagnostics . CodeAnalysis . SuppressMessage (
269- "Design" ,
270- "SST2315:A type that owns a disposable should be disposable" ,
271- Justification =
272- "Test double that owns a ManualResetEventSlim used to gate OnNext so the test can observe timeout "
273- + "serialization. Its lifetime is the test's; the test process owns and releases it, so it is deliberately "
274- + "not IDisposable." ) ]
275- private sealed class BlockingObserver : IObserver < int >
221+ /// <summary>Observer that makes the armed timeout due from inside <see cref="OnNext"/>, so the timeout is
222+ /// dispatched while the value is still in flight.</summary>
223+ /// <param name="clock">The clock that dispatches due work inline.</param>
224+ /// <param name="dueTime">The amount to advance the clock by so the armed timeout becomes due.</param>
225+ private sealed class ReentrantTimeoutObserver ( VirtualClock clock , TimeSpan dueTime ) : IObserver < int >
276226 {
277- /// <summary>Non-zero while <see cref="OnNext"/> is active. Written by the notifying thread and read by
278- /// the timeout thread, so the two must not race on a plain field.</summary>
279- private int _isInOnNext ;
280-
281- /// <summary>Non-zero once an error arrived while <see cref="OnNext"/> was active. The test reads this
282- /// while both threads are still running, so the write has to be published rather than merely made.</summary>
283- private int _errorEnteredDuringOnNext ;
284-
285- /// <summary>The number of forwarded values.</summary>
286- private int _values ;
287-
288- /// <summary>The number of forwarded errors.</summary>
289- private int _errors ;
290-
291- /// <summary>Gets the task completed when <see cref="OnNext"/> is entered.</summary>
292- public TaskCompletionSource OnNextEntered { get ; } = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
293-
294- /// <summary>Gets the event released by the test to unblock <see cref="OnNext"/>.</summary>
295- public ManualResetEventSlim ReleaseOnNext { get ; } = new ( ) ;
296-
297227 /// <summary>Gets the number of forwarded values.</summary>
298- public int Values => Volatile . Read ( ref _values ) ;
228+ public int Values { get ; private set ; }
299229
300230 /// <summary>Gets the number of forwarded errors.</summary>
301- public int Errors => Volatile . Read ( ref _errors ) ;
231+ public int Errors { get ; private set ; }
232+
233+ /// <summary>Gets a value indicating whether an error arrived while <see cref="OnNext"/> was active.</summary>
234+ public bool ErrorEnteredDuringOnNext { get ; private set ; }
302235
303- /// <summary>Gets a value indicating whether an error entered while <see cref="OnNext"/> was active.</summary>
304- public bool ErrorEnteredDuringOnNext => Volatile . Read ( ref _errorEnteredDuringOnNext ) != 0 ;
236+ /// <summary>Gets or sets a value indicating whether <see cref="OnNext"/> is active.</summary>
237+ private bool IsInOnNext { get ; set ; }
305238
306239 /// <inheritdoc/>
307240 public void OnCompleted ( )
@@ -311,22 +244,21 @@ public void OnCompleted()
311244 /// <inheritdoc/>
312245 public void OnError ( Exception error )
313246 {
314- if ( Volatile . Read ( ref _isInOnNext ) != 0 )
247+ if ( IsInOnNext )
315248 {
316- Volatile . Write ( ref _errorEnteredDuringOnNext , 1 ) ;
249+ ErrorEnteredDuringOnNext = true ;
317250 }
318251
319- _ = Interlocked . Increment ( ref _errors ) ;
252+ Errors ++ ;
320253 }
321254
322255 /// <inheritdoc/>
323256 public void OnNext ( int value )
324257 {
325- _ = Interlocked . Increment ( ref _values ) ;
326- Volatile . Write ( ref _isInOnNext , 1 ) ;
327- OnNextEntered . SetResult ( ) ;
328- _ = ReleaseOnNext . Wait ( WaitTimeout ) ;
329- Volatile . Write ( ref _isInOnNext , 0 ) ;
258+ Values ++ ;
259+ IsInOnNext = true ;
260+ clock . AdvanceBy ( dueTime ) ;
261+ IsInOnNext = false ;
330262 }
331263 }
332264}
0 commit comments