views:

50

answers:

3

As the headline says, how would you test a client/server application, that is written in C/C++, that talks through a protocol over a network? Im a bit confused on how to do this. I have thought about making some mocking, but I have never tried mocking, so I dont know if this is the best way. How should I do this? I have written many unit tests, but never tried to test something that interact over a network.

A: 

I use the command pattern in the unit test driver (client) to send test commands to the server. The advantage of this is that the test is coded in one place.

Example for testing a request timeout:

Client sends a sleep command to server and then the request. The request times out and the test case is passed.

Peter G.
I dont know if I understand this, but you say that you have an unit test, that sends messages to the server and then see if it gets the right answer back? That sounds like a fairly good way of doing things in my project.
mslot
Yes, more precisely the unit test sends two kinds of messages to server. Messages of the first kind establish the test conditions (e.g. sleep to trigger a timeout) and the second kind are the actual requests to test.
Peter G.
A: 

Typically you'll want to use mocking to verify that each side reacts as required to messages from the other side (e.g., that when the client receives a response from the server that it processes that response correctly).

To test the network functionality itself, you can test both running on the same machine, and you can run one (or both) inside a virtual machine. If you have two network adapters, you can even dedicate each to a virtual machine so the network traffic actually goes out one, to a switch/router, and comes back in the other (particularly useful when/if you want to capture and verify packets).

Jerry Coffin
I havent thought of the virtual machine!! Thanks for reminding me about that,
mslot
A: 

I have some client/server code that I unit test through the loopback address. I use some mocks when I have to test error conditions. So, I test with the real code when I can and the mocks when I need to trigger very specific conditions.

Chad Simpkins
You say that you mock when you need to trigger special conditions. Can you give an example on this?
mslot
The most common reason is triggering error conditions. If I want to test my client code when the server reports an error I will make a mock to return the error code when it is too difficult to get the server to do it. Also, it is hard to trigger socket errors on the loopback, so I will mock a socket connection when I want to make sure I handle socket error correctly.
Chad Simpkins