public async Task ClientCanSendReadOnlyStreamToServer(bool orderedArguments) { var ms = new MemoryStream(MemoryBuffer); var readOnlyStream = new OneWayStreamWrapper(ms, canRead: true); int bytesReceived; if (orderedArguments) { bytesReceived = await this.clientRpc.InvokeWithCancellationAsync <int>( nameof(Server.AcceptReadableStream), new object[] { ExpectedFileName, readOnlyStream }, this.TimeoutToken); } else { bytesReceived = await this.clientRpc.InvokeWithParameterObjectAsync <int>( nameof(Server.AcceptReadableStream), new { fileName = ExpectedFileName, content = readOnlyStream }, this.TimeoutToken); } Assert.Equal(MemoryBuffer.Length, bytesReceived); // Assert that the client-side stream is closed, since the server closed their side. await this.AssertStreamClosesAsync(ms); }
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 void Position_NonSeekableStream() { using var nonSeekableWrapper = new OneWayStreamWrapper(this.underlyingStream, canRead: true); using Stream? stream = nonSeekableWrapper.ReadSlice(10); Assert.Equal(0, stream.Position); Assert.Throws <NotSupportedException>(() => stream.Position = 3); Assert.Equal(0, stream.Position); stream.ReadByte(); Assert.Equal(1, stream.Position); }
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); }
public async Task ClientCanSendWriteOnlyStreamToServer(bool orderedArguments) { (Stream, Stream)duplexStream = FullDuplexStream.CreatePair(); var writeOnlyStream = new OneWayStreamWrapper(duplexStream.Item2, canWrite: true); int bytesToReceive = MemoryBuffer.Length - 1; if (orderedArguments) { await this.clientRpc.InvokeWithCancellationAsync( nameof(Server.AcceptWritableStream), new object[] { writeOnlyStream, bytesToReceive }, this.TimeoutToken); } else { await this.clientRpc.InvokeWithParameterObjectAsync( nameof(Server.AcceptWritableStream), new { lengthToWrite = bytesToReceive, content = writeOnlyStream }, this.TimeoutToken); } // Read all that the server wanted us to know, and verify it. byte[] buffer = new byte[bytesToReceive + 1]; int receivedBytes = 0; while (receivedBytes < bytesToReceive) { int count = await duplexStream.Item1.ReadAsync(buffer, receivedBytes, buffer.Length - receivedBytes); receivedBytes += count; } Assert.Equal(MemoryBuffer.Take(bytesToReceive), buffer.Take(bytesToReceive)); // Assert that the client-side stream is closed, since the server closed their side. await this.AssertStreamClosesAsync(duplexStream.Item2); }