Fix EthernetTetheringTest testTestNetworkUpstream flaky am: 95cb089853 am: 3b9d3c49f6

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1878054

Change-Id: I7424f6ac20ea7a56db1f4428dd0f01c9854054a5
This commit is contained in:
markchien
2022-02-22 07:55:38 +00:00
committed by Automerger Merge Worker

View File

@@ -106,6 +106,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@@ -278,7 +279,7 @@ public class EthernetTetheringTest {
final String localAddr = "192.0.2.3/28"; final String localAddr = "192.0.2.3/28";
final String clientAddr = "192.0.2.2/28"; final String clientAddr = "192.0.2.2/28";
mTetheringEventCallback = enableEthernetTethering(iface, mTetheringEventCallback = enableEthernetTethering(iface,
requestWithStaticIpv4(localAddr, clientAddr)); requestWithStaticIpv4(localAddr, clientAddr), null /* any upstream */);
mTetheringEventCallback.awaitInterfaceTethered(); mTetheringEventCallback.awaitInterfaceTethered();
assertInterfaceHasIpAddress(iface, localAddr); assertInterfaceHasIpAddress(iface, localAddr);
@@ -361,7 +362,8 @@ public class EthernetTetheringTest {
final TetheringRequest request = new TetheringRequest.Builder(TETHERING_ETHERNET) final TetheringRequest request = new TetheringRequest.Builder(TETHERING_ETHERNET)
.setConnectivityScope(CONNECTIVITY_SCOPE_LOCAL).build(); .setConnectivityScope(CONNECTIVITY_SCOPE_LOCAL).build();
mTetheringEventCallback = enableEthernetTethering(iface, request); mTetheringEventCallback = enableEthernetTethering(iface, request,
null /* any upstream */);
mTetheringEventCallback.awaitInterfaceLocalOnly(); mTetheringEventCallback.awaitInterfaceLocalOnly();
// makePacketReader only works after tethering is started, because until then the interface // makePacketReader only works after tethering is started, because until then the interface
@@ -392,7 +394,7 @@ public class EthernetTetheringTest {
final String iface = mTetheredInterfaceRequester.getInterface(); final String iface = mTetheredInterfaceRequester.getInterface();
// Enable Ethernet tethering and check that it starts. // Enable Ethernet tethering and check that it starts.
mTetheringEventCallback = enableEthernetTethering(iface); mTetheringEventCallback = enableEthernetTethering(iface, null /* any upstream */);
// There is nothing more we can do on a physical interface without connecting an actual // There is nothing more we can do on a physical interface without connecting an actual
// client, which is not possible in this test. // client, which is not possible in this test.
@@ -405,8 +407,11 @@ public class EthernetTetheringTest {
private final CountDownLatch mLocalOnlyStartedLatch = new CountDownLatch(1); private final CountDownLatch mLocalOnlyStartedLatch = new CountDownLatch(1);
private final CountDownLatch mLocalOnlyStoppedLatch = new CountDownLatch(1); private final CountDownLatch mLocalOnlyStoppedLatch = new CountDownLatch(1);
private final CountDownLatch mClientConnectedLatch = new CountDownLatch(1); private final CountDownLatch mClientConnectedLatch = new CountDownLatch(1);
private final CountDownLatch mUpstreamConnectedLatch = new CountDownLatch(1); private final CountDownLatch mUpstreamLatch = new CountDownLatch(1);
private final TetheringInterface mIface; private final TetheringInterface mIface;
private final Network mExpectedUpstream;
private boolean mAcceptAnyUpstream = false;
private volatile boolean mInterfaceWasTethered = false; private volatile boolean mInterfaceWasTethered = false;
private volatile boolean mInterfaceWasLocalOnly = false; private volatile boolean mInterfaceWasLocalOnly = false;
@@ -415,8 +420,14 @@ public class EthernetTetheringTest {
private volatile Network mUpstream = null; private volatile Network mUpstream = null;
MyTetheringEventCallback(TetheringManager tm, String iface) { MyTetheringEventCallback(TetheringManager tm, String iface) {
this(tm, iface, null);
mAcceptAnyUpstream = true;
}
MyTetheringEventCallback(TetheringManager tm, String iface, Network expectedUpstream) {
mTm = tm; mTm = tm;
mIface = new TetheringInterface(TETHERING_ETHERNET, iface); mIface = new TetheringInterface(TETHERING_ETHERNET, iface);
mExpectedUpstream = expectedUpstream;
} }
public void unregister() { public void unregister() {
@@ -526,19 +537,30 @@ public class EthernetTetheringTest {
Log.d(TAG, "Got upstream changed: " + network); Log.d(TAG, "Got upstream changed: " + network);
mUpstream = network; mUpstream = network;
if (mUpstream != null) mUpstreamConnectedLatch.countDown(); if (mAcceptAnyUpstream || Objects.equals(mUpstream, mExpectedUpstream)) {
mUpstreamLatch.countDown();
}
} }
public Network awaitFirstUpstreamConnected() throws Exception { public Network awaitUpstreamChanged() throws Exception {
assertTrue("Did not receive upstream connected callback after " + TIMEOUT_MS + "ms", if (!mUpstreamLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
mUpstreamConnectedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); fail("Did not receive upstream " + (mAcceptAnyUpstream ? "any" : mExpectedUpstream)
+ " callback after " + TIMEOUT_MS + "ms");
}
return mUpstream; return mUpstream;
} }
} }
private MyTetheringEventCallback enableEthernetTethering(String iface, private MyTetheringEventCallback enableEthernetTethering(String iface,
TetheringRequest request) throws Exception { TetheringRequest request, Network expectedUpstream) throws Exception {
MyTetheringEventCallback callback = new MyTetheringEventCallback(mTm, iface); // Enable ethernet tethering with null expectedUpstream means the test accept any upstream
// after etherent tethering started.
final MyTetheringEventCallback callback;
if (expectedUpstream != null) {
callback = new MyTetheringEventCallback(mTm, iface, expectedUpstream);
} else {
callback = new MyTetheringEventCallback(mTm, iface);
}
mTm.registerTetheringEventCallback(mHandler::post, callback); mTm.registerTetheringEventCallback(mHandler::post, callback);
StartTetheringCallback startTetheringCallback = new StartTetheringCallback() { StartTetheringCallback startTetheringCallback = new StartTetheringCallback() {
@@ -565,10 +587,11 @@ public class EthernetTetheringTest {
return callback; return callback;
} }
private MyTetheringEventCallback enableEthernetTethering(String iface) throws Exception { private MyTetheringEventCallback enableEthernetTethering(String iface, Network expectedUpstream)
throws Exception {
return enableEthernetTethering(iface, return enableEthernetTethering(iface,
new TetheringRequest.Builder(TETHERING_ETHERNET) new TetheringRequest.Builder(TETHERING_ETHERNET)
.setShouldShowEntitlementUi(false).build()); .setShouldShowEntitlementUi(false).build(), expectedUpstream);
} }
private int getMTU(TestNetworkInterface iface) throws SocketException { private int getMTU(TestNetworkInterface iface) throws SocketException {
@@ -592,7 +615,8 @@ public class EthernetTetheringTest {
private void checkVirtualEthernet(TestNetworkInterface iface, int mtu) throws Exception { private void checkVirtualEthernet(TestNetworkInterface iface, int mtu) throws Exception {
FileDescriptor fd = iface.getFileDescriptor().getFileDescriptor(); FileDescriptor fd = iface.getFileDescriptor().getFileDescriptor();
mDownstreamReader = makePacketReader(fd, mtu); mDownstreamReader = makePacketReader(fd, mtu);
mTetheringEventCallback = enableEthernetTethering(iface.getInterfaceName()); mTetheringEventCallback = enableEthernetTethering(iface.getInterfaceName(),
null /* any upstream */);
checkTetheredClientCallbacks(mDownstreamReader); checkTetheredClientCallbacks(mDownstreamReader);
} }
@@ -690,7 +714,8 @@ public class EthernetTetheringTest {
private void assertInvalidStaticIpv4Request(String iface, String local, String client) private void assertInvalidStaticIpv4Request(String iface, String local, String client)
throws Exception { throws Exception {
try { try {
enableEthernetTethering(iface, requestWithStaticIpv4(local, client)); enableEthernetTethering(iface, requestWithStaticIpv4(local, client),
null /* any upstream */);
fail("Unexpectedly accepted invalid IPv4 configuration: " + local + ", " + client); fail("Unexpectedly accepted invalid IPv4 configuration: " + local + ", " + client);
} catch (IllegalArgumentException | NullPointerException expected) { } } catch (IllegalArgumentException | NullPointerException expected) { }
} }
@@ -746,9 +771,10 @@ public class EthernetTetheringTest {
assertEquals("TetheredInterfaceCallback for unexpected interface", assertEquals("TetheredInterfaceCallback for unexpected interface",
mDownstreamIface.getInterfaceName(), iface); mDownstreamIface.getInterfaceName(), iface);
mTetheringEventCallback = enableEthernetTethering(mDownstreamIface.getInterfaceName()); mTetheringEventCallback = enableEthernetTethering(mDownstreamIface.getInterfaceName(),
mUpstreamTracker.getNetwork());
assertEquals("onUpstreamChanged for unexpected network", mUpstreamTracker.getNetwork(), assertEquals("onUpstreamChanged for unexpected network", mUpstreamTracker.getNetwork(),
mTetheringEventCallback.awaitFirstUpstreamConnected()); mTetheringEventCallback.awaitUpstreamChanged());
mDownstreamReader = makePacketReader(mDownstreamIface); mDownstreamReader = makePacketReader(mDownstreamIface);
// TODO: do basic forwarding test here. // TODO: do basic forwarding test here.
@@ -951,9 +977,10 @@ public class EthernetTetheringTest {
assertEquals("TetheredInterfaceCallback for unexpected interface", assertEquals("TetheredInterfaceCallback for unexpected interface",
mDownstreamIface.getInterfaceName(), iface); mDownstreamIface.getInterfaceName(), iface);
mTetheringEventCallback = enableEthernetTethering(mDownstreamIface.getInterfaceName()); mTetheringEventCallback = enableEthernetTethering(mDownstreamIface.getInterfaceName(),
mUpstreamTracker.getNetwork());
assertEquals("onUpstreamChanged for unexpected network", mUpstreamTracker.getNetwork(), assertEquals("onUpstreamChanged for unexpected network", mUpstreamTracker.getNetwork(),
mTetheringEventCallback.awaitFirstUpstreamConnected()); mTetheringEventCallback.awaitUpstreamChanged());
mDownstreamReader = makePacketReader(mDownstreamIface); mDownstreamReader = makePacketReader(mDownstreamIface);
mUpstreamReader = makePacketReader(mUpstreamTracker.getTestIface()); mUpstreamReader = makePacketReader(mUpstreamTracker.getTestIface());