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

Commit 5e192393 authored by Chalard Jean's avatar Chalard Jean Committed by android-build-merger
Browse files

Merge "Add a common test library." am: 96903842 am: 4343cf32 am: 5e8cf1bb

am: 47dcad54

Change-Id: I95f1961e1467b9167e58ff0577e1e7706a338e41
parents a30e1b27 47dcad54
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2019 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.
//

java_library {
    name: "net-tests-utils",
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],
    static_libs: [
        "kotlin-test",
    ],
}
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.testutils

import android.os.ConditionVariable
import android.os.Handler
import android.os.HandlerThread
import java.util.concurrent.Executor
import kotlin.test.fail

/**
 * Block until the specified Handler or HandlerThread becomes idle, or until timeoutMs has passed.
 */
fun Handler.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this, timeoutMs)
fun HandlerThread.waitForIdle(timeoutMs: Long) = waitForIdleHandler(this.threadHandler, timeoutMs)
fun waitForIdleHandler(handler: HandlerThread, timeoutMs: Long) {
    waitForIdleHandler(handler.threadHandler, timeoutMs)
}
fun waitForIdleHandler(handler: Handler, timeoutMs: Long) {
    val cv = ConditionVariable(false)
    handler.post(cv::open)
    if (!cv.block(timeoutMs)) {
        fail("Handler did not become idle after ${timeoutMs}ms")
    }
}

/**
 * Block until the given Serial Executor becomes idle, or until timeoutMs has passed.
 */
fun waitForIdleSerialExecutor(executor: Executor, timeoutMs: Long) {
    val cv = ConditionVariable()
    executor.execute(cv::open)
    if (!cv.block(timeoutMs)) {
        fail("Executor did not become idle after ${timeoutMs}ms")
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ android_test {
    static_libs: [
        "androidx.test.rules",
        "mockito-target-extended-minus-junit4",
        "net-tests-utils",
        "NetworkStackBase",
        "testables",
    ],
+4 −11
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.networkstack.R;
import com.android.networkstack.metrics.DataStallDetectionStats;
import com.android.networkstack.metrics.DataStallStatsUtils;
import com.android.testutils.HandlerUtilsKt;

import org.junit.After;
import org.junit.Before;
@@ -420,7 +421,7 @@ public class NetworkMonitorTest {
        final WrappedNetworkMonitor nm = new WrappedNetworkMonitor();
        nm.start();
        setNetworkCapabilities(nm, nc);
        waitForIdle(nm.getHandler());
        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
        mCreatedNetworkMonitors.add(nm);
        return nm;
    }
@@ -437,15 +438,7 @@ public class NetworkMonitorTest {

    private void setNetworkCapabilities(NetworkMonitor nm, NetworkCapabilities nc) {
        nm.notifyNetworkCapabilitiesChanged(nc);
        waitForIdle(nm.getHandler());
    }

    private void waitForIdle(Handler handler) {
        final ConditionVariable cv = new ConditionVariable(false);
        handler.post(cv::open);
        if (!cv.block(HANDLER_TIMEOUT_MS)) {
            fail("Timed out waiting for handler");
        }
        HandlerUtilsKt.waitForIdle(nm.getHandler(), HANDLER_TIMEOUT_MS);
    }

    @Test
@@ -1125,7 +1118,7 @@ public class NetworkMonitorTest {
        } catch (RemoteException e) {
            fail("Unexpected exception: " + e);
        }
        waitForIdle(monitor.getHandler());
        HandlerUtilsKt.waitForIdle(monitor.getHandler(), HANDLER_TIMEOUT_MS);

        return monitor;
    }
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ java_defaults {
        "framework-protos",
        "androidx.test.rules",
        "mockito-target-minus-junit4",
        "net-tests-utils",
        "platform-test-annotations",
        "services.core",
        "services.net",
Loading