Roman Elizarov | b170819 | 2017-12-22 12:14:05 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental |
| 18 | |
| 19 | import kotlin.js.Promise |
| 20 | import kotlin.test.* |
| 21 | |
| 22 | // :todo: This test does not actually test anything because of KT-21970 JS: Support async tests in Mocha and others |
| 23 | // One should watch for errors in the console to see if there were any failures (the test would still pass) |
| 24 | class PromiseTest : TestBase() { |
| 25 | @Test |
| 26 | fun testPromiseResolvedAsDeferred() = promise { |
| 27 | val promise = Promise<String> { resolve, _ -> |
| 28 | resolve("OK") |
| 29 | } |
| 30 | val deferred = promise.asDeferred() |
| 31 | assertEquals("OK", deferred.await()) |
| 32 | } |
| 33 | |
| 34 | @Test |
| 35 | fun testPromiseRejectedAsDeferred() = promise { |
| 36 | val promise = Promise<String> { _, reject -> |
| 37 | reject(TestException("Rejected")) |
| 38 | } |
| 39 | val deferred = promise.asDeferred() |
| 40 | try { |
| 41 | deferred.await() |
| 42 | expectUnreached() |
| 43 | } catch (e: Throwable) { |
| 44 | assertTrue(e is TestException) |
| 45 | assertEquals("Rejected", e.message) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | fun testCompletedDeferredAsPromise() = promise { |
| 51 | val deferred = async(coroutineContext, CoroutineStart.UNDISPATCHED) { |
| 52 | // completed right away |
| 53 | "OK" |
| 54 | } |
| 55 | val promise = deferred.asPromise() |
| 56 | assertEquals("OK", promise.await()) |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | fun testWaitForDeferredAsPromise() = promise { |
| 61 | val deferred = async(coroutineContext) { |
| 62 | // will complete later |
| 63 | "OK" |
| 64 | } |
| 65 | val promise = deferred.asPromise() |
| 66 | assertEquals("OK", promise.await()) // await yields main thread to deferred coroutine |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | fun testCancellableAwaitPromise() = promise { |
| 71 | lateinit var r: (String) -> Unit |
| 72 | val toAwait = Promise<String> { resolve, _ -> r = resolve } |
| 73 | val job = launch(coroutineContext, CoroutineStart.UNDISPATCHED) { |
| 74 | toAwait.await() // suspends |
| 75 | } |
| 76 | job.cancel() // cancel the job |
| 77 | r("fail") // too late, the waiting job was already cancelled |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | fun testAsPromiseAsDeferred() = promise { |
| 82 | val deferred = async { "OK" } |
| 83 | val promise = deferred.asPromise() |
| 84 | val d2 = promise.asDeferred() |
| 85 | assertTrue(d2 === deferred) |
| 86 | assertEquals("OK", d2.await()) |
| 87 | } |
| 88 | |
| 89 | private class TestException(message: String) : Exception(message) |
| 90 | } |