Skip to content

Commit

Permalink
Tweak StreamConformanceTests for cancellation (#44342)
Browse files Browse the repository at this point in the history
- Avoid unnecessary timers
- Separate tests for precancellation, ReadAsync(byte[], ...) cancellation, and ReadAsync(Memory, ...) cancellation
  • Loading branch information
stephentoub authored Nov 6, 2020
1 parent 640f0f7 commit 54d27b2
Showing 1 changed file with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,23 +532,30 @@ protected async Task ValidatePrecanceledOperations_ThrowsCancellationException(S
}
}

protected async Task ValidateCancelableReads_AfterInvocation_ThrowsCancellationException(Stream stream)
protected async Task ValidateCancelableReadAsyncTask_AfterInvocation_ThrowsCancellationException(Stream stream)
{
if (!stream.CanRead || !FullyCancelableOperations)
{
return;
}

CancellationTokenSource cts;

cts = new CancellationTokenSource(1);
await AssertCanceledAsync(cts.Token, () => stream.ReadAsync(new byte[1], 0, 1, cts.Token));
var cts = new CancellationTokenSource();
Task<int> t = stream.ReadAsync(new byte[1], 0, 1, cts.Token);
cts.Cancel();
await AssertCanceledAsync(cts.Token, () => t);
}

if (UsableAfterCanceledReads)
protected async Task ValidateCancelableReadAsyncValueTask_AfterInvocation_ThrowsCancellationException(Stream stream)
{
if (!stream.CanRead || !FullyCancelableOperations)
{
cts = new CancellationTokenSource(1);
await AssertCanceledAsync(cts.Token, async () => { await stream.ReadAsync(new Memory<byte>(new byte[1]), cts.Token); });
return;
}

var cts = new CancellationTokenSource();
Task<int> t = stream.ReadAsync(new byte[1], cts.Token).AsTask();
cts.Cancel();
await AssertCanceledAsync(cts.Token, () => t);
}

protected async Task WhenAllOrAnyFailed(Task task1, Task task2)
Expand Down Expand Up @@ -1624,17 +1631,34 @@ public virtual async Task Disposed_ThrowsObjectDisposedException()
}

[Fact]
public virtual async Task ReadWriteAsync_Canceled_ThrowsOperationCanceledException()
public virtual async Task ReadWriteAsync_PrecanceledOperations_ThrowsCancellationException()
{
using StreamPair streams = await CreateConnectedStreamsAsync();

foreach (Stream stream in streams)
{
await ValidatePrecanceledOperations_ThrowsCancellationException(stream);
await ValidateCancelableReads_AfterInvocation_ThrowsCancellationException(stream);
}
}

[Fact]
public virtual async Task ReadAsync_CancelPendingTask_ThrowsCancellationException()
{
using StreamPair streams = await CreateConnectedStreamsAsync();
(Stream writeable, Stream readable) = GetReadWritePair(streams);

await ValidateCancelableReadAsyncTask_AfterInvocation_ThrowsCancellationException(readable);
}

[Fact]
public virtual async Task ReadAsync_CancelPendingValueTask_ThrowsCancellationException()
{
using StreamPair streams = await CreateConnectedStreamsAsync();
(Stream writeable, Stream readable) = GetReadWritePair(streams);

await ValidateCancelableReadAsyncValueTask_AfterInvocation_ThrowsCancellationException(readable);
}

[Fact]
public virtual async Task ReadWriteByte_Success()
{
Expand Down

0 comments on commit 54d27b2

Please sign in to comment.