This question already has an answer here:
I am writing a library which connects a server with a client to synchronize two instances of an object. To do this, within the (Portable) library, I use generic Streams, and two interfaces, IMessageServer and IMessageClient, which are to be implemented in an application that uses this library. I have a demo app that I've been using to test, where I use a TCP connection with a NetworkStream, and everything works great. In this method:
private static byte[] GetDataFromStream(int dataLength, Stream stream)
{
var data = new byte[dataLength];
var toRead = dataLength;
var offset = 0;
while (toRead > 0)
{
var read = stream.Read(data, offset, toRead);
if (read == 0)
{
throw new ServerSocketConnectionException("Server Socket Closed");
}
toRead -= read;
offset += read;
}
return data;
}
The line that calls stream.Read(...) blocks execution when I use a NetworkStream. Now, I am writing some units tests, and I thought it would be simple to use a MemoryStream, which does not require a network connection and such. In terms of my library, it is incredibly simple because of the interface design and all of that, but it seems like stream.Read(..) does NOT block for a MemoryStream. This library, unfortunately, relies on that call blocking.
My question, then, is: is there a better way to unit test when they involve NetworkStreams? Or is there an improvement that could be made to the GetDataFromStream(...) method? Is using a MemoryStream just not going to work?
Aucun commentaire:
Enregistrer un commentaire