Allow tests to set the NetworkAgent's callbacks when creating it.

ConnectivityServiceTest#testNetworkAgentCallbacks tests the
behaviour of the NetworkAgent callbacks onNetworkCreated,
onNetworkUnwanted, and onNetworkDestroyed. This uses a
NetworkAgentWrapper method that sets the callbacks after the
test agent is constructed.

This infrastructure not sufficient to test an upcoming change
which will make onNetworkCreated be fired as soon as the
registration onNetworkCreated is fired as soon as the agent is
registered. Fix the code so that the callbacks can be specified
at agent registration time. This is also a bit more realistic
since in real usage, the callbacks are methods on the
NetworkAgent subclass and are already set when the agent is
constructed.

Bug: 143158421
Test: test-only change
Change-Id: I53c58e7b6c6ae4abf08e0df5051694cc4568a510
This commit is contained in:
Lorenzo Colitti
2022-08-01 18:19:17 +09:00
parent 28a690dc79
commit 54eae6c348
2 changed files with 86 additions and 54 deletions

View File

@@ -428,6 +428,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
@@ -922,9 +923,6 @@ public class ConnectivityServiceTest {
private int mProbesSucceeded;
private String mNmValidationRedirectUrl = null;
private boolean mNmProvNotificationRequested = false;
private Runnable mCreatedCallback;
private Runnable mUnwantedCallback;
private Runnable mDisconnectedCallback;
private final ConditionVariable mNetworkStatusReceived = new ConditionVariable();
// Contains the redirectUrl from networkStatus(). Before reading, wait for
@@ -932,22 +930,34 @@ public class ConnectivityServiceTest {
private String mRedirectUrl;
TestNetworkAgentWrapper(int transport) throws Exception {
this(transport, new LinkProperties(), null /* ncTemplate */, null /* provider */);
this(transport, new LinkProperties(), null /* ncTemplate */, null /* provider */, null);
}
TestNetworkAgentWrapper(int transport, LinkProperties linkProperties)
throws Exception {
this(transport, linkProperties, null /* ncTemplate */, null /* provider */);
this(transport, linkProperties, null /* ncTemplate */, null /* provider */, null);
}
private TestNetworkAgentWrapper(int transport, LinkProperties linkProperties,
NetworkCapabilities ncTemplate) throws Exception {
this(transport, linkProperties, ncTemplate, null /* provider */);
this(transport, linkProperties, ncTemplate, null /* provider */, null);
}
private TestNetworkAgentWrapper(int transport, LinkProperties linkProperties,
NetworkCapabilities ncTemplate, NetworkProvider provider) throws Exception {
super(transport, linkProperties, ncTemplate, provider, mServiceContext);
this(transport, linkProperties, ncTemplate, provider /* provider */, null);
}
private TestNetworkAgentWrapper(int transport, NetworkAgentWrapper.Callbacks callbacks)
throws Exception {
this(transport, new LinkProperties(), null /* ncTemplate */, null /* provider */,
callbacks);
}
private TestNetworkAgentWrapper(int transport, LinkProperties linkProperties,
NetworkCapabilities ncTemplate, NetworkProvider provider,
NetworkAgentWrapper.Callbacks callbacks) throws Exception {
super(transport, linkProperties, ncTemplate, provider, callbacks, mServiceContext);
// Waits for the NetworkAgent to be registered, which includes the creation of the
// NetworkMonitor.
@@ -968,23 +978,6 @@ public class ConnectivityServiceTest {
mNetworkStatusReceived.open();
}
@Override
public void onNetworkCreated() {
super.onNetworkCreated();
if (mCreatedCallback != null) mCreatedCallback.run();
}
@Override
public void onNetworkUnwanted() {
super.onNetworkUnwanted();
if (mUnwantedCallback != null) mUnwantedCallback.run();
}
@Override
public void onNetworkDestroyed() {
super.onNetworkDestroyed();
if (mDisconnectedCallback != null) mDisconnectedCallback.run();
}
}
@Override
@@ -1214,18 +1207,6 @@ public class ConnectivityServiceTest {
p.timestampMillis = DATA_STALL_TIMESTAMP;
mNmCallbacks.notifyDataStallSuspected(p);
}
public void setCreatedCallback(Runnable r) {
mCreatedCallback = r;
}
public void setUnwantedCallback(Runnable r) {
mUnwantedCallback = r;
}
public void setDisconnectedCallback(Runnable r) {
mDisconnectedCallback = r;
}
}
/**
@@ -3566,37 +3547,35 @@ public class ConnectivityServiceTest {
final NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_WIFI).build();
final TestNetworkCallback callback = new TestNetworkCallback();
final AtomicReference<Network> wifiNetwork = new AtomicReference<>();
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
// Expectations for state when various callbacks fire. These expectations run on the handler
// thread and not on the test thread because they need to prevent the handler thread from
// advancing while they examine state.
// 1. When onCreated fires, netd has been told to create the network.
mWiFiNetworkAgent.setCreatedCallback(() -> {
final Consumer<NetworkAgent> onNetworkCreated = (agent) -> {
eventOrder.offer("onNetworkCreated");
wifiNetwork.set(mWiFiNetworkAgent.getNetwork());
assertNotNull(wifiNetwork.get());
try {
verify(mMockNetd).networkCreate(nativeNetworkConfigPhysical(
wifiNetwork.get().getNetId(), INetd.PERMISSION_NONE));
agent.getNetwork().getNetId(), INetd.PERMISSION_NONE));
} catch (RemoteException impossible) {
fail();
}
});
};
// 2. onNetworkUnwanted isn't precisely ordered with respect to any particular events. Just
// check that it is fired at some point after disconnect.
mWiFiNetworkAgent.setUnwantedCallback(() -> eventOrder.offer("onNetworkUnwanted"));
final Consumer<NetworkAgent> onNetworkUnwanted = (agent) -> {
eventOrder.offer("onNetworkUnwanted");
};
// 3. While the teardown timer is running, connectivity APIs report the network is gone, but
// netd has not yet been told to destroy it.
final Runnable duringTeardown = () -> {
final Consumer<Network> duringTeardown = (network) -> {
eventOrder.offer("timePasses");
assertNull(mCm.getLinkProperties(wifiNetwork.get()));
assertNull(mCm.getLinkProperties(network));
try {
verify(mMockNetd, never()).networkDestroy(wifiNetwork.get().getNetId());
verify(mMockNetd, never()).networkDestroy(network.getNetId());
} catch (RemoteException impossible) {
fail();
}
@@ -3604,15 +3583,20 @@ public class ConnectivityServiceTest {
// 4. After onNetworkDisconnected is called, connectivity APIs report the network is gone,
// and netd has been told to destroy it.
mWiFiNetworkAgent.setDisconnectedCallback(() -> {
final Consumer<NetworkAgent> onNetworkDisconnected = (agent) -> {
eventOrder.offer("onNetworkDisconnected");
assertNull(mCm.getLinkProperties(wifiNetwork.get()));
assertNull(mCm.getLinkProperties(agent.getNetwork()));
try {
verify(mMockNetd).networkDestroy(wifiNetwork.get().getNetId());
verify(mMockNetd).networkDestroy(agent.getNetwork().getNetId());
} catch (RemoteException impossible) {
fail();
}
});
};
final NetworkAgentWrapper.Callbacks callbacks = new NetworkAgentWrapper.Callbacks(
onNetworkCreated, onNetworkUnwanted, onNetworkDisconnected);
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI, callbacks);
// Connect a network, and file a request for it after it has come up, to ensure the nascent
// timer is cleared and the test does not have to wait for it. Filing the request after the
@@ -3634,7 +3618,7 @@ public class ConnectivityServiceTest {
// down the network and started the teardown timer, and short enough that the lambda is
// scheduled to run before the teardown timer.
final Handler h = new Handler(mCsHandlerThread.getLooper());
h.postDelayed(duringTeardown, 150);
h.postDelayed(() -> duringTeardown.accept(mWiFiNetworkAgent.getNetwork()), 150);
// Disconnect the network and check that events happened in the right order.
mCm.unregisterNetworkCallback(callback);