DO NOT MERGE: ConnectivityServiceTest: fix testAvoidBadWifiSettings
This patch introduces an assertEventuallyThat helper function in ConnectivityServiceTest which given a boolean function retries until the function returns true or until a maximum retry time is reached. This function is used to fix flakyness of testAvoidBadWifiSetting where the Message posted by reevaluate() could reach the Handler's MessageQueue after waitForIdle takes effect, resulting in the test to fail. Instead of fixing the flakyness by introdcing hard sleep times, assertEventuallyThat is used to reduce the overall test time. With this change the test has been observed to pass with 100% success rate over 50000 invocations. Test: $ runtest frameworks-net Bug: 32561414 (cherry picked from commitb54c8cffa8) (cherry picked from commitb1bddc92c5) Change-Id: I432f90a699dadfef37a5d0a69e25050753340964
This commit is contained in:
committed by
Lorenzo Colitti
parent
020844b6cf
commit
0bcc4a517e
@@ -91,6 +91,7 @@ import java.util.concurrent.CountDownLatch;
|
|||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link ConnectivityService}.
|
* Tests for {@link ConnectivityService}.
|
||||||
@@ -622,7 +623,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class WrappedAvoidBadWifiTracker extends AvoidBadWifiTracker {
|
private class WrappedAvoidBadWifiTracker extends AvoidBadWifiTracker {
|
||||||
public boolean configRestrictsAvoidBadWifi;
|
public volatile boolean configRestrictsAvoidBadWifi;
|
||||||
|
|
||||||
public WrappedAvoidBadWifiTracker(Context c, Handler h, Runnable r) {
|
public WrappedAvoidBadWifiTracker(Context c, Handler h, Runnable r) {
|
||||||
super(c, h, r);
|
super(c, h, r);
|
||||||
@@ -2180,7 +2181,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
tracker.reevaluate();
|
tracker.reevaluate();
|
||||||
mService.waitForIdle();
|
mService.waitForIdle();
|
||||||
String msg = String.format("config=false, setting=%s", values[i]);
|
String msg = String.format("config=false, setting=%s", values[i]);
|
||||||
assertTrue(msg, mService.avoidBadWifi());
|
assertEventuallyTrue(() -> mService.avoidBadWifi(), 50);
|
||||||
assertFalse(msg, tracker.shouldNotifyWifiUnvalidated());
|
assertFalse(msg, tracker.shouldNotifyWifiUnvalidated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2189,19 +2190,19 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
Settings.Global.putInt(cr, settingName, 0);
|
Settings.Global.putInt(cr, settingName, 0);
|
||||||
tracker.reevaluate();
|
tracker.reevaluate();
|
||||||
mService.waitForIdle();
|
mService.waitForIdle();
|
||||||
assertFalse(mService.avoidBadWifi());
|
assertEventuallyTrue(() -> !mService.avoidBadWifi(), 50);
|
||||||
assertFalse(tracker.shouldNotifyWifiUnvalidated());
|
assertFalse(tracker.shouldNotifyWifiUnvalidated());
|
||||||
|
|
||||||
Settings.Global.putInt(cr, settingName, 1);
|
Settings.Global.putInt(cr, settingName, 1);
|
||||||
tracker.reevaluate();
|
tracker.reevaluate();
|
||||||
mService.waitForIdle();
|
mService.waitForIdle();
|
||||||
assertTrue(mService.avoidBadWifi());
|
assertEventuallyTrue(() -> mService.avoidBadWifi(), 50);
|
||||||
assertFalse(tracker.shouldNotifyWifiUnvalidated());
|
assertFalse(tracker.shouldNotifyWifiUnvalidated());
|
||||||
|
|
||||||
Settings.Global.putString(cr, settingName, null);
|
Settings.Global.putString(cr, settingName, null);
|
||||||
tracker.reevaluate();
|
tracker.reevaluate();
|
||||||
mService.waitForIdle();
|
mService.waitForIdle();
|
||||||
assertFalse(mService.avoidBadWifi());
|
assertEventuallyTrue(() -> !mService.avoidBadWifi(), 50);
|
||||||
assertTrue(tracker.shouldNotifyWifiUnvalidated());
|
assertTrue(tracker.shouldNotifyWifiUnvalidated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2418,6 +2419,17 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
networkCallback.assertNoCallback();
|
networkCallback.assertNoCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void assertEventuallyTrue(BooleanSupplier fn, long maxWaitingTimeMs) throws Exception {
|
||||||
|
long start = SystemClock.elapsedRealtime();
|
||||||
|
while (SystemClock.elapsedRealtime() <= start + maxWaitingTimeMs) {
|
||||||
|
if (fn.getAsBoolean()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Thread.sleep(10);
|
||||||
|
}
|
||||||
|
assertTrue(fn.getAsBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
private static class TestKeepaliveCallback extends PacketKeepaliveCallback {
|
private static class TestKeepaliveCallback extends PacketKeepaliveCallback {
|
||||||
|
|
||||||
public static enum CallbackType { ON_STARTED, ON_STOPPED, ON_ERROR };
|
public static enum CallbackType { ON_STARTED, ON_STOPPED, ON_ERROR };
|
||||||
|
|||||||
Reference in New Issue
Block a user