Simulate network connectivity issues on Linux using Java Simulate network connectivity issues on Linux using Java selenium selenium

Simulate network connectivity issues on Linux using Java


Option 1

Stubbing

You'll have to identify what would be the exception thrown and what will be the component that throws it in a real case scenario. You can do that easily, simulate the scenario in your machine and when the exception is thrown, the stack-trace will tell you exactly what component thrown it.

Then you'll have to extend the component that throws the exception and inject it in the proper place and, ultimately, create an API to trigger that exception.

If you think you need a framework to automate this kind of tests, have a look to to Fitnesse.


Option 2

Simulation

Simulating a real network problem, would be overly complicated and the benefits are not worth the effort in this case (imo).

Anyway... Linux has an excellent built in network emulation layer called netem that allows any kind of seamless interaction with the network traffic. This kernel module is controlled by the tc command line interface.

It's not very easy to get it right when you want to apply those condition to a single port because you'll have to:

  • Netfilter rule to mark the packets that we want to limit
  • Traffic Control policy
  • Filter to bind the packets to the policy

A simple example would be if you want to have 10% packet loss on the whole eth0, in which case you'll use:

tc qdisc change dev eth0 root netem loss 10%

Then you'll have to wrap this functionality in some java controllable way.If I failed on discouraging you, here is a good guide: TrafficControl


You can execute the following commands on windows on cmd to disconnect and reconnect network.

> ipconfig /release> ipconfig /renew

Using this you can use the Java Runtime class to execute the command.

Runtime.getRuntime().exec("ipconfig /release");

I have tested this on windows and it works. The Linux equivalent of the cmd commands are as follows

> sudo ifconfig eth0 up> sudo ifconfig eth0 down

Note that eth0 here is the name of my Ethernet connection. You can list the names of all the connections using

> ifconfig -a

You can look at the following thread to execute bash through Java - How to execute bash command with sudo privileges in Java?