test-butler icon indicating copy to clipboard operation
test-butler copied to clipboard

Switching from offline back to online

Open gingo opened this issue 8 years ago • 8 comments

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().

gingo avatar Dec 04 '16 09:12 gingo

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

SKART1 avatar Mar 13 '17 09:03 SKART1

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

gingo avatar Mar 13 '17 20:03 gingo

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?

SKART1 avatar Mar 14 '17 09:03 SKART1

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.

gingo avatar Mar 14 '17 10:03 gingo

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

SKART1 avatar Mar 14 '17 10:03 SKART1

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.

shatou-aqarmap avatar Apr 02 '17 18:04 shatou-aqarmap

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.

iank-wp avatar May 04 '17 21:05 iank-wp

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

kushsaini10 avatar Jan 13 '18 18:01 kushsaini10