9

I need to test my app in following scenario:

  • connect device to wifi (with the internet).
  • simulate internet interruption (lack of the internet but the device must stay connected to the wifi network).
  • after few seconds the internet need to be active again.

I could simulate this pulling the adsl cable out of my ruter, but then it won't reconnect fast enough. Is this any way to simulate this programmatically?

XorOrNor
  • 8,868
  • 12
  • 48
  • 81
  • Very good question! I always do it with my router. But that is a pain in the ass! – A.S. Dec 20 '13 at 11:18
  • possible duplicate of [Simulate low bandwidth in android](http://stackoverflow.com/questions/8693117/simulate-low-bandwidth-in-android) – starkej2 Sep 04 '14 at 14:10
  • @blacksh33p Low bandwidth != Reliability (packet loss, network interruption etc.). This other question seems more appropriate: http://stackoverflow.com/q/130354/489607, but I still don't consider it a duplicate, by looking at the context and tags for this particular question. While the answers are certainly intersected, this question should/could stimulate other, specific, answers. – davidcesarino Sep 04 '14 at 18:45

4 Answers4

3

If you have a Mac, you can do it with Network Link Conditionner. It's really efficient, see here the step to install it :

http://nshipster.com/network-link-conditioner/

Andros
  • 4,069
  • 1
  • 22
  • 30
  • I don't understand how the Mac (or any other computer OS) is involved here? – XorOrNor Dec 20 '13 at 11:30
  • 1
    Well with your mac you can share your wifi connection, connect your device on this Wifi provide by your Mac. Enable the Network Link Conditionner and there you go ... – Andros Dec 20 '13 at 11:32
  • 1
    Sorry if I'm not explicit enough, with this tool, you can provide very very bad network (like loose 100% packet), and 1 second after put a very good network. – Andros Dec 20 '13 at 11:34
0

If you happen to have another device, you can create a hotspot with it and disallow mobile networks internet. The testing device will be connected to the hotspot, but will not be able to reach anywhere due to lack of Internet. As for the connection there, I think it is faster than restarting a router. However, you have to have another device for this to happen... Otherwise I don't know, and I'd like to know a true answer, as well.

  • I do have another device but am I able to "disallow internet" on the fly? Without reconnecting Wifi? – XorOrNor Dec 20 '13 at 11:28
  • I believe so, it is just like turning internet on or off. If I understand your question correctly, that is, I think that is the case. But keep in mind that if you create a hotspot, you cannot use your Wi-Fi on your emitting device, only the mobile networks. – Yordan Lyubenov Dec 20 '13 at 11:54
0

Try to pass all network requests (I use it with db transactions too) through a android.os.Handler. Use Runnables (better implement your own Runnable class) to implement the actions to perform (add callbacks, logging and other logic in your Runnable class). This way you can easily register listeners in your Runnable class to do whatever sleeping or failures your are interested in.

Here is some code from my project. The M_ClientRunnable class implements Runnable and has additional methods for registering "cancellators", failure listeners and success listeners.

class LooperThread extends Thread {
    public Handler mHandler;

    public LooperThread() {
        super();
        start();
    }

    public void run() {
        Looper.prepare();
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                getLogger().info(TAG, msg.toString());
            }
        };
        Looper.loop();
    }

    public boolean post(M_ClientRunnable runnable) {
        if(!isAlive()) {
            start();
        }
        if (mHandler!=null) {
            return mHandler.post(runnable);
        } else {
            return false;
        }
    }
};
private LooperThread looper = new LooperThread();
@Override
public boolean post(M_ClientRunnable runnable) {
    return looper.post(runnable);
}
agelbess
  • 4,249
  • 3
  • 20
  • 21
0

I don't have a real solution yet, but I found a workaround for the task. I'm using MikroTik router and its Firewall filter allows me to disable internet access for the particular IP adress on the fly:

6 X chain=forward action=drop protocol=tcp dst-address=192.168.1.20 
 in-interface=Internet(modem) 
XorOrNor
  • 8,868
  • 12
  • 48
  • 81