Skip to content
Merged
Changes from 1 commit
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 @@ -62,19 +62,22 @@ public async Task VerifyComputeHashAsync(int size)
[ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
public async Task ComputeHashAsync_SupportsCancellation()
{
using (CancellationTokenSource cancellationSource = new CancellationTokenSource(100))
using (PositionValueStream stream = new SlowPositionValueStream(10000))
using (CancellationTokenSource cancellationSource = new CancellationTokenSource())
using (PositionValueStream stream = new SelfCancellingStream(10000, cancellationSource))
using (HashAlgorithm hash = new SummingTestHashAlgorithm())
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => hash.ComputeHashAsync(stream, cancellationSource.Token));

Assert.True(cancellationSource.IsCancellationRequested);
}
}

[Fact]
public void ComputeHashAsync_Disposed()
{
using (PositionValueStream stream = new SlowPositionValueStream(10000))
using (CancellationTokenSource cancellationSource = new CancellationTokenSource())
using (PositionValueStream stream = new SelfCancellingStream(10000, cancellationSource))
using (HashAlgorithm hash = new SummingTestHashAlgorithm())
{
hash.Dispose();
Expand All @@ -85,6 +88,10 @@ public void ComputeHashAsync_Disposed()
// Not returning or awaiting the Task, it never got created.
hash.ComputeHashAsync(stream);
});

// If SelfCancellingStream.Read (or ReadAsync) was called it will trip cancellation,
// so use that as a signal for whether or not the stream was ever read from.
Assert.False(cancellationSource.IsCancellationRequested, "Stream.Read was invoked");
}
}

Expand Down Expand Up @@ -123,15 +130,19 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
// implementations by verifying the right value is produced.
}

private class SlowPositionValueStream : PositionValueStream
private class SelfCancellingStream : PositionValueStream
{
public SlowPositionValueStream(int totalCount) : base(totalCount)
private readonly CancellationTokenSource _cancellationSource;

public SelfCancellingStream(int totalCount, CancellationTokenSource cancellationSource)
: base(totalCount)
{
_cancellationSource = cancellationSource;
}

public override int Read(byte[] buffer, int offset, int count)
{
System.Threading.Thread.Sleep(1000);
_cancellationSource.Cancel(throwOnFirstException: true);
return base.Read(buffer, offset, count);
}
}
Expand Down