public async Task WriteAsyncThrowsDuringMultiSegmentWriteCompleteReturnsAllMemory() { using (var pool = new DisposeTrackingBufferPool()) { var stream = new ThrowAfterNWritesStream(1); var options = new StreamPipeWriterOptions(pool); PipeWriter writer = PipeWriter.Create(stream, options); writer.WriteEmpty(pool.MaxBufferSize); writer.WriteEmpty(pool.MaxBufferSize); writer.WriteEmpty(pool.MaxBufferSize); Assert.Equal(3, pool.CurrentlyRentedBlocks); Assert.Equal(0, pool.DisposedBlocks); await Assert.ThrowsAsync <InvalidOperationException>(async() => await writer.FlushAsync()); Assert.Equal(2, pool.CurrentlyRentedBlocks); Assert.Equal(1, pool.DisposedBlocks); writer.Complete(); Assert.Equal(0, writer.UnflushedBytes); Assert.Equal(0, pool.CurrentlyRentedBlocks); Assert.Equal(3, pool.DisposedBlocks); } }
public async Task MultiSegmentWritesUntilFailure() { using (var pool = new TestMemoryPool()) { var pipe = new Pipe(s_testOptions); pipe.Writer.WriteEmpty(pool.MaxBufferSize); pipe.Writer.WriteEmpty(pool.MaxBufferSize); pipe.Writer.WriteEmpty(pool.MaxBufferSize); await pipe.Writer.FlushAsync(); pipe.Writer.Complete(); var stream = new ThrowAfterNWritesStream(2); try { await pipe.Reader.CopyToAsync(stream); Assert.True(false, $"CopyToAsync should have failed, wrote {stream.Writes} times."); } catch (InvalidOperationException) { } Assert.Equal(2, stream.Writes); ReadResult result = await pipe.Reader.ReadAsync(); Assert.Equal(4096, result.Buffer.Length); pipe.Reader.Complete(); } }
public async Task MultiSegmentWritesUntilFailure() { using (var pool = new TestMemoryPool()) { var pipe = new Pipe(new PipeOptions(pool: pool, readerScheduler: PipeScheduler.Inline)); pipe.Writer.WriteEmpty(4096); pipe.Writer.WriteEmpty(4096); pipe.Writer.WriteEmpty(4096); await pipe.Writer.FlushAsync(); pipe.Writer.Complete(); var stream = new ThrowAfterNWritesStream(2); await Assert.ThrowsAsync <InvalidOperationException>(() => pipe.Reader.CopyToAsync(stream)); ReadResult result = await pipe.Reader.ReadAsync(); Assert.Equal(4096, result.Buffer.Length); pipe.Reader.Complete(); } }
public async Task MultiSegmentWritesUntilFailure() { using (var pool = new DisposeTrackingBufferPool()) { var pipe = new Pipe(new PipeOptions(pool, readerScheduler: PipeScheduler.Inline, useSynchronizationContext: false)); pipe.Writer.WriteEmpty(4096); pipe.Writer.WriteEmpty(4096); pipe.Writer.WriteEmpty(4096); await pipe.Writer.FlushAsync(); pipe.Writer.Complete(); Assert.Equal(3, pool.CurrentlyRentedBlocks); var stream = new ThrowAfterNWritesStream(2); try { await pipe.Reader.CopyToAsync(stream); Assert.True(false, $"CopyToAsync should have failed, wrote {stream.Writes} times."); } catch (InvalidOperationException) { } Assert.Equal(2, stream.Writes); Assert.Equal(1, pool.CurrentlyRentedBlocks); Assert.Equal(2, pool.DisposedBlocks); ReadResult result = await pipe.Reader.ReadAsync(); Assert.Equal(4096, result.Buffer.Length); pipe.Reader.Complete(); Assert.Equal(0, pool.CurrentlyRentedBlocks); Assert.Equal(3, pool.DisposedBlocks); } }
public async Task ThrowingFromStreamCallsAdvanceToWithStartOfLastReadResult(int throwAfterNWrites) { var wrappedPipeReader = new TestPipeReader(PipeReader); var stream = new ThrowAfterNWritesStream(throwAfterNWrites); Task task = wrappedPipeReader.CopyToAsync(stream); Pipe.Writer.WriteEmpty(10); await Pipe.Writer.FlushAsync(); // Write twice for the test case where the stream throws on the second write. Pipe.Writer.WriteEmpty(10); await Pipe.Writer.FlushAsync(); await Assert.ThrowsAsync <InvalidOperationException>(() => task); SequencePosition startPosition = wrappedPipeReader.LastReadResult.Buffer.Start; Assert.NotNull(startPosition.GetObject()); Assert.True(startPosition.Equals(wrappedPipeReader.LastConsumed)); Assert.True(startPosition.Equals(wrappedPipeReader.LastExamined)); }