Loading packages/NetworkStack/tests/lib/Android.bp 0 → 100644 +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", ], } packages/NetworkStack/tests/lib/src/com/android/testutils/HandlerUtils.kt 0 → 100644 +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") } } packages/NetworkStack/tests/unit/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ android_test { static_libs: [ "androidx.test.rules", "mockito-target-extended-minus-junit4", "net-tests-utils", "NetworkStackBase", "testables", ], Loading packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java +4 −11 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; } Loading @@ -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 Loading Loading @@ -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; } Loading tests/net/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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 Loading
packages/NetworkStack/tests/lib/Android.bp 0 → 100644 +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", ], }
packages/NetworkStack/tests/lib/src/com/android/testutils/HandlerUtils.kt 0 → 100644 +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") } }
packages/NetworkStack/tests/unit/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ android_test { static_libs: [ "androidx.test.rules", "mockito-target-extended-minus-junit4", "net-tests-utils", "NetworkStackBase", "testables", ], Loading
packages/NetworkStack/tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java +4 −11 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; } Loading @@ -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 Loading Loading @@ -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; } Loading
tests/net/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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