Skip to content

Commit 6121663

Browse files
committed
Improve FutureTest to guarantee testing of CompletionStage.await slow path
1 parent c8af35c commit 6121663

File tree

1 file changed

+22
-14
lines changed
  • integration/kotlinx-coroutines-jdk8/src/test/kotlin/kotlinx/coroutines/experimental/future

1 file changed

+22
-14
lines changed

integration/kotlinx-coroutines-jdk8/src/test/kotlin/kotlinx/coroutines/experimental/future/FutureTest.kt

+22-14
Original file line numberDiff line numberDiff line change
@@ -90,60 +90,68 @@ class FutureTest : TestBase() {
9090
@Test
9191
fun testCompletedFutureExceptionally() {
9292
val toAwait = CompletableFuture<String>()
93-
toAwait.completeExceptionally(RuntimeException("O"))
94-
val future = future<String> {
93+
toAwait.completeExceptionally(TestException("O"))
94+
val future = future {
9595
try {
9696
toAwait.await()
97-
} catch (e: RuntimeException) {
97+
} catch (e: TestException) {
9898
e.message!!
9999
} + "K"
100100
}
101101
assertThat(future.get(), IsEqual("OK"))
102102
}
103103

104104
@Test
105+
// Test fast-path of CompletionStage.await() extension
105106
fun testCompletedCompletionStageExceptionally() {
106107
val completable = CompletableFuture<String>()
107108
val toAwait: CompletionStage<String> = completable
108-
completable.completeExceptionally(RuntimeException("O"))
109-
val future = future<String> {
109+
completable.completeExceptionally(TestException("O"))
110+
val future = future {
110111
try {
111112
toAwait.await()
112-
} catch (e: RuntimeException) {
113+
} catch (e: TestException) {
113114
e.message!!
114115
} + "K"
115116
}
116117
assertThat(future.get(), IsEqual("OK"))
117118
}
118119

119120
@Test
120-
fun testWaitForFutureWithException() {
121+
// Test slow-path of CompletionStage.await() extension
122+
fun testWaitForFutureWithException() = runTest {
123+
expect(1)
121124
val toAwait = CompletableFuture<String>()
122-
val future = future<String> {
125+
val future = future(coroutineContext, start = CoroutineStart.UNDISPATCHED) {
123126
try {
124-
toAwait.await()
125-
} catch (e: RuntimeException) {
127+
expect(2)
128+
toAwait.await() // will suspend (slow path)
129+
} catch (e: TestException) {
130+
expect(4)
126131
e.message!!
127132
} + "K"
128133
}
134+
expect(3)
129135
assertFalse(future.isDone)
130-
toAwait.completeExceptionally(RuntimeException("O"))
136+
toAwait.completeExceptionally(TestException("O"))
137+
yield() // to future coroutine
131138
assertThat(future.get(), IsEqual("OK"))
139+
finish(5)
132140
}
133141

134142
@Test
135143
fun testWaitForCompletionStageWithException() {
136144
val completable = CompletableFuture<String>()
137145
val toAwait: CompletionStage<String> = completable
138-
val future = future<String> {
146+
val future = future {
139147
try {
140148
toAwait.await()
141-
} catch (e: RuntimeException) {
149+
} catch (e: TestException) {
142150
e.message!!
143151
} + "K"
144152
}
145153
assertFalse(future.isDone)
146-
completable.completeExceptionally(RuntimeException("O"))
154+
completable.completeExceptionally(TestException("O"))
147155
assertThat(future.get(), IsEqual("OK"))
148156
}
149157

0 commit comments

Comments
 (0)