Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +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 | |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
| 19 | import org.junit.Test |
| 20 | import java.io.IOException |
Roman Elizarov | 12f961d | 2017-02-06 15:44:03 +0300 | [diff] [blame] | 21 | import org.junit.Assert.* |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 22 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 23 | class AsyncTest : TestBase() { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 24 | @Test |
| 25 | fun testSimple(): Unit = runBlocking { |
| 26 | expect(1) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 27 | val d = async(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 28 | expect(3) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 29 | 42 |
| 30 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 31 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 32 | check(d.isActive) |
| 33 | check(d.await() == 42) |
| 34 | check(!d.isActive) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 35 | expect(4) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 36 | check(d.await() == 42) // second await -- same result |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 37 | finish(5) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 38 | } |
| 39 | |
Roman Elizarov | 4b0ef7b | 2017-04-17 12:39:29 +0300 | [diff] [blame] | 40 | @Test |
| 41 | fun testUndispatched(): Unit = runBlocking { |
| 42 | expect(1) |
| 43 | val d = async(context, start = CoroutineStart.UNDISPATCHED) { |
| 44 | expect(2) |
| 45 | 42 |
| 46 | } |
| 47 | expect(3) |
| 48 | check(!d.isActive) |
| 49 | check(d.await() == 42) |
| 50 | finish(4) |
| 51 | } |
| 52 | |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 53 | @Test(expected = IOException::class) |
| 54 | fun testSimpleException(): Unit = runBlocking { |
| 55 | expect(1) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 56 | val d = async(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 57 | finish(3) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 58 | throw IOException() |
| 59 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 60 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 61 | d.await() // will throw IOException |
| 62 | } |
| 63 | |
| 64 | @Test(expected = IOException::class) |
| 65 | fun testDeferAndYieldException(): Unit = runBlocking { |
| 66 | expect(1) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 67 | val d = async(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 68 | expect(3) |
| 69 | yield() // no effect, parent waiting |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 70 | finish(4) |
| 71 | throw IOException() |
| 72 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 73 | expect(2) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 74 | d.await() // will throw IOException |
| 75 | } |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 76 | |
| 77 | @Test |
| 78 | fun testDeferWithTwoWaiters() = runBlocking { |
| 79 | expect(1) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 80 | val d = async(context) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 81 | expect(5) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 82 | yield() |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 83 | expect(9) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 84 | 42 |
| 85 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 86 | expect(2) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 87 | launch(context) { |
| 88 | expect(6) |
| 89 | check(d.await() == 42) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 90 | expect(11) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 91 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 92 | expect(3) |
| 93 | launch(context) { |
| 94 | expect(7) |
| 95 | check(d.await() == 42) |
| 96 | expect(12) |
| 97 | } |
| 98 | expect(4) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 99 | yield() // this actually yields control to async, which produces results and resumes both waiters (in order) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 100 | expect(8) |
| 101 | yield() // yield again to "d", which completes |
| 102 | expect(10) |
| 103 | yield() // yield to both waiters |
| 104 | finish(13) |
Roman Elizarov | daa7922 | 2017-01-26 11:31:31 +0300 | [diff] [blame] | 105 | } |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 106 | |
| 107 | class BadClass { |
| 108 | override fun equals(other: Any?): Boolean = error("equals") |
| 109 | override fun hashCode(): Int = error("hashCode") |
| 110 | override fun toString(): String = error("toString") |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | fun testDeferBadClass() = runBlocking { |
| 115 | val bad = BadClass() |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 116 | val d = async(context) { |
Roman Elizarov | b6b0125 | 2017-02-06 13:17:40 +0300 | [diff] [blame] | 117 | expect(1) |
| 118 | bad |
| 119 | } |
| 120 | assertTrue(d.await() === bad) |
| 121 | finish(2) |
| 122 | } |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 123 | } |