summaryrefslogtreecommitdiffstats
path: root/services/tests
diff options
context:
space:
mode:
authorPaul Jensen <pauljensen@google.com>2015-06-12 10:31:09 -0400
committerPaul Jensen <pauljensen@google.com>2015-06-12 10:31:09 -0400
commit0a2823e539abeb037d3acb01b5d5eed7c85f4ed7 (patch)
treeaa474ed133a2022839afb2c404c0714cb2403284 /services/tests
parentfd9a2dafdc6516a62bdf13c81ec2ac579a8fd998 (diff)
downloadframeworks_base-0a2823e539abeb037d3acb01b5d5eed7c85f4ed7.zip
frameworks_base-0a2823e539abeb037d3acb01b5d5eed7c85f4ed7.tar.gz
frameworks_base-0a2823e539abeb037d3acb01b5d5eed7c85f4ed7.tar.bz2
Remove most sleep() calls from ConnectivityServiceTest
Change-Id: I90d2f6811ed1cb84614101200ac377e920bd864a
Diffstat (limited to 'services/tests')
-rw-r--r--services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java86
1 files changed, 67 insertions, 19 deletions
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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
}
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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
}
}
+ 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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
// 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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
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 @@ public class ConnectivityServiceTest extends AndroidTestCase {
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