Merge changes I0e6df469,I3d46d5f7

* changes:
  Fix testMobileDataAlwaysOn flakiness
  Speed up NetworkFactory tests.
This commit is contained in:
Lorenzo Colitti
2021-02-19 08:13:38 +00:00
committed by Gerrit Code Review

View File

@@ -994,10 +994,12 @@ public class ConnectivityServiceTest {
// Used to collect the networks requests managed by this factory. This is a duplicate of // Used to collect the networks requests managed by this factory. This is a duplicate of
// the internal information stored in the NetworkFactory (which is private). // the internal information stored in the NetworkFactory (which is private).
private SparseArray<NetworkRequest> mNetworkRequests = new SparseArray<>(); private SparseArray<NetworkRequest> mNetworkRequests = new SparseArray<>();
private final HandlerThread mHandlerSendingRequests;
public MockNetworkFactory(Looper looper, Context context, String logTag, public MockNetworkFactory(Looper looper, Context context, String logTag,
NetworkCapabilities filter) { NetworkCapabilities filter, HandlerThread threadSendingRequests) {
super(looper, context, logTag, filter); super(looper, context, logTag, filter);
mHandlerSendingRequests = threadSendingRequests;
} }
public int getMyRequestCount() { public int getMyRequestCount() {
@@ -1051,7 +1053,8 @@ public class ConnectivityServiceTest {
public void terminate() { public void terminate() {
super.terminate(); super.terminate();
// Make sure there are no remaining requests unaccounted for. // Make sure there are no remaining requests unaccounted for.
assertNull(mRequestHistory.poll(TIMEOUT_MS, r -> true)); HandlerUtils.waitForIdle(mHandlerSendingRequests, TIMEOUT_MS);
assertNull(mRequestHistory.poll(0, r -> true));
} }
// Trigger releasing the request as unfulfillable // Trigger releasing the request as unfulfillable
@@ -2782,7 +2785,7 @@ public class ConnectivityServiceTest {
final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests"); final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
handlerThread.start(); handlerThread.start();
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter); mServiceContext, "testFactory", filter, mCsHandlerThread);
testFactory.setScoreFilter(40); testFactory.setScoreFilter(40);
ConditionVariable cv = testFactory.getNetworkStartedCV(); ConditionVariable cv = testFactory.getNetworkStartedCV();
testFactory.register(); testFactory.register();
@@ -2890,7 +2893,7 @@ public class ConnectivityServiceTest {
// does not crash. // does not crash.
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter); mServiceContext, "testFactory", filter, mCsHandlerThread);
// Register the factory and don't be surprised when the default request arrives. // Register the factory and don't be surprised when the default request arrives.
testFactory.register(); testFactory.register();
testFactory.expectRequestAdd(); testFactory.expectRequestAdd();
@@ -4127,7 +4130,7 @@ public class ConnectivityServiceTest {
.addTransportType(TRANSPORT_CELLULAR) .addTransportType(TRANSPORT_CELLULAR)
.addCapability(NET_CAPABILITY_INTERNET); .addCapability(NET_CAPABILITY_INTERNET);
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter); mServiceContext, "testFactory", filter, mCsHandlerThread);
testFactory.setScoreFilter(40); testFactory.setScoreFilter(40);
// Register the factory and expect it to start looking for a network. // Register the factory and expect it to start looking for a network.
@@ -4175,6 +4178,7 @@ public class ConnectivityServiceTest {
// ... and cell data to be torn down after nascent network timeout. // ... and cell data to be torn down after nascent network timeout.
cellNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent, cellNetworkCallback.expectCallback(CallbackEntry.LOST, mCellNetworkAgent,
mService.mNascentDelayMs + TEST_CALLBACK_TIMEOUT_MS); mService.mNascentDelayMs + TEST_CALLBACK_TIMEOUT_MS);
waitForIdle();
assertLength(1, mCm.getAllNetworks()); assertLength(1, mCm.getAllNetworks());
} finally { } finally {
testFactory.terminate(); testFactory.terminate();
@@ -4474,7 +4478,7 @@ public class ConnectivityServiceTest {
.addTransportType(TRANSPORT_WIFI) .addTransportType(TRANSPORT_WIFI)
.addCapability(NET_CAPABILITY_INTERNET); .addCapability(NET_CAPABILITY_INTERNET);
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
mServiceContext, "testFactory", filter); mServiceContext, "testFactory", filter, mCsHandlerThread);
testFactory.setScoreFilter(40); testFactory.setScoreFilter(40);
// Register the factory and expect it to receive the default request. // Register the factory and expect it to receive the default request.