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

Commit 741fdea5 authored by Hugo Benichi's avatar Hugo Benichi Committed by android-build-merger
Browse files

Merge "Networking unit tests: fix some flaky tests" am: 501bae64

am: b87cf12a

Change-Id: Id95a8ef670b8f84664c518d19df449720ae90eb2
parents d02751ef b87cf12a
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -349,7 +349,6 @@ public class NsdManagerTest {
                chan.connect(mContext, this, msg.replyTo);
                chan.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
            }

        }

        public static MockServiceHandler create(Context context) {
+3 −3
Original line number Diff line number Diff line
@@ -33,8 +33,8 @@ import java.util.Vector;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class SharedLogTest {
    private static final String TIMESTAMP_PATTERN = "\\d{2}:\\d{2}:\\d{2}\\.\\d{3}";
    private static final String TIMESTAMP = "HH:MM:SS.xxx";
    private static final String TIMESTAMP_PATTERN = "\\d{2}:\\d{2}:\\d{2}";
    private static final String TIMESTAMP = "HH:MM:SS";

    @Test
    public void testBasicOperation() {
@@ -85,7 +85,7 @@ public class SharedLogTest {
            String got = lines[i];
            String want = expected[i];
            assertTrue(String.format("'%s' did not contain '%s'", got, want), got.endsWith(want));
            assertTrue(String.format("'%s' did not contain a HH:MM:SS.xxx timestamp", got),
            assertTrue(String.format("'%s' did not contain a %s timestamp", got, TIMESTAMP),
                    got.replaceFirst(TIMESTAMP_PATTERN, TIMESTAMP).contains(TIMESTAMP));
        }
    }
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.util;

import static org.junit.Assert.fail;

import android.os.ConditionVariable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

public final class TestUtils {
    private TestUtils() { }

    /**
     * Block until the given Handler thread becomes idle, or until timeoutMs has passed.
     */
    public static void waitForIdleHandler(HandlerThread handlerThread, long timeoutMs) {
        // TODO: convert to getThreadHandler once it is available on aosp
        waitForIdleLooper(handlerThread.getLooper(), timeoutMs);
    }

    /**
     * Block until the given Looper becomes idle, or until timeoutMs has passed.
     */
    public static void waitForIdleLooper(Looper looper, long timeoutMs) {
        waitForIdleHandler(new Handler(looper), timeoutMs);
    }

    /**
     * Block until the given Handler becomes idle, or until timeoutMs has passed.
     */
    public static void waitForIdleHandler(Handler handler, long timeoutMs) {
        final ConditionVariable cv = new ConditionVariable();
        handler.post(() -> cv.open());
        if (!cv.block(timeoutMs)) {
            fail(handler.toString() + " did not become idle after " + timeoutMs + " ms");
        }
    }
}
+5 −15
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import static android.net.ConnectivityManager.TYPE_WIFI;
import static android.net.ConnectivityManager.getNetworkTypeName;
import static android.net.NetworkCapabilities.*;

import static com.android.internal.util.TestUtils.waitForIdleHandler;

import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
@@ -212,20 +214,8 @@ public class ConnectivityServiceTest extends AndroidTestCase {
        }
    }

    /**
     * Block until the given handler becomes idle, or until timeoutMs has passed.
     */
    private static void waitForIdleHandler(HandlerThread handlerThread, int timeoutMs) {
        final ConditionVariable cv = new ConditionVariable();
        final Handler handler = new Handler(handlerThread.getLooper());
        handler.post(() -> cv.open());
        if (!cv.block(timeoutMs)) {
            fail("HandlerThread " + handlerThread.getName() +
                    " did not become idle after " + timeoutMs + " ms");
        }
    }

    public void waitForIdle(int timeoutMs) {
    public void waitForIdle(int timeoutMsAsInt) {
        long timeoutMs = timeoutMsAsInt;
        waitForIdleHandler(mService.mHandlerThread, timeoutMs);
        waitForIdle(mCellNetworkAgent, timeoutMs);
        waitForIdle(mWiFiNetworkAgent, timeoutMs);
@@ -233,7 +223,7 @@ public class ConnectivityServiceTest extends AndroidTestCase {
        waitForIdleHandler(mService.mHandlerThread, timeoutMs);
    }

    public void waitForIdle(MockNetworkAgent agent, int timeoutMs) {
    public void waitForIdle(MockNetworkAgent agent, long timeoutMs) {
        if (agent == null) {
            return;
        }
+9 −20
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;

import static com.android.internal.util.TestUtils.waitForIdleHandler;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -56,7 +58,6 @@ import android.util.ArrayMap;

import com.android.internal.net.VpnInfo;
import com.android.server.net.NetworkStatsService;
import com.android.server.net.NetworkStatsServiceTest.IdleableHandlerThread;
import com.android.server.net.NetworkStatsServiceTest.LatchedHandler;

import java.util.ArrayList;
@@ -102,7 +103,7 @@ public class NetworkStatsObserversTest {

    private long mElapsedRealtime;

    private IdleableHandlerThread mObserverHandlerThread;
    private HandlerThread mObserverHandlerThread;
    private Handler mObserverNoopHandler;

    private LatchedHandler mHandler;
@@ -118,7 +119,7 @@ public class NetworkStatsObserversTest {
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        mObserverHandlerThread = new IdleableHandlerThread("HandlerThread");
        mObserverHandlerThread = new HandlerThread("HandlerThread");
        mObserverHandlerThread.start();
        final Looper observerLooper = mObserverHandlerThread.getLooper();
        mStatsObservers = new NetworkStatsObservers() {
@@ -319,7 +320,7 @@ public class NetworkStatsObserversTest {
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }

    @Test
@@ -356,7 +357,7 @@ public class NetworkStatsObserversTest {
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }

    @Test
@@ -429,7 +430,7 @@ public class NetworkStatsObserversTest {
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }

    @Test
@@ -470,19 +471,7 @@ public class NetworkStatsObserversTest {
    }

    private void waitForObserverToIdle() {
        waitForIdleLooper(mObserverHandlerThread.getLooper(), WAIT_TIMEOUT_MS);
        waitForIdleLooper(mHandler.getLooper(), WAIT_TIMEOUT_MS);
    }

    // TODO: unify with ConnectivityService.waitForIdleHandler and
    // NetworkServiceStatsTest.IdleableHandlerThread
    private static void waitForIdleLooper(Looper looper, long timeoutMs) {
        final ConditionVariable cv = new ConditionVariable();
        final Handler handler = new Handler(looper);
        handler.post(() -> cv.open());
        if (!cv.block(timeoutMs)) {
            fail("Looper did not become idle after " + timeoutMs + " ms");
        waitForIdleHandler(mObserverHandlerThread, WAIT_TIMEOUT_MS);
        waitForIdleHandler(mHandler, WAIT_TIMEOUT_MS);
    }
}

}
Loading