I've had a bug in our software that occurs when I receive a connection timeout. These errors are very rare (usually when my connection gets dropped by our internal network). How can I generate this kind of effect artificially so I can test our software?
If it matters the app is written in C++/MFC using CAsyncSocket classes.
Edit:
I've tried using a non-existent host, and I get the socket error:
WSAEINVAL (10022) Invalid argument
My next attempt was to use Alexander's suggestion of connecting to a different port, e.g. 81 (on my own server though). That worked great. Exactly the same as a dropped connection (60 second wait, then error). Thank you!
You can use the Python REPL to simulate a timeout while receiving data (i.e. after a connection has been established successfully). Nothing but a standard Python installation is needed.
Now it waits for an incoming connection. Connect whatever you want to test to
localhost:9000
. When you do, Python will accept the connection andaccept()
will return it. Unless you send any data through theclientsocket
, the caller's socket should time out during the nextrecv()
.I would like to point everybody's attention to pathod
With a config (taken from their examples) of
200:b@100:dr
you'll get a connection that randomly drops.If you want to use an active connection you can also use http://httpbin.org/delay/#, where # is the time you want their server to wait before sending a response. As long as your timeout is shorter than the delay ... should simulate the effect. I've successfully used it with the python requests package.
You may want to modify your request if you're sending anything sensitive - no idea what happens to the data sent to them.
There are a couple of tactics I've used in the past to simulate networking issues;
One of these ideas might give you some means of artifically generating the scenario you need
Connect to a non-routable IP address, such as 10.255.255.1.
The technique I use frequently to simulate a random connection timeout is to use ssh local port forwarding.
This will forward traffic on localhost:12345 to realserver.com:80 You can loop this around in your own local machine as well, if you want:
So you can point your application at your localhost and custom port, and the traffic will get routed to the target host:port. Then you can exit out of this shell (you may also need to ctrl+c the shell after you exit) and it will kill the forwarding which causes your app to see a connection loss.