1

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?

0xDECAFBAD
  • 627
  • 1
  • 8
  • 21
  • 2
    What is the behavior you want to simulate? If you want to make the MemoryStream wait when it has no bytes (therefore to wait until you feed it some bytes), a possible solution can be to use a wrapper around MemoryStream and to override Read or ReadAsync methods and to check and sleep until the buffer has some bytes. The call would still be blocking though. Or you can wait for an async method inside AsyncRead which fills your memory stream – Oguz Ozgul Mar 13 '20 at 22:04
  • That is exactly what I've been / I've was looking for. "Or you can wait for an async method inside AsyncRead which fills your memory stream" - I didn't thought of ManualResetEvent in the first place, but it was the first thing I've thought of reading your comment - It's working now. If you provide something as solution i can check it as solved (rather than the comment) – 0xDECAFBAD Mar 13 '20 at 22:31
  • The comment is fine :) Your problem being solved is what matters. – Oguz Ozgul Mar 13 '20 at 22:33
  • There's nothing built-in for this, no. You'd need to make your own `Stream`-derived class. – Stephen Cleary Mar 14 '20 at 03:26
  • If anyone has the same requirements to implement such a `Stream`-derived class: I can highly recommend taking a look at @StephenCleary's AsyncEx-library - it was really helpful – 0xDECAFBAD Mar 14 '20 at 16:44

0 Answers0