Remove most sleep() calls from ConnectivityServiceTest
Change-Id: I90d2f6811ed1cb84614101200ac377e920bd864a
diff --git a/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java b/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
index bb0a36f..b58c2e2 100644
--- a/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
@@ -228,6 +228,10 @@
}
private static class MockNetworkFactory extends NetworkFactory {
+ final ConditionVariable mNetworkStartedCV = new ConditionVariable();
+ final ConditionVariable mNetworkStoppedCV = new ConditionVariable();
+ final ConditionVariable mNetworkRequestedCV = new ConditionVariable();
+ final ConditionVariable mNetworkReleasedCV = new ConditionVariable();
final AtomicBoolean mNetworkStarted = new AtomicBoolean(false);
public MockNetworkFactory(Looper looper, Context context, String logTag,
@@ -241,15 +245,51 @@
protected void startNetwork() {
mNetworkStarted.set(true);
+ mNetworkStartedCV.open();
}
protected void stopNetwork() {
mNetworkStarted.set(false);
+ mNetworkStoppedCV.open();
}
public boolean getMyStartRequested() {
return mNetworkStarted.get();
}
+
+ public ConditionVariable getNetworkStartedCV() {
+ mNetworkStartedCV.close();
+ return mNetworkStartedCV;
+ }
+
+ public ConditionVariable getNetworkStoppedCV() {
+ mNetworkStoppedCV.close();
+ return mNetworkStoppedCV;
+ }
+
+ protected void needNetworkFor(NetworkRequest networkRequest, int score) {
+ super.needNetworkFor(networkRequest, score);
+ mNetworkRequestedCV.open();
+ }
+
+ protected void releaseNetworkFor(NetworkRequest networkRequest) {
+ super.releaseNetworkFor(networkRequest);
+ mNetworkReleasedCV.open();
+ }
+
+ public ConditionVariable getNetworkRequestedCV() {
+ mNetworkRequestedCV.close();
+ return mNetworkRequestedCV;
+ }
+
+ public ConditionVariable getNetworkReleasedCV() {
+ mNetworkReleasedCV.close();
+ return mNetworkReleasedCV;
+ }
+
+ public void waitForNetworkRequests(final int count) {
+ waitFor(new Criteria() { public boolean get() { return count == getRequestCount(); } });
+ }
}
private class WrappedConnectivityService extends ConnectivityService {
@@ -286,6 +326,21 @@
}
}
+ private interface Criteria {
+ public boolean get();
+ }
+
+ static private void waitFor(Criteria criteria) {
+ int delays = 0;
+ while (!criteria.get()) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ }
+ if (++delays == 5) fail();
+ }
+ }
+
@Override
public void setUp() throws Exception {
super.setUp();
@@ -396,7 +451,7 @@
// Test cellular linger timeout.
try {
Thread.sleep(6000);
- } catch (Exception e) {
+ } catch (InterruptedException e) {
}
verifyActiveNetwork(TRANSPORT_WIFI);
assertEquals(1, mCm.getAllNetworks().length);
@@ -421,14 +476,14 @@
mCellNetworkAgent.connect(false);
try {
Thread.sleep(1000);
- } catch (Exception e) {
+ } catch (InterruptedException e) {
}
verifyActiveNetwork(TRANSPORT_WIFI);
// Test cellular disconnect.
mCellNetworkAgent.disconnect();
try {
Thread.sleep(1000);
- } catch (Exception e) {
+ } catch (InterruptedException e) {
}
verifyActiveNetwork(TRANSPORT_WIFI);
// Test bringing up validated cellular
@@ -481,26 +536,26 @@
filter.addCapability(NET_CAPABILITY_INTERNET);
final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
handlerThread.start();
- MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
+ final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter);
testFactory.setScoreFilter(40);
+ ConditionVariable cv = testFactory.getNetworkStartedCV();
testFactory.register();
- try {
- Thread.sleep(500);
- } catch (Exception e) {}
+ cv.block();
assertEquals(1, testFactory.getMyRequestCount());
assertEquals(true, testFactory.getMyStartRequested());
// now bring in a higher scored network
MockNetworkAgent testAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
- ConditionVariable cv = waitForConnectivityBroadcasts(1);
+ cv = waitForConnectivityBroadcasts(1);
+ ConditionVariable cvRelease = testFactory.getNetworkStoppedCV();
testAgent.connect(true);
cv.block();
// part of the bringup makes another network request and then releases it
// wait for the release
- try { Thread.sleep(500); } catch (Exception e) {}
- assertEquals(1, testFactory.getMyRequestCount());
+ cvRelease.block();
assertEquals(false, testFactory.getMyStartRequested());
+ testFactory.waitForNetworkRequests(1);
// bring in a bunch of requests..
ConnectivityManager.NetworkCallback[] networkCallbacks =
@@ -511,21 +566,14 @@
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
mCm.requestNetwork(builder.build(), networkCallbacks[i]);
}
-
- try {
- Thread.sleep(1000);
- } catch (Exception e) {}
- assertEquals(11, testFactory.getMyRequestCount());
+ testFactory.waitForNetworkRequests(11);
assertEquals(false, testFactory.getMyStartRequested());
// remove the requests
for (int i = 0; i < networkCallbacks.length; i++) {
mCm.unregisterNetworkCallback(networkCallbacks[i]);
}
- try {
- Thread.sleep(500);
- } catch (Exception e) {}
- assertEquals(1, testFactory.getMyRequestCount());
+ testFactory.waitForNetworkRequests(1);
assertEquals(false, testFactory.getMyStartRequested());
// drop the higher scored network