@@ -90,60 +90,68 @@ class FutureTest : TestBase() {
90
90
@Test
91
91
fun testCompletedFutureExceptionally () {
92
92
val toAwait = CompletableFuture <String >()
93
- toAwait.completeExceptionally(RuntimeException (" O" ))
94
- val future = future< String > {
93
+ toAwait.completeExceptionally(TestException (" O" ))
94
+ val future = future {
95
95
try {
96
96
toAwait.await()
97
- } catch (e: RuntimeException ) {
97
+ } catch (e: TestException ) {
98
98
e.message!!
99
99
} + " K"
100
100
}
101
101
assertThat(future.get(), IsEqual (" OK" ))
102
102
}
103
103
104
104
@Test
105
+ // Test fast-path of CompletionStage.await() extension
105
106
fun testCompletedCompletionStageExceptionally () {
106
107
val completable = CompletableFuture <String >()
107
108
val toAwait: CompletionStage <String > = completable
108
- completable.completeExceptionally(RuntimeException (" O" ))
109
- val future = future< String > {
109
+ completable.completeExceptionally(TestException (" O" ))
110
+ val future = future {
110
111
try {
111
112
toAwait.await()
112
- } catch (e: RuntimeException ) {
113
+ } catch (e: TestException ) {
113
114
e.message!!
114
115
} + " K"
115
116
}
116
117
assertThat(future.get(), IsEqual (" OK" ))
117
118
}
118
119
119
120
@Test
120
- fun testWaitForFutureWithException () {
121
+ // Test slow-path of CompletionStage.await() extension
122
+ fun testWaitForFutureWithException () = runTest {
123
+ expect(1 )
121
124
val toAwait = CompletableFuture <String >()
122
- val future = future< String > {
125
+ val future = future(coroutineContext, start = CoroutineStart . UNDISPATCHED ) {
123
126
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 )
126
131
e.message!!
127
132
} + " K"
128
133
}
134
+ expect(3 )
129
135
assertFalse(future.isDone)
130
- toAwait.completeExceptionally(RuntimeException (" O" ))
136
+ toAwait.completeExceptionally(TestException (" O" ))
137
+ yield () // to future coroutine
131
138
assertThat(future.get(), IsEqual (" OK" ))
139
+ finish(5 )
132
140
}
133
141
134
142
@Test
135
143
fun testWaitForCompletionStageWithException () {
136
144
val completable = CompletableFuture <String >()
137
145
val toAwait: CompletionStage <String > = completable
138
- val future = future< String > {
146
+ val future = future {
139
147
try {
140
148
toAwait.await()
141
- } catch (e: RuntimeException ) {
149
+ } catch (e: TestException ) {
142
150
e.message!!
143
151
} + " K"
144
152
}
145
153
assertFalse(future.isDone)
146
- completable.completeExceptionally(RuntimeException (" O" ))
154
+ completable.completeExceptionally(TestException (" O" ))
147
155
assertThat(future.get(), IsEqual (" OK" ))
148
156
}
149
157
0 commit comments