I am trying to test my basic System.IO.Pipelines setup where the memory from the writer will be filled from a TcpClient's stream (ReadAsync
-method) similar to this code.
Memory<byte> memory = writer.GetMemory(minimumBufferSize);
int read = await stream.ReadAsync(memory);
The NetworkStream
from the TcpClient
will prevent the continuation if it's waiting for data / if there is no data to read. I am using interfaces of all network-related parts (Stream, TcpClient, ...) to mock them within unit-tests. Within my tests I use MemoryStreams within my mocks
streamMock
.Setup(m => m.ReadAsync(It.IsAny<Memory<byte>>(), It.IsAny<CancellationToken>()))
.Returns((Memory<byte> memory, CancellationToken token) =>
{
return memStream.ReadAsync(memory, cancelToken);
}
The problem is, that the used MemoryStream will always continue instantly with 0 bytes read, as ReadAsync
is only a asynchronous wrapper around Read
. Is it possible to get a similar NetworkStream
-behaviour described above within my unit-tests?