linkedin / test-butler

Reliable Android Testing, at your service
Apache License 2.0
1.05k stars 92 forks source link

Switching from offline back to online #40

Open gingo opened 7 years ago

gingo commented 7 years ago

Hi there!

I have this two methods in my test-class:

public class ContentLoaderTest {

    @Test
    public void loadContent() throws Exception {

        // just for sure enable both internet connection types
        TestButler.setGsmState(true);
        TestButler.setWifiState(true);

        // place Thread.sleep(5000) here and everything is fine ;)

        // networking here is failing

    }

    @Test
    public void loadContentOffline() throws Exception {

        TestButler.setGsmState(false);
        TestButler.setWifiState(false);

        try {

            // networking here seems to be offline

        } finally {
            // enable it again
            TestButler.setGsmState(true);
            TestButler.setWifiState(true);
        }

    }

}

Method loadContentOffline() is started first and even if internet connectivity is reenabled again the second method is offline. It works with Thread.sleep().

SKART1 commented 7 years ago

@gingo could you please add better description, looks like we are facing same problem

gingo commented 7 years ago

Please try to formulate question. What details do you need?

SKART1 commented 7 years ago

the second method is offline

How can be a method "offline"? Is it about internet state? How are you checking that internet is available? Are you waiting for some condition or checking only once?

gingo commented 7 years ago

Yes, networking is failing as mentioned in comment. There is probably some delay when calling this methods:

    TestButler.setGsmState(true);
    TestButler.setWifiState(true);

I guess, TestButtler should wait before wifi is in desired state and then return from this methods.

SKART1 commented 7 years ago

How are you checking that internet is available?

It takes some time for android OS to actually disable internet - so we are using something like this:

doEnable();
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

boolean result = PredicateWaiter.wait(RETRY_COUNT,
        new PredicateWaiter.DelayDependOnCount.SimpleLinearDelay(PAUSE_MS),
        new PredicateWaiter.Predicate() {
            @Override
            public boolean compute(int tryCount) {
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo == null) {
                    Log.d(TAG, "Predicate on " + tryCount + " is false because of activeNetworkInfo is null");
                } else if (activeNetworkInfo.isAvailable()) {
                    Log.d(TAG, "Predicate on " + tryCount + " is false because of activeNetworkInfo.isAvailable() is false");
                }
                return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
            }
        });
if (!result) {
    throw new InternetManagerException("Internet was not enabled");
}

But sometimes internet do not coming back - even after 1 minute of waiting

shatou-aqarmap commented 7 years ago

I am having the same issue. The problem is that the test that disables the internet breaks down the following test although I am calling both setGsmState(true) and setWifiState(true) before the following tests. The only way to fix this for now is to test offline test in a separate test class.

iank-wp commented 7 years ago

I'm having the exact same problem as well. The first test will shutoff the wifi, and then re-enable it to do some network calls (which are successful). However the next test will have network calls time out.

kushsaini10 commented 6 years ago

I have made an Idling Resource to halt test until required connectivity state is met check it out here https://github.com/kushsaini10/connectivity-idling-resource