Remove most sleep() calls from ConnectivityServiceTest
Change-Id: I90d2f6811ed1cb84614101200ac377e920bd864a
This commit is contained in:
@@ -228,6 +228,10 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class MockNetworkFactory extends NetworkFactory {
|
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);
|
final AtomicBoolean mNetworkStarted = new AtomicBoolean(false);
|
||||||
|
|
||||||
public MockNetworkFactory(Looper looper, Context context, String logTag,
|
public MockNetworkFactory(Looper looper, Context context, String logTag,
|
||||||
@@ -241,15 +245,51 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
|
|
||||||
protected void startNetwork() {
|
protected void startNetwork() {
|
||||||
mNetworkStarted.set(true);
|
mNetworkStarted.set(true);
|
||||||
|
mNetworkStartedCV.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void stopNetwork() {
|
protected void stopNetwork() {
|
||||||
mNetworkStarted.set(false);
|
mNetworkStarted.set(false);
|
||||||
|
mNetworkStoppedCV.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getMyStartRequested() {
|
public boolean getMyStartRequested() {
|
||||||
return mNetworkStarted.get();
|
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 {
|
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
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
@@ -396,7 +451,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
// Test cellular linger timeout.
|
// Test cellular linger timeout.
|
||||||
try {
|
try {
|
||||||
Thread.sleep(6000);
|
Thread.sleep(6000);
|
||||||
} catch (Exception e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
verifyActiveNetwork(TRANSPORT_WIFI);
|
verifyActiveNetwork(TRANSPORT_WIFI);
|
||||||
assertEquals(1, mCm.getAllNetworks().length);
|
assertEquals(1, mCm.getAllNetworks().length);
|
||||||
@@ -421,14 +476,14 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
mCellNetworkAgent.connect(false);
|
mCellNetworkAgent.connect(false);
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
} catch (Exception e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
verifyActiveNetwork(TRANSPORT_WIFI);
|
verifyActiveNetwork(TRANSPORT_WIFI);
|
||||||
// Test cellular disconnect.
|
// Test cellular disconnect.
|
||||||
mCellNetworkAgent.disconnect();
|
mCellNetworkAgent.disconnect();
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
} catch (Exception e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
verifyActiveNetwork(TRANSPORT_WIFI);
|
verifyActiveNetwork(TRANSPORT_WIFI);
|
||||||
// Test bringing up validated cellular
|
// Test bringing up validated cellular
|
||||||
@@ -481,26 +536,26 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
filter.addCapability(NET_CAPABILITY_INTERNET);
|
filter.addCapability(NET_CAPABILITY_INTERNET);
|
||||||
final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
|
final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
|
||||||
handlerThread.start();
|
handlerThread.start();
|
||||||
MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
|
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
|
||||||
mServiceContext, "testFactory", filter);
|
mServiceContext, "testFactory", filter);
|
||||||
testFactory.setScoreFilter(40);
|
testFactory.setScoreFilter(40);
|
||||||
|
ConditionVariable cv = testFactory.getNetworkStartedCV();
|
||||||
testFactory.register();
|
testFactory.register();
|
||||||
try {
|
cv.block();
|
||||||
Thread.sleep(500);
|
|
||||||
} catch (Exception e) {}
|
|
||||||
assertEquals(1, testFactory.getMyRequestCount());
|
assertEquals(1, testFactory.getMyRequestCount());
|
||||||
assertEquals(true, testFactory.getMyStartRequested());
|
assertEquals(true, testFactory.getMyStartRequested());
|
||||||
|
|
||||||
// now bring in a higher scored network
|
// now bring in a higher scored network
|
||||||
MockNetworkAgent testAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
|
MockNetworkAgent testAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
|
||||||
ConditionVariable cv = waitForConnectivityBroadcasts(1);
|
cv = waitForConnectivityBroadcasts(1);
|
||||||
|
ConditionVariable cvRelease = testFactory.getNetworkStoppedCV();
|
||||||
testAgent.connect(true);
|
testAgent.connect(true);
|
||||||
cv.block();
|
cv.block();
|
||||||
// part of the bringup makes another network request and then releases it
|
// part of the bringup makes another network request and then releases it
|
||||||
// wait for the release
|
// wait for the release
|
||||||
try { Thread.sleep(500); } catch (Exception e) {}
|
cvRelease.block();
|
||||||
assertEquals(1, testFactory.getMyRequestCount());
|
|
||||||
assertEquals(false, testFactory.getMyStartRequested());
|
assertEquals(false, testFactory.getMyStartRequested());
|
||||||
|
testFactory.waitForNetworkRequests(1);
|
||||||
|
|
||||||
// bring in a bunch of requests..
|
// bring in a bunch of requests..
|
||||||
ConnectivityManager.NetworkCallback[] networkCallbacks =
|
ConnectivityManager.NetworkCallback[] networkCallbacks =
|
||||||
@@ -511,21 +566,14 @@ public class ConnectivityServiceTest extends AndroidTestCase {
|
|||||||
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
||||||
mCm.requestNetwork(builder.build(), networkCallbacks[i]);
|
mCm.requestNetwork(builder.build(), networkCallbacks[i]);
|
||||||
}
|
}
|
||||||
|
testFactory.waitForNetworkRequests(11);
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (Exception e) {}
|
|
||||||
assertEquals(11, testFactory.getMyRequestCount());
|
|
||||||
assertEquals(false, testFactory.getMyStartRequested());
|
assertEquals(false, testFactory.getMyStartRequested());
|
||||||
|
|
||||||
// remove the requests
|
// remove the requests
|
||||||
for (int i = 0; i < networkCallbacks.length; i++) {
|
for (int i = 0; i < networkCallbacks.length; i++) {
|
||||||
mCm.unregisterNetworkCallback(networkCallbacks[i]);
|
mCm.unregisterNetworkCallback(networkCallbacks[i]);
|
||||||
}
|
}
|
||||||
try {
|
testFactory.waitForNetworkRequests(1);
|
||||||
Thread.sleep(500);
|
|
||||||
} catch (Exception e) {}
|
|
||||||
assertEquals(1, testFactory.getMyRequestCount());
|
|
||||||
assertEquals(false, testFactory.getMyStartRequested());
|
assertEquals(false, testFactory.getMyStartRequested());
|
||||||
|
|
||||||
// drop the higher scored network
|
// drop the higher scored network
|
||||||
|
|||||||
Reference in New Issue
Block a user