Skip to content

Commit

Permalink
Propagate exception from OperationQueue.enqueueAndWait
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 344924468
  • Loading branch information
jblebrun authored and arcs-c3po committed Dec 1, 2020
1 parent 21de150 commit 46755db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion java/arcs/core/storage/util/OperationQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ interface OperationQueue {
suspend fun <T> enqueueAndWait(block: suspend () -> T): T {
val result = CompletableDeferred<T>()
enqueue {
result.complete(block())
try {
result.complete(block())
} catch (t: Throwable) {
result.completeExceptionally(t)
}
}
return result.await()
}
Expand Down
5 changes: 1 addition & 4 deletions javatests/arcs/core/storage/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions javatests/arcs/core/storage/util/SimpleQueueTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Unit>()
Expand Down

0 comments on commit 46755db

Please sign in to comment.