diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-16 18:15:52 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-16 18:15:52 +0000 |
commit | d5208ee84f2a7fb5986931038c1c7ddf22c86786 (patch) | |
tree | 90f0404edc70c1bbb354024cfe2ed56b01020438 | |
parent | 126582b8d8869641d34f13d65adb070ab29e8cd7 (diff) | |
parent | 7c4073f3f973e54ac8354cd949b4d0e0a3e7c37a (diff) | |
download | net-frc_340821000.tar.gz |
Snap for 10337615 from 7c4073f3f973e54ac8354cd949b4d0e0a3e7c37a to aml-frc-releasefrc_340821000frc_340819280frc_340819220frc_340819190frc_340819030frc_340819020frc_340819010frc_340818170frc_340818110
Change-Id: Iec223480a7e388d87c89b8a8906ab6f045b1ac4c
-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() }) } |