Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 5 | @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913 |
| 6 | |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 7 | package kotlinx.coroutines.experimental |
| 8 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 9 | import kotlin.coroutines.experimental.* |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 10 | import kotlin.test.* |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 11 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 12 | class AsyncLazyTest : TestBase() { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 13 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 14 | fun testSimple() = runTest { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 15 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 16 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 17 | expect(3) |
| 18 | 42 |
| 19 | } |
| 20 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 21 | assertTrue(!d.isActive && !d.isCompleted) |
| 22 | assertTrue(d.await() == 42) |
| 23 | assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 24 | expect(4) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 25 | assertTrue(d.await() == 42) // second await -- same result |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 26 | finish(5) |
| 27 | } |
| 28 | |
| 29 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 30 | fun testLazyDeferAndYield() = runTest { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 31 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 32 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 33 | expect(3) |
| 34 | yield() // this has not effect, because parent coroutine is waiting |
| 35 | expect(4) |
| 36 | 42 |
| 37 | } |
| 38 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 39 | assertTrue(!d.isActive && !d.isCompleted) |
| 40 | assertTrue(d.await() == 42) |
| 41 | assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 42 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 43 | assertTrue(d.await() == 42) // second await -- same result |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 44 | finish(6) |
| 45 | } |
| 46 | |
| 47 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 48 | fun testLazyDeferAndYield2() = runTest { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 49 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 50 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 51 | expect(7) |
| 52 | 42 |
| 53 | } |
| 54 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 55 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 56 | launch(coroutineContext) { // see how it looks from another coroutine |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 57 | expect(4) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 58 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 59 | yield() // yield back to main |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 60 | expect(6) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 61 | assertTrue(d.isActive && !d.isCompleted) // implicitly started by main's await |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 62 | yield() // yield to d |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 63 | } |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 64 | expect(3) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 65 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 66 | yield() // yield to second child (lazy async is not computing yet) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 67 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 68 | assertTrue(!d.isActive && !d.isCompleted) |
| 69 | assertTrue(d.await() == 42) // starts computing |
| 70 | assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 71 | finish(8) |
| 72 | } |
| 73 | |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 74 | @Test |
| 75 | fun testSimpleException() = runTest( |
| 76 | expected = { it is TestException } |
| 77 | ) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 78 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 79 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 80 | finish(3) |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 81 | throw TestException() |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 82 | } |
| 83 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 84 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 85 | d.await() // will throw IOException |
| 86 | } |
| 87 | |
| 88 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 89 | fun testLazyDeferAndYieldException() = runTest( |
| 90 | expected = { it is TestException } |
| 91 | ) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 92 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 93 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 94 | expect(3) |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 95 | yield() // this has not effect, because parent coroutine is waiting |
| 96 | finish(4) |
| 97 | throw TestException() |
| 98 | } |
| 99 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 100 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 101 | d.await() // will throw IOException |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | fun testCatchException() = runTest { |
| 106 | expect(1) |
| 107 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
| 108 | expect(3) |
| 109 | throw TestException() |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 110 | } |
| 111 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 112 | assertTrue(!d.isActive && !d.isCompleted) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 113 | try { |
| 114 | d.await() // will throw IOException |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 115 | } catch (e: TestException) { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 116 | assertTrue(!d.isActive && d.isCompleted && d.isCompletedExceptionally && !d.isCancelled) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 117 | expect(4) |
| 118 | } |
| 119 | finish(5) |
| 120 | } |
| 121 | |
| 122 | @Test |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 123 | fun testStart() = runTest { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 124 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 125 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 126 | expect(4) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 127 | 42 |
| 128 | } |
| 129 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 130 | assertTrue(!d.isActive && !d.isCompleted) |
| 131 | assertTrue(d.start()) |
| 132 | assertTrue(d.isActive && !d.isCompleted) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 133 | expect(3) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 134 | assertTrue(!d.start()) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 135 | yield() // yield to started coroutine |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 136 | assertTrue(!d.isActive && d.isCompleted && !d.isCompletedExceptionally) // and it finishes |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 137 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 138 | assertTrue(d.await() == 42) // await sees result |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 139 | finish(6) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 140 | } |
| 141 | |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 142 | @Test |
| 143 | fun testCancelBeforeStart() = runTest( |
| 144 | expected = { it is JobCancellationException } |
| 145 | ) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 146 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 147 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 148 | expectUnreached() |
| 149 | 42 |
| 150 | } |
| 151 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 152 | assertTrue(!d.isActive && !d.isCompleted) |
| 153 | assertTrue(d.cancel()) |
| 154 | assertTrue(!d.isActive && d.isCompleted && d.isCompletedExceptionally && d.isCancelled) |
| 155 | assertTrue(!d.cancel()) |
| 156 | assertTrue(!d.start()) |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 157 | finish(3) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 158 | assertTrue(d.await() == 42) // await shall throw CancellationException |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 159 | expectUnreached() |
| 160 | } |
| 161 | |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 162 | @Test |
| 163 | fun testCancelWhileComputing() = runTest( |
| 164 | expected = { it is CancellationException } |
| 165 | ) { |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 166 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 167 | val d = async(coroutineContext, CoroutineStart.LAZY) { |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 168 | expect(4) |
| 169 | yield() // yield to main, that is going to cancel us |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 170 | expectUnreached() |
| 171 | 42 |
| 172 | } |
| 173 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 174 | assertTrue(!d.isActive && !d.isCompleted && !d.isCancelled) |
| 175 | assertTrue(d.start()) |
| 176 | assertTrue(d.isActive && !d.isCompleted && !d.isCancelled) |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 177 | expect(3) |
| 178 | yield() // yield to d |
| 179 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 180 | assertTrue(d.isActive && !d.isCompleted && !d.isCancelled) |
| 181 | assertTrue(d.cancel()) |
| 182 | assertTrue(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // cancelling ! |
| 183 | assertTrue(!d.cancel()) |
| 184 | assertTrue(!d.isActive && !d.isCompletedExceptionally && d.isCancelled) // still cancelling |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 185 | finish(6) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 186 | assertTrue(d.await() == 42) // await shall throw CancellationException |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 187 | expectUnreached() |
| 188 | } |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 189 | |
| 190 | private class TestException : Exception() |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 191 | } |