Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you unit test for tcp connection?

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

like image 870
melaos Avatar asked Nov 08 '11 01:11

melaos


People also ask

How do I test a TCP server?

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.

How do I know if my TCP connection is working?

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.


2 Answers

You should have two kinds of tests:

  1. Integration tests - with real bare-bone TCP server that can send and 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.
  2. Unit tests - using Mocking you can test more complex scenarios. You 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

like image 163
Dror Helper Avatar answered Oct 11 '22 18:10

Dror Helper


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.

like image 36
seand Avatar answered Oct 11 '22 19:10

seand