1717package kotlinx.coroutines.experimental.future
1818
1919import kotlinx.coroutines.experimental.*
20+ import kotlinx.coroutines.experimental.CancellationException
2021import org.hamcrest.core.IsEqual
2122import org.junit.Assert.*
2223import org.junit.Before
2324import 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.*
2726import java.util.concurrent.atomic.AtomicInteger
2827import java.util.concurrent.locks.ReentrantLock
2928import kotlin.concurrent.withLock
@@ -280,17 +279,22 @@ class FutureTest : TestBase() {
280279
281280 @Test
282281 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+ }
284285 val deferred = future.asDeferred()
285286
286287 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)
288291
289292 try {
290293 deferred.await()
291294 fail(" deferred.await() should throw an exception" )
292295 } catch (e: Exception ) {
293- assertEquals(" something went wrong" , e.cause!! .message)
296+ assertTrue(e is TestException )
297+ assertEquals(" something went wrong" , e.message)
294298 }
295299 }
296300
@@ -299,7 +303,7 @@ class FutureTest : TestBase() {
299303 val lock = ReentrantLock ().apply { lock() }
300304
301305 val deferred: Deferred <Int > = CompletableFuture .supplyAsync {
302- lock.withLock { throw Exception (" something went wrong" ) }
306+ lock.withLock { throw TestException (" something went wrong" ) }
303307 }.asDeferred()
304308
305309 assertFalse(deferred.isCompleted)
@@ -310,10 +314,16 @@ class FutureTest : TestBase() {
310314 fail(" deferred.await() should throw an exception" )
311315 } catch (e: Exception ) {
312316 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
314322 }
315323 }
316324
325+ class TestException (message : String ) : Exception(message)
326+
317327 private fun wrapContinuation (wrapper : (() -> Unit ) -> Unit ): CoroutineDispatcher = object : CoroutineDispatcher () {
318328 override fun dispatch (context : CoroutineContext , block : Runnable ) {
319329 wrapper {
0 commit comments