jsTest.PromiseTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-coroutines-core
Show all versions of kotlinx-coroutines-core
Coroutines support libraries for Kotlin
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlin.js.*
import kotlin.test.*
class PromiseTest : TestBase() {
@Test
fun testPromiseResolvedAsDeferred() = GlobalScope.promise {
val promise = Promise { resolve, _ ->
resolve("OK")
}
val deferred = promise.asDeferred()
assertEquals("OK", deferred.await())
}
@Test
fun testPromiseRejectedAsDeferred() = GlobalScope.promise {
lateinit var promiseReject: (Throwable) -> Unit
val promise = Promise { _, reject ->
promiseReject = reject
}
val deferred = promise.asDeferred()
// reject after converting to deferred to avoid "Unhandled promise rejection" warnings
promiseReject(TestException("Rejected"))
try {
deferred.await()
expectUnreached()
} catch (e: Throwable) {
assertTrue(e is TestException)
assertEquals("Rejected", e.message)
}
}
@Test
fun testCompletedDeferredAsPromise() = GlobalScope.promise {
val deferred = async(start = CoroutineStart.UNDISPATCHED) {
// completed right away
"OK"
}
val promise = deferred.asPromise()
assertEquals("OK", promise.await())
}
@Test
fun testWaitForDeferredAsPromise() = GlobalScope.promise {
val deferred = async {
// will complete later
"OK"
}
val promise = deferred.asPromise()
assertEquals("OK", promise.await()) // await yields main thread to deferred coroutine
}
@Test
fun testCancellableAwaitPromise() = GlobalScope.promise {
lateinit var r: (String) -> Unit
val toAwait = Promise { resolve, _ -> r = resolve }
val job = launch(start = CoroutineStart.UNDISPATCHED) {
toAwait.await() // suspends
}
job.cancel() // cancel the job
r("fail") // too late, the waiting job was already cancelled
}
@Test
fun testAsPromiseAsDeferred() = GlobalScope.promise {
val deferred = async { "OK" }
val promise = deferred.asPromise()
val d2 = promise.asDeferred()
assertSame(d2, deferred)
assertEquals("OK", d2.await())
}
@Test
fun testLeverageTestResult(): TestResult {
// Cannot use expect(..) here
var seq = 0
val result = runTest {
++seq
}
return result.then {
if (seq != 1) error("Unexpected result: $seq")
}
}
}