Skip to content

Tweak StreamConformanceTests for cancellation #44342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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