2

I have some client code that communicates with a server using streams that I want to test. I want to test that the client sends the correct commands to the server.

To do this I have created a Connect method that takes two streams: one the client reads from and one the client writes to. I can then supply a MemoryStream to both, and read the commands sent off the MemoryStream that the client writes to.

My problem is that I need to code a method that requires those two streams to be the same. I have also noticed, that the only time I need to use two different streams, is when I want to test the client. Therefore I want to refactor the Connect method to take only one stream, which will both be written to and read from.

I cant pass a MemoryStream to this method, since the write method writes to some internal byte array, from which the read method also reads from. If I give this to a client, it will be able to read the commands it just sent!

Therefore I would ask what stream setup can do this? I simply want a stream that reads from one source, but writes to another. I would need access to both sources, so that the "server", which is my test, can send the correct responses to commands, and read the commands the client sent.

In essence I need two streams: S1 and S2. One stream is given to the client, the other to the server.
S1's read method is "connected" to S2's write method => S1 reads what S2 writes.
S1's write method is "connected" to S2's read method => S2 reads what S1 writes.
This is exactly the normal case when a client communicates with a server. Here I just want to do it in memory. How can I code this?

2
  • I don't think you need the streams to be the same -- don't you just need the contents of the streams to be the same? That is, if you read them off, do they have the same content? Dec 23, 2010 at 14:42
  • @Tim. I need to run SSL on top of the streams. Since SslStream only takes one stream, I really have to combine two streams into one, or only have one. Sorry if that was a bit unclear.
    – foens
    Dec 23, 2010 at 15:18

3 Answers 3

5

Why not abstract the sending of the commands from the transport of the commands?

By that I mean you have one class that decides what commands need to be sent and another that dictates the means by which they're sent. That way you can test that the command class is generating the correct commands (in the correct order) by mocking away the transport class without ever having to worry about the implementation detail of streams.

The implementation of transport class would then just take the data given to it and write it directly to whatever stream it would normally without you having to think about making it work with the command class tests. That class would be harder to test, but by that point you've established the correct commands are being generated so it's integration testing to ensure they're being written successfully.

3
  • I really do not like to add the extra complexity, since the only time the commands are not to be transported on streams, is when I test them. I know it would be the right way to do unit testing, I just do not want the added complexity for other readers. It has to be easy to read for everyone, also people not knowing much about strategy patterns.
    – foens
    Dec 23, 2010 at 15:06
  • I'd argue that it's easier to read tbh as you've clearly separated out that there's one place commands are generated and one place that they're transported. Also future maintainers should be able to more clearly spot and fix errors by determining whether it's to do with generating or transporting...
    – Dave D
    Dec 23, 2010 at 15:09
  • I really think it is the right way go! But I went the "implemented a combined stream" way... +1 for the answer though.
    – foens
    Dec 23, 2010 at 15:46
3

You can create a subclass of Stream that takes two other streams in the constructor and then override the Read and Write methods to read and write in the correct streams.

Be sure to read the Notes to Implementers section on the MSDN page for the Stream class.

2
  • I have thought much about going this way. But I am doing unit testing. Unit testing needs to be simple. After all, unit tests is there to make sure things work. If I have to implement a Stream class, it is not simple - I would need to test that class too. I really hoped such stream existed in the framework.
    – foens
    Dec 23, 2010 at 15:08
  • Anyways. I ended up doing this. It seemed easier and would not force me to change my implementation inside the class I am testing. :)
    – foens
    Dec 23, 2010 at 15:44
0

You can create a bidirectional stream which encapsulates two memory streams. You read from one and write to another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.