ClearCancellationToken();
}
- ReadOnlySequence<byte> buffer = _readHead == null ? default : GetCurrentReadOnlySequence();
+ ReadOnlySequence<byte> buffer = GetCurrentReadOnlySequence();
result = new ReadResult(buffer, isCancellationRequested, _isStreamCompleted);
return true;
private ReadOnlySequence<byte> GetCurrentReadOnlySequence()
{
- Debug.Assert(_readHead != null && _readTail != null);
- return new ReadOnlySequence<byte>(_readHead, _readIndex, _readTail, _readTail.End);
+ // If _readHead is null then _readTail is also null
+ return _readHead is null ? default : new ReadOnlySequence<byte>(_readHead, _readIndex, _readTail!, _readTail!.End);
}
private void AllocateReadTail(int? minimumSize = null)
reader.Complete();
}
- [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
- public async Task ReadCanBeCanceledViaCancelPendingReadWhenReadIsAsync()
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task ReadCanBeCanceledViaCancelPendingReadWhenReadAsync(bool useZeroByteReads)
{
var stream = new CancelledReadsStream();
- PipeReader reader = PipeReader.Create(stream);
+ PipeReader reader = PipeReader.Create(stream, new StreamPipeReaderOptions(useZeroByteReads: useZeroByteReads));
ValueTask<ReadResult> task = reader.ReadAsync();
reader.Complete();
}
+ [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task ReadCanBeCanceledViaCancelPendingReadWhenReadAtLeastAsync(bool useZeroByteReads)
+ {
+ var stream = new CancelledReadsStream();
+ PipeReader reader = PipeReader.Create(stream, new StreamPipeReaderOptions(useZeroByteReads: useZeroByteReads));
+
+ ValueTask<ReadResult> task = reader.ReadAtLeastAsync(1);
+
+ reader.CancelPendingRead();
+
+ stream.WaitForReadTask.TrySetResult(null);
+
+ ReadResult readResult = await task;
+ Assert.True(readResult.IsCanceled);
+ reader.Complete();
+ }
+
[Fact]
public async Task ReadAsyncReturnsCanceledIfCanceledBeforeRead()
{