From 54fa56def5a8f684d9c11252486e8a26936c37b5 Mon Sep 17 00:00:00 2001 From: Mark Chien Date: Tue, 2 Apr 2024 04:27:04 +0000 Subject: [PATCH] Move setupOnce from EthernetTetheringTestBase to EthernetTetheringTest If we want to get rid of the flakiness, we may need to change the production code. You can find a more detailed analysis in b/318121782/comment#4. Here's a quick and easy alternative fix: just move setupOnce from EthernetTetheringTestBase to EthernetTetheringTest. The flakiness started when I added setupOnce into EthernetTetheringTestBase to fix flakiness in EthernetTetheringTest. Bug: 318121782 Test: TH Change-Id: I0a6fb25773aa60872f71fe4b34e42a0b745a767f --- .../net/EthernetTetheringTestBase.java | 31 +------------------ .../android/net/EthernetTetheringTest.java | 30 ++++++++++++++++++ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java index f6968850a9..120b8711d8 100644 --- a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java +++ b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java @@ -74,7 +74,6 @@ import com.android.testutils.TestNetworkTracker; import org.junit.After; import org.junit.Before; -import org.junit.BeforeClass; import java.io.FileDescriptor; import java.net.Inet4Address; @@ -144,7 +143,7 @@ public abstract class EthernetTetheringTestBase { private static final Context sContext = InstrumentationRegistry.getInstrumentation().getContext(); - private static final EthernetManager sEm = sContext.getSystemService(EthernetManager.class); + protected static final EthernetManager sEm = sContext.getSystemService(EthernetManager.class); private static final TetheringManager sTm = sContext.getSystemService(TetheringManager.class); private static final PackageManager sPackageManager = sContext.getPackageManager(); private static final CtsNetUtils sCtsNetUtils = new CtsNetUtils(sContext); @@ -168,34 +167,6 @@ public abstract class EthernetTetheringTestBase { return sContext; } - @BeforeClass - public static void setUpOnce() throws Exception { - // The first test case may experience tethering restart with IP conflict handling. - // Tethering would cache the last upstreams so that the next enabled tethering avoids - // picking up the address that is in conflict with the upstreams. To protect subsequent - // tests, turn tethering on and off before running them. - MyTetheringEventCallback callback = null; - TestNetworkInterface testIface = null; - assumeTrue(sEm != null); - try { - // If the physical ethernet interface is available, do nothing. - if (isInterfaceForTetheringAvailable()) return; - - testIface = createTestInterface(); - setIncludeTestInterfaces(true); - - callback = enableEthernetTethering(testIface.getInterfaceName(), null); - callback.awaitUpstreamChanged(true /* throwTimeoutException */); - } catch (TimeoutException e) { - Log.d(TAG, "WARNNING " + e); - } finally { - maybeCloseTestInterface(testIface); - maybeUnregisterTetheringEventCallback(callback); - - setIncludeTestInterfaces(false); - } - } - @Before public void setUp() throws Exception { mHandlerThread = new HandlerThread(getClass().getSimpleName()); diff --git a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java index 4949eaa9e2..c54d1b4d54 100644 --- a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java +++ b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java @@ -59,6 +59,7 @@ import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import com.android.testutils.NetworkStackModuleTest; import com.android.testutils.TapPacketReader; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -150,6 +151,35 @@ public class EthernetTetheringTest extends EthernetTetheringTestBase { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 /* Address: 1.2.3.4 */ }; + /** Enable/disable tethering once before running the tests. */ + @BeforeClass + public static void setUpOnce() throws Exception { + // The first test case may experience tethering restart with IP conflict handling. + // Tethering would cache the last upstreams so that the next enabled tethering avoids + // picking up the address that is in conflict with the upstreams. To protect subsequent + // tests, turn tethering on and off before running them. + MyTetheringEventCallback callback = null; + TestNetworkInterface testIface = null; + assumeTrue(sEm != null); + try { + // If the physical ethernet interface is available, do nothing. + if (isInterfaceForTetheringAvailable()) return; + + testIface = createTestInterface(); + setIncludeTestInterfaces(true); + + callback = enableEthernetTethering(testIface.getInterfaceName(), null); + callback.awaitUpstreamChanged(true /* throwTimeoutException */); + } catch (TimeoutException e) { + Log.d(TAG, "WARNNING " + e); + } finally { + maybeCloseTestInterface(testIface); + maybeUnregisterTetheringEventCallback(callback); + + setIncludeTestInterfaces(false); + } + } + @Test public void testVirtualEthernetAlreadyExists() throws Exception { // This test requires manipulating packets. Skip if there is a physical Ethernet connected.