From 46755dbba7f6210a87b23f10a4c935a7b377cd1c Mon Sep 17 00:00:00 2001 From: Jason LeBrun Date: Mon, 30 Nov 2020 17:51:29 -0800 Subject: [PATCH] Propagate exception from OperationQueue.enqueueAndWait PiperOrigin-RevId: 344924468 --- java/arcs/core/storage/util/OperationQueue.kt | 6 +++++- javatests/arcs/core/storage/util/BUILD | 5 +---- .../arcs/core/storage/util/SimpleQueueTest.kt | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/java/arcs/core/storage/util/OperationQueue.kt b/java/arcs/core/storage/util/OperationQueue.kt index 8bc1abc4274..680e6c20494 100644 --- a/java/arcs/core/storage/util/OperationQueue.kt +++ b/java/arcs/core/storage/util/OperationQueue.kt @@ -27,7 +27,11 @@ interface OperationQueue { suspend fun enqueueAndWait(block: suspend () -> T): T { val result = CompletableDeferred() enqueue { - result.complete(block()) + try { + result.complete(block()) + } catch (t: Throwable) { + result.completeExceptionally(t) + } } return result.await() } diff --git a/javatests/arcs/core/storage/util/BUILD b/javatests/arcs/core/storage/util/BUILD index bc9e4e8717a..1cf499b43fb 100644 --- a/javatests/arcs/core/storage/util/BUILD +++ b/javatests/arcs/core/storage/util/BUILD @@ -14,11 +14,8 @@ arcs_kt_jvm_test_suite( "//java/arcs/core/common", "//java/arcs/core/crdt", "//java/arcs/core/storage:proxy", - "//java/arcs/core/storage:reference", - "//java/arcs/core/storage:storage_key", - "//java/arcs/core/storage/keys", - "//java/arcs/core/storage/referencemode", "//java/arcs/core/storage/util", + "//java/arcs/core/testutil", "//third_party/java/junit:junit-android", "//third_party/java/truth:truth-android", "//third_party/kotlin/kotlinx_atomicfu", diff --git a/javatests/arcs/core/storage/util/SimpleQueueTest.kt b/javatests/arcs/core/storage/util/SimpleQueueTest.kt index e8254d0fcb2..49b53225ce8 100644 --- a/javatests/arcs/core/storage/util/SimpleQueueTest.kt +++ b/javatests/arcs/core/storage/util/SimpleQueueTest.kt @@ -1,5 +1,6 @@ package arcs.core.storage.util +import arcs.core.testutil.assertSuspendingThrows import com.google.common.truth.Truth.assertThat import kotlinx.atomicfu.atomic import kotlinx.coroutines.CompletableDeferred @@ -33,6 +34,19 @@ class SimpleQueueTest { assertThat(ran).isTrue() } + @Test + fun enqueueAndWait_propagatesException() = runBlockingTest { + val queue = SimpleQueue() + class FakeException : Exception("test dummy") + val dummyException = FakeException() + + val receivedException = assertSuspendingThrows(FakeException::class) { + queue.enqueueAndWait { throw dummyException } + } + // Strangely, the returned exception seems to be wrapped in itself... is this a Kotlin bug? + assertThat(receivedException.cause).isEqualTo(dummyException) + } + @Test fun onEmptyIsCalled() = runBlockingTest { val deferred = CompletableDeferred()