17
17
package kotlinx.coroutines.experimental.future
18
18
19
19
import kotlinx.coroutines.experimental.*
20
+ import kotlinx.coroutines.experimental.CancellationException
20
21
import org.hamcrest.core.IsEqual
21
22
import org.junit.Assert.*
22
23
import org.junit.Before
23
24
import org.junit.Test
24
- import java.util.concurrent.CompletableFuture
25
- import java.util.concurrent.CompletionStage
26
- import java.util.concurrent.ExecutionException
25
+ import java.util.concurrent.*
27
26
import java.util.concurrent.atomic.AtomicInteger
28
27
import java.util.concurrent.locks.ReentrantLock
29
28
import kotlin.concurrent.withLock
@@ -280,17 +279,22 @@ class FutureTest : TestBase() {
280
279
281
280
@Test
282
281
fun testFailedFutureAsDeferred () = runBlocking {
283
- val future = CompletableFuture <Int >().apply { completeExceptionally(Exception (" something went wrong" )) }
282
+ val future = CompletableFuture <Int >().apply {
283
+ completeExceptionally(TestException (" something went wrong" ))
284
+ }
284
285
val deferred = future.asDeferred()
285
286
286
287
assertTrue(deferred.isCompletedExceptionally)
287
- assertEquals(" something went wrong" , deferred.getCompletionExceptionOrNull()!! .cause!! .message)
288
+ val completionException = deferred.getCompletionExceptionOrNull()!!
289
+ assertTrue(completionException is TestException )
290
+ assertEquals(" something went wrong" , completionException.message)
288
291
289
292
try {
290
293
deferred.await()
291
294
fail(" deferred.await() should throw an exception" )
292
295
} catch (e: Exception ) {
293
- assertEquals(" something went wrong" , e.cause!! .message)
296
+ assertTrue(e is TestException )
297
+ assertEquals(" something went wrong" , e.message)
294
298
}
295
299
}
296
300
@@ -299,7 +303,7 @@ class FutureTest : TestBase() {
299
303
val lock = ReentrantLock ().apply { lock() }
300
304
301
305
val deferred: Deferred <Int > = CompletableFuture .supplyAsync {
302
- lock.withLock { throw Exception (" something went wrong" ) }
306
+ lock.withLock { throw TestException (" something went wrong" ) }
303
307
}.asDeferred()
304
308
305
309
assertFalse(deferred.isCompleted)
@@ -310,10 +314,16 @@ class FutureTest : TestBase() {
310
314
fail(" deferred.await() should throw an exception" )
311
315
} catch (e: Exception ) {
312
316
assertTrue(deferred.isCompletedExceptionally)
313
- assertEquals(" something went wrong" , e.cause!! .message)
317
+ assertTrue(e is CompletionException ) // that's how supplyAsync wraps it
318
+ val cause = e.cause!!
319
+ assertTrue(cause is TestException )
320
+ assertEquals(" something went wrong" , cause.message)
321
+ assertSame(e, deferred.getCompletionExceptionOrNull()) // same exception is returns as thrown
314
322
}
315
323
}
316
324
325
+ class TestException (message : String ) : Exception(message)
326
+
317
327
private fun wrapContinuation (wrapper : (() -> Unit ) -> Unit ): CoroutineDispatcher = object : CoroutineDispatcher () {
318
328
override fun dispatch (context : CoroutineContext , block : Runnable ) {
319
329
wrapper {
0 commit comments