diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-15 23:16:25 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-15 23:16:25 +0000 |
commit | 87c5e02a6a2f7fe95148c244781c225f6c3da189 (patch) | |
tree | 90f0404edc70c1bbb354024cfe2ed56b01020438 | |
parent | 2365269099af68e37e007cbb8fa344057d65b816 (diff) | |
parent | 8859b6613e37d6346170db9cec54d8fd0ae3cc88 (diff) | |
download | net-android14-d1-s7-release.tar.gz |
Snap for 10331821 from 8859b6613e37d6346170db9cec54d8fd0ae3cc88 to udc-d1-releaseandroid-14.0.0_r9android-14.0.0_r8android-14.0.0_r7android-14.0.0_r6android-14.0.0_r5android-14.0.0_r4android-14.0.0_r3android-14.0.0_r12android-14.0.0_r11android-14.0.0_r10android14-d1-s7-releaseandroid14-d1-s6-releaseandroid14-d1-s5-releaseandroid14-d1-s4-releaseandroid14-d1-s3-releaseandroid14-d1-s2-releaseandroid14-d1-s1-releaseandroid14-d1-release
Change-Id: Ie0ad35983a8646f5a0ee6d20d5d08feec94cae79
-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() }) } |