Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d8e222d

Browse files
committed
Limit ReadAsyncCompletesIfFlushAsyncCanceledMidFlush iteration count
1 parent bee0e9d commit d8e222d

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/System.IO.Pipelines/tests/FlushAsyncCancellationTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,16 @@ public void ReadAsyncCompletesIfFlushAsyncCanceledMidFlush()
333333
// and FlushAsync is cancelled while the method is running
334334
Pipe = new Pipe();
335335
var resetEvent = new ManualResetEvent(false);
336-
336+
var iterations = 0;
337+
var cancellations = 0;
337338
var cancellationTokenSource = new CancellationTokenSource();
338339
var writer = Task.Run(async () =>
339340
{
340-
var cancellations = 0;
341-
while (cancellations < 20)
341+
// We are limiting iteration count because on slower machines we are not able to
342+
// reproduce race conditions enough times during the test
343+
while (cancellations < 20 && iterations < 2_000_000)
342344
{
345+
iterations++;
343346
try
344347
{
345348
// We want reader to be awaiting
@@ -353,7 +356,6 @@ public void ReadAsyncCompletesIfFlushAsyncCanceledMidFlush()
353356
cancellationTokenSource = new CancellationTokenSource();
354357
continue;
355358
}
356-
357359
await Pipe.Writer.FlushAsync(cancellationTokenSource.Token);
358360
}
359361
catch (OperationCanceledException)
@@ -379,7 +381,6 @@ public void ReadAsyncCompletesIfFlushAsyncCanceledMidFlush()
379381
}
380382

381383
var result = await readTask;
382-
383384
if (result.Buffer.IsEmpty)
384385
{
385386
return;
@@ -401,6 +402,7 @@ public void ReadAsyncCompletesIfFlushAsyncCanceledMidFlush()
401402
});
402403

403404
Assert.True(Task.WaitAll(new [] { writer, reader, canceller }, TimeSpan.FromSeconds(30)), "Reader was not completed in reasonable time");
405+
Assert.True(cancellations > 0);
404406
}
405407
}
406408

0 commit comments

Comments
 (0)