diff options
author | Hansen Kurli <hkurli@google.com> | 2023-05-22 11:06:07 +0000 |
---|---|---|
committer | Hansen Kurli <hkurli@google.com> | 2023-06-13 06:18:55 +0000 |
commit | b1d5f322dd99496fe5ab4ef240f9220bbcc92634 (patch) | |
tree | 90f0404edc70c1bbb354024cfe2ed56b01020438 | |
parent | 3155fcbfa2e005e78f01b3db1f9b9166e64b848b (diff) | |
download | net-android14-dev.tar.gz |
Use ThrowingSupplier for visibleOnHandlerThread.android14-dev
In order to return values from functions that are run on the handler
thread, use a ThrowingSupplier instead of a ThrowingRunnable.
Also maintain the case with ThrowingRunnable by overloading.
Bug: 281646074
Bug: 283885097
Test: NetworkStaticLibTests
(cherry picked from https://android-review.googlesource.com/q/commit:e51b449da4dfb751652be93b5a88b424e682e9cb)
Merged-In: Ic1b86f9f764997ce5d6848f04185194a961d1106
Change-Id: Ic1b86f9f764997ce5d6848f04185194a961d1106
-rw-r--r-- | common/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt | 9 | ||||
-rw-r--r-- | common/testutils/devicetests/com/android/testutils/HandlerUtils.kt | 19 |
2 files changed, 21 insertions, 7 deletions
diff --git a/common/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt b/common/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt index 30e0dafe..0f6fa48b 100644 --- a/common/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt +++ b/common/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt @@ -18,8 +18,10 @@ package com.android.testutils import android.os.Handler import android.os.HandlerThread +import com.android.testutils.FunctionalUtils.ThrowingSupplier import kotlin.test.assertEquals import kotlin.test.assertFailsWith +import kotlin.test.assertNull import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @@ -69,13 +71,18 @@ class HandlerUtilsTest { repeat(ATTEMPTS) { attempt -> var x = -10 - visibleOnHandlerThread(handler) { x = attempt } + var y = -11 + y = visibleOnHandlerThread(handler, ThrowingSupplier<Int> { x = attempt; attempt }) assertEquals(attempt, x) + assertEquals(attempt, y) handler.post { assertEquals(attempt, x) } } assertFailsWith<IllegalArgumentException> { visibleOnHandlerThread(handler) { throw IllegalArgumentException() } } + + // Null values may be returned by the supplier + assertNull(visibleOnHandlerThread(handler, ThrowingSupplier<Nothing?> { null })) } } diff --git a/common/testutils/devicetests/com/android/testutils/HandlerUtils.kt b/common/testutils/devicetests/com/android/testutils/HandlerUtils.kt index aa252a56..f00ca116 100644 --- a/common/testutils/devicetests/com/android/testutils/HandlerUtils.kt +++ b/common/testutils/devicetests/com/android/testutils/HandlerUtils.kt @@ -23,6 +23,7 @@ import android.os.Handler import android.os.HandlerThread import android.util.Log import com.android.testutils.FunctionalUtils.ThrowingRunnable +import com.android.testutils.FunctionalUtils.ThrowingSupplier import java.lang.Exception import java.util.concurrent.Executor import kotlin.test.fail @@ -55,7 +56,8 @@ fun waitForIdleSerialExecutor(executor: Executor, timeoutMs: Long) { } /** - * Executes a block of code, making its side effects visible on the caller and the handler thread + * Executes a block of code that returns a value, making its side effects visible on the caller and + * the handler thread. * * After this function returns, the side effects of the passed block of code are guaranteed to be * observed both on the thread running the handler and on the thread running this method. @@ -63,15 +65,15 @@ fun waitForIdleSerialExecutor(executor: Executor, timeoutMs: Long) { * until it's executed, so keep in mind this method will block, (including, if the handler isn't * running, blocking forever). */ -fun visibleOnHandlerThread(handler: Handler, r: ThrowingRunnable) { +fun <T> visibleOnHandlerThread(handler: Handler, supplier: ThrowingSupplier<T>): T { val cv = ConditionVariable() - var e: Exception? = null + var rv: Result<T> = Result.failure(RuntimeException("Not run")) handler.post { try { - r.run() + rv = Result.success(supplier.get()) } catch (exception: Exception) { Log.e(TAG, "visibleOnHandlerThread caught exception", exception) - e = exception + rv = Result.failure(exception) } cv.open() } @@ -79,5 +81,10 @@ fun visibleOnHandlerThread(handler: Handler, r: ThrowingRunnable) { // and this thread also has seen the change (since cv.open() happens-before cv.block() // returns). cv.block() - e?.let { throw it } + return rv.getOrThrow() +} + +/** Overload of visibleOnHandlerThread but executes a block of code that does not return a value. */ +inline fun visibleOnHandlerThread(handler: Handler, r: ThrowingRunnable){ + visibleOnHandlerThread(handler, ThrowingSupplier<Unit> { r.run() }) } |