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
If you want to test a TCP service on your local computer, use the IP address 127.0. 0.1. This is the reserved "local host" address.
Press the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.
You should have two kinds of tests:
Using both kinds of tests you should be able to cover most of your client's functioanlity
For unit testing, I'd create a simple socket server (kicked off when the UT starts) just for the purpose of testing the client. If you make it simple and standalone you'll reduce hassles running the test. You may also be able to use tools like ncat to facilitate this.
However having said that there may be problems that might be harder for the UT to pick up. Keepalive issues, maybe external problems such as routing. But if you use a real listening socket (as opposed to mocking a connection) it's real TCP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With