public async Task UsePipe_Stream_WriteOnlyStream() { var streamPair = FullDuplexStream.CreatePair(); var writeOnlyStream = new OneWayStreamWrapper(streamPair.Item1, canWrite: true); var duplexPipe = writeOnlyStream.UsePipe(); // Verify that reading isn't allowed. await Assert.ThrowsAsync <InvalidOperationException>(async() => await duplexPipe.Input.ReadAsync(this.TimeoutToken)); byte[] expected = new byte[] { 1, 2, 3 }; await duplexPipe.Output.WriteAsync(expected, this.TimeoutToken); duplexPipe.Output.Complete(); int totalBytesRead = 0; byte[] readBytes = new byte[10]; int bytesRead; do { bytesRead = await streamPair.Item2.ReadAsync(readBytes, totalBytesRead, readBytes.Length - totalBytesRead, this.TimeoutToken); this.Logger.WriteLine("Read {0} bytes", bytesRead); totalBytesRead += bytesRead; }while (bytesRead > 0); Assert.Equal(expected, readBytes.Take(totalBytesRead)); // Complete writing and verify stream closed. duplexPipe.Output.Complete(); await this.AssertStreamClosesAsync(streamPair.Item1); }
public async Task UsePipe_Stream_ReadOnlyStream() { var streamPair = FullDuplexStream.CreatePair(); byte[] expected = new byte[] { 1, 2, 3 }; await streamPair.Item2.WriteAsync(expected, 0, expected.Length, this.TimeoutToken); await streamPair.Item2.FlushAsync(this.TimeoutToken); var readOnlyStream = new OneWayStreamWrapper(streamPair.Item1, canRead: true); var duplexPipe = readOnlyStream.UsePipe(); var readResult = await duplexPipe.Input.ReadAsync(this.TimeoutToken); Assert.Equal(expected, readResult.Buffer.ToArray()); Assert.Throws <InvalidOperationException>(() => duplexPipe.Output.GetSpan()); // Complete reading and verify stream closed. duplexPipe.Input.Complete(); await this.AssertStreamClosesAsync(streamPair.Item1); }