I’ve recently been given a chance to write a simple tcp-client for a project of mine, and due to my huge ignorance with tcp/ip, I have a hard time getting it to work properly.
I find that sometimes I can get weird connection issue when the connection refused as the tcp server is down or sometimes I might get an exception when calling receive.
As the tcp-server is a black box and we have no access to it, I’m wondering in this situation, what’s the best way to write unit tests for this?
I’m thinking either I should write a tcp-server and make it return certain inputs as what I would expect from the real server or just mock the functions related to return ideal situation data.
I just don’t like the fact that sometimes I might get weird connection/receive issue and I would like to know how to properly write unit test which I can reuse/extend in the future to ensure that everything that work before should still be working in case my code or their code change.
thanks
You should have two kinds of tests:
receive connections. Create this server that has the bare minimum
functionality and use it to test how your client behaves. Using a
simple TCP server you can test how your client send and receive
messages and how it connects and disconnect from the server. Another
useful test is how several clients connect and send messages to the
server.
won’t be able to send or receive messages but you can test the
client’s internal logic – how it behaves if two messages with
arrive, re-sending in case of errors etc.
Using both kinds of tests you should be able to cover most of your client’s functioanlity