Merge "Add a common test library."

am: 9690384221

Change-Id: I39702b09925304a619c4d4f94435c965078dee8f
This commit is contained in:
Chalard Jean
2019-05-29 05:32:13 -07:00
committed by android-build-merger
11 changed files with 101 additions and 89 deletions

View File

@@ -0,0 +1,26 @@
//
// 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",
],
}

View File

@@ -0,0 +1,50 @@
/*
* 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")
}
}

View File

@@ -23,6 +23,7 @@ android_test {
static_libs: [
"androidx.test.rules",
"mockito-target-extended-minus-junit4",
"net-tests-utils",
"NetworkStackBase",
"testables",
],

View File

@@ -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;
}

View File

@@ -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",

View File

@@ -16,8 +16,6 @@
package android.net.nsd;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@@ -40,6 +38,7 @@ import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.AsyncChannel;
import com.android.testutils.HandlerUtilsKt;
import org.junit.After;
import org.junit.Before;
@@ -74,7 +73,7 @@ public class NsdManagerTest {
@After
public void tearDown() throws Exception {
mServiceHandler.waitForIdle(mTimeoutMs);
HandlerUtilsKt.waitForIdle(mServiceHandler, mTimeoutMs);
mServiceHandler.chan.disconnect();
mServiceHandler.stop();
if (mManager != null) {
@@ -334,7 +333,7 @@ public class NsdManagerTest {
}
int verifyRequest(int expectedMessageType) {
mServiceHandler.waitForIdle(mTimeoutMs);
HandlerUtilsKt.waitForIdle(mServiceHandler, mTimeoutMs);
verify(mServiceHandler, timeout(mTimeoutMs)).handleMessage(any());
reset(mServiceHandler);
Message received = mServiceHandler.getLastMessage();
@@ -366,10 +365,6 @@ public class NsdManagerTest {
lastMessage.copyFrom(msg);
}
void waitForIdle(long timeoutMs) {
waitForIdleHandler(this, timeoutMs);
}
@Override
public void handleMessage(Message msg) {
setLastMessage(msg);

View File

@@ -68,10 +68,6 @@ import static android.net.NetworkPolicyManager.RULE_REJECT_ALL;
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
import static android.net.RouteInfo.RTN_UNREACHABLE;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static com.android.internal.util.TestUtils.waitForIdleLooper;
import static com.android.internal.util.TestUtils.waitForIdleSerialExecutor;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -196,6 +192,7 @@ import com.android.server.connectivity.Tethering;
import com.android.server.connectivity.Vpn;
import com.android.server.net.NetworkPinner;
import com.android.server.net.NetworkPolicyManagerInternal;
import com.android.testutils.HandlerUtilsKt;
import org.junit.After;
import org.junit.Before;
@@ -375,19 +372,19 @@ public class ConnectivityServiceTest {
public void waitForIdle(int timeoutMsAsInt) {
long timeoutMs = timeoutMsAsInt;
waitForIdleHandler(mService.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
waitForIdle(mCellNetworkAgent, timeoutMs);
waitForIdle(mWiFiNetworkAgent, timeoutMs);
waitForIdle(mEthernetNetworkAgent, timeoutMs);
waitForIdleHandler(mService.mHandlerThread, timeoutMs);
waitForIdleLooper(ConnectivityThread.getInstanceLooper(), timeoutMs);
HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(ConnectivityThread.get(), timeoutMs);
}
public void waitForIdle(MockNetworkAgent agent, long timeoutMs) {
if (agent == null) {
return;
}
waitForIdleHandler(agent.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(agent.mHandlerThread, timeoutMs);
}
private void waitForIdle() {
@@ -1220,7 +1217,7 @@ public class ConnectivityServiceTest {
}
public void waitForIdle(int timeoutMs) {
waitForIdleHandler(mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(mHandlerThread, timeoutMs);
}
public void waitForIdle() {
@@ -4107,7 +4104,7 @@ public class ConnectivityServiceTest {
}
public void assertNoCallback() {
waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
HandlerUtilsKt.waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
CallbackValue cv = mCallbacks.peek();
assertNull("Unexpected callback: " + cv, cv);
}

View File

@@ -51,7 +51,6 @@ import android.net.LinkProperties;
import android.net.NetworkStats;
import android.net.RouteInfo;
import android.net.util.SharedLog;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.INetworkManagementService;
import android.os.Looper;
@@ -63,6 +62,7 @@ import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.test.FakeSettingsProvider;
import com.android.testutils.HandlerUtilsKt;
import org.junit.After;
import org.junit.Before;
@@ -90,6 +90,7 @@ public class OffloadControllerTest {
private static final String IPV6_DISCARD_PREFIX = "100::/64";
private static final String USB_PREFIX = "192.168.42.0/24";
private static final String WIFI_PREFIX = "192.168.43.0/24";
private static final long WAIT_FOR_IDLE_TIMEOUT = 2 * 1000;
@Mock private OffloadHardwareInterface mHardware;
@Mock private ApplicationInfo mApplicationInfo;
@@ -131,9 +132,7 @@ public class OffloadControllerTest {
}
private void waitForIdle() {
ConditionVariable cv = new ConditionVariable();
new Handler(Looper.getMainLooper()).post(() -> { cv.open(); });
cv.block();
HandlerUtilsKt.waitForIdle(new Handler(Looper.getMainLooper()), WAIT_FOR_IDLE_TIMEOUT);
}
private OffloadController makeOffloadController() throws Exception {

View File

@@ -28,8 +28,6 @@ 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.mockito.Matchers.any;
@@ -56,6 +54,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.internal.net.VpnInfo;
import com.android.server.net.NetworkStatsServiceTest.LatchedHandler;
import com.android.testutils.HandlerUtilsKt;
import org.junit.Before;
import org.junit.Test;
@@ -457,7 +456,7 @@ public class NetworkStatsObserversTest {
}
private void waitForObserverToIdle() {
waitForIdleHandler(mObserverHandlerThread, WAIT_TIMEOUT_MS);
waitForIdleHandler(mHandler, WAIT_TIMEOUT_MS);
HandlerUtilsKt.waitForIdle(mObserverHandlerThread, WAIT_TIMEOUT_MS);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT_MS);
}
}

View File

@@ -52,7 +52,6 @@ import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
import static org.junit.Assert.assertEquals;
@@ -105,6 +104,7 @@ import com.android.internal.util.ArrayUtils;
import com.android.internal.util.test.BroadcastInterceptingContext;
import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
import com.android.testutils.HandlerUtilsKt;
import libcore.io.IoUtils;
@@ -1321,8 +1321,6 @@ public class NetworkStatsServiceTest {
expectNetworkStatsSummary(buildEmptyStats());
expectNetworkStatsUidDetail(buildEmptyStats());
// Register and verify request and that binder was called
DataUsageRequest request =
mService.registerUsageCallback(mServiceContext.getOpPackageName(), inputRequest,
@@ -1334,14 +1332,11 @@ public class NetworkStatsServiceTest {
// Send dummy message to make sure that any previous message has been handled
mHandler.sendMessage(mHandler.obtainMessage(-1));
waitForIdleHandler(mHandler, WAIT_TIMEOUT);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
// Make sure that the caller binder gets connected
verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt());
// modify some number on wifi, and trigger poll event
// not enough traffic to call data usage callback
incrementCurrentTime(HOUR_IN_MILLIS);
@@ -1674,7 +1669,7 @@ public class NetworkStatsServiceTest {
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
// Send dummy message to make sure that any previous message has been handled
mHandler.sendMessage(mHandler.obtainMessage(-1));
waitForIdleHandler(mHandler, WAIT_TIMEOUT);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
}
static class LatchedHandler extends Handler {

View File

@@ -19,56 +19,12 @@ package com.android.internal.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import java.util.concurrent.Executor;
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) {
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");
}
}
/**
* Block until the given Serial Executor becomes idle, or until timeoutMs has passed.
*/
public static void waitForIdleSerialExecutor(@NonNull Executor executor, long timeoutMs) {
final ConditionVariable cv = new ConditionVariable();
executor.execute(() -> cv.open());
if (!cv.block(timeoutMs)) {
fail(executor.toString() + " did not become idle after " + timeoutMs + " ms");
}
}
/**
* Return a new instance of {@code T} after being parceled then unparceled.
*/