Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cbaa8dd authored by Chiachang Wang's avatar Chiachang Wang
Browse files

Update sequence and method of the mock object to prevent flaky

Use doReturn family of methods as suggestion in the Mockito doc
to prevent certain rare occasions. Also mock the tcp polling
interval before creating NetworkMonitor to prevent interacting
mock while NetworkMonitor's handler may not be idle.

Bug: 161426331
Test: atest NetworkStackNextTests:NetworkMonitorTest \
      --rerun-until-failure 100
Change-Id: I420bb197f4f62513b48828181a9907852cdfdc5f
parent 2b0d8046
Loading
Loading
Loading
Loading
+3 −4
Original line number Original line Diff line number Diff line
@@ -1931,12 +1931,11 @@ public class NetworkMonitorTest {
            throws Exception {
            throws Exception {
        assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q));
        assumeTrue(ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q));
        setupTcpDataStall();
        setupTcpDataStall();
        setTcpPollingInterval(0);
        // NM suspects data stall from TCP signal and sends data stall metrics.
        // NM suspects data stall from TCP signal and sends data stall metrics.
        setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_TCP);
        setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_TCP);
        final WrappedNetworkMonitor nm = prepareNetworkMonitorForVerifyDataStall(nc);
        final WrappedNetworkMonitor nm = prepareNetworkMonitorForVerifyDataStall(nc);

        // Trigger a tcp event immediately.
        // Trigger a tcp event immediately.
        setTcpPollingInterval(0);
        nm.sendTcpPollingEvent();
        nm.sendTcpPollingEvent();
        // Allow only one transport type in the context of this test for simplification.
        // Allow only one transport type in the context of this test for simplification.
        final int[] transports = nc.getTransportTypes();
        final int[] transports = nc.getTransportTypes();
@@ -2546,8 +2545,8 @@ public class NetworkMonitorTest {
    }
    }


    private void setTcpPollingInterval(int time) {
    private void setTcpPollingInterval(int time) {
        when(mDependencies.getDeviceConfigPropertyInt(any(),
        doReturn(time).when(mDependencies).getDeviceConfigPropertyInt(any(),
                eq(CONFIG_DATA_STALL_TCP_POLLING_INTERVAL), anyInt())).thenReturn(time);
                eq(CONFIG_DATA_STALL_TCP_POLLING_INTERVAL), anyInt());
    }
    }


    private void setFallbackUrl(String url) {
    private void setFallbackUrl(String url) {