blob: a7b66d03138121ff01b26d9e373bae7a1d9abe8b [file] [log] [blame]
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlinx.coroutines.experimental.future
import kotlinx.coroutines.experimental.CoroutineDispatcher
import org.junit.Test
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.experimental.CoroutineContext
import org.junit.Assert.*
class FutureTest {
@Test
fun testSimple() {
val future = future {
CompletableFuture.supplyAsync {
"O"
}.await() + "K"
}
assertEquals("OK", future.get())
}
@Test
fun testWaitForCompletion() {
val toAwait = CompletableFuture<String>()
val future = future {
toAwait.await() + "K"
}
assertFalse(future.isDone)
toAwait.complete("O")
assertEquals("OK", future.get())
}
@Test
fun testDoneFutureCompletedExceptionally() {
val toAwait = CompletableFuture<String>()
toAwait.completeExceptionally(RuntimeException("O"))
val future = future<String> {
try {
toAwait.await()
} catch (e: RuntimeException) {
e.message!!
} + "K"
}
assertFalse(future.isDone)
assertEquals("OK", future.get())
}
@Test
fun testAwaitedFutureCompletedExceptionally() {
val toAwait = CompletableFuture<String>()
val future = future<String> {
try {
toAwait.await()
} catch (e: RuntimeException) {
e.message!!
} + "K"
}
assertFalse(future.isDone)
toAwait.completeExceptionally(RuntimeException("O"))
assertEquals("OK", future.get())
}
@Test
fun testExceptionInsideCoroutine() {
val future = future {
if (CompletableFuture.supplyAsync { true }.await()) {
throw IllegalStateException("OK")
}
CompletableFuture.supplyAsync { "fail" }.await()
}
try {
future.get()
fail("'get' should've throw an exception")
} catch (e: ExecutionException) {
assertTrue(e.cause is IllegalStateException)
assertEquals("OK", e.cause!!.message)
}
}
@Test
fun testContinuationWrapped() {
val depth = AtomicInteger()
val future = future(wrapContinuation {
depth.andIncrement
it()
depth.andDecrement
}) {
assertEquals("Part before first suspension must be wrapped", 1, depth.get())
val result =
CompletableFuture.supplyAsync {
while (depth.get() > 0) ;
assertEquals("Part inside suspension point should not be wrapped", 0, depth.get())
"OK"
}.await()
assertEquals("Part after first suspension should be wrapped", 1, depth.get())
CompletableFuture.supplyAsync {
while (depth.get() > 0) ;
assertEquals("Part inside suspension point should not be wrapped", 0, depth.get())
"ignored"
}.await()
result
}
assertEquals("OK", future.get())
}
private fun wrapContinuation(wrapper: (() -> Unit) -> Unit): CoroutineDispatcher = object : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
wrapper {
block.run()
}
}
}
}