Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 1 | |
Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016-2017 JetBrains s.r.o. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 18 | @file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913 |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 19 | |
| 20 | package kotlinx.coroutines.experimental |
| 21 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 22 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 23 | import kotlin.test.* |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 24 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 25 | class WithContextTest : TestBase() { |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 26 | |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 27 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 28 | fun testSameContextNoSuspend() = runTest { |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 29 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 30 | launch(coroutineContext) { // make sure there is not early dispatch here |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 31 | finish(5) // after main exits |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 32 | } |
| 33 | expect(2) |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 34 | val result = withContext(coroutineContext) { // same context! |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 35 | expect(3) // still here |
| 36 | "OK" |
| 37 | } |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 38 | assertEquals("OK", result) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 39 | expect(4) |
| 40 | // will wait for the first coroutine |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 44 | fun testSameContextWithSuspend() = runTest { |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 45 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 46 | launch(coroutineContext) { // make sure there is not early dispatch here |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 47 | expect(4) |
| 48 | } |
| 49 | expect(2) |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 50 | val result = withContext(coroutineContext) { // same context! |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 51 | expect(3) // still here |
| 52 | yield() // now yields to launch! |
| 53 | expect(5) |
| 54 | "OK" |
| 55 | } |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 56 | assertEquals("OK", result) |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 57 | finish(6) |
| 58 | } |
| 59 | |
| 60 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 61 | fun testCancelWithJobNoSuspend() = runTest { |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 62 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 63 | launch(coroutineContext) { // make sure there is not early dispatch to here |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 64 | finish(6) // after main exits |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 65 | } |
| 66 | expect(2) |
| 67 | val job = Job() |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 68 | val result = withContext(coroutineContext + job) { // same context + new job |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 69 | expect(3) // still here |
| 70 | job.cancel() // cancel out job! |
| 71 | try { |
| 72 | yield() // shall throw CancellationException |
| 73 | expectUnreached() |
| 74 | } catch (e: CancellationException) { |
| 75 | expect(4) |
| 76 | } |
| 77 | "OK" |
| 78 | } |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 79 | assertEquals("OK", result) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 80 | expect(5) |
| 81 | // will wait for the first coroutine |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 85 | fun testCancelWithJobWithSuspend() = runTest { |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 86 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 87 | launch(coroutineContext) { // make sure there is not early dispatch to here |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 88 | expect(4) |
| 89 | } |
| 90 | expect(2) |
| 91 | val job = Job() |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 92 | val result = withContext(coroutineContext + job) { // same context + new job |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 93 | expect(3) // still here |
| 94 | yield() // now yields to launch! |
| 95 | expect(5) |
| 96 | job.cancel() // cancel out job! |
| 97 | try { |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 98 | yield() // shall throw CancellationException |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 99 | expectUnreached() |
| 100 | } catch (e: CancellationException) { |
| 101 | expect(6) |
| 102 | } |
| 103 | "OK" |
| 104 | } |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 105 | assertEquals("OK", result) |
Roman Elizarov | 4fe1801 | 2017-03-07 17:32:48 +0300 | [diff] [blame] | 106 | finish(7) |
| 107 | } |
| 108 | |
| 109 | @Test |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 110 | fun testRunCancellableDefault() = runTest( |
| 111 | expected = { it is JobCancellationException } |
| 112 | ) { |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 113 | val job = Job() |
| 114 | job.cancel() // cancel before it has a chance to run |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 115 | withContext(job + wrapperDispatcher(coroutineContext)) { |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 116 | expectUnreached() // will get cancelled |
| 117 | } |
| 118 | } |
| 119 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 120 | @Test |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 121 | fun testRunAtomicTryCancel() = runTest { |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 122 | expect(1) |
| 123 | val job = Job() |
| 124 | job.cancel() // try to cancel before it has a chance to run |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 125 | |
| 126 | try { |
| 127 | withContext(job + wrapperDispatcher(coroutineContext), CoroutineStart.ATOMIC) { |
| 128 | require(isActive) |
| 129 | // but start atomically |
| 130 | expect(2) |
| 131 | yield() // but will cancel here |
| 132 | expectUnreached() |
| 133 | } |
| 134 | } catch (e: JobCancellationException) { |
| 135 | // This block should be invoked *after* context body |
| 136 | finish(3) |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 140 | @Test |
| 141 | fun testRunUndispatchedTryCancel() = runTest( |
| 142 | expected = { it is JobCancellationException } |
| 143 | ) { |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 144 | expect(1) |
| 145 | val job = Job() |
| 146 | job.cancel() // try to cancel before it has a chance to run |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 147 | withContext(job + wrapperDispatcher(coroutineContext), CoroutineStart.UNDISPATCHED) { // but start atomically |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 148 | require(isActive) |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 149 | finish(2) |
| 150 | yield() // but will cancel here |
| 151 | expectUnreached() |
| 152 | } |
| 153 | } |
| 154 | |
Roman Elizarov | bcdd8e1 | 2017-10-20 16:42:06 +0800 | [diff] [blame] | 155 | @Test |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 156 | fun testRunSelfCancellationWithException() = runTest { |
Roman Elizarov | bcdd8e1 | 2017-10-20 16:42:06 +0800 | [diff] [blame] | 157 | expect(1) |
| 158 | var job: Job? = null |
| 159 | job = launch(coroutineContext) { |
| 160 | try { |
| 161 | expect(3) |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 162 | withContext(wrapperDispatcher(coroutineContext)) { |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 163 | require(isActive) |
Roman Elizarov | bcdd8e1 | 2017-10-20 16:42:06 +0800 | [diff] [blame] | 164 | expect(5) |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 165 | require(job!!.cancel()) // cancel itself |
| 166 | require(!job!!.cancel(AssertionError())) // cancel again, no success here |
| 167 | require(!isActive) |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 168 | throw TestException() // but throw a different exception |
Roman Elizarov | bcdd8e1 | 2017-10-20 16:42:06 +0800 | [diff] [blame] | 169 | } |
| 170 | } catch (e: Throwable) { |
| 171 | expect(7) |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 172 | // make sure TestException, not CancellationException or AssertionError is thrown |
Roman Elizarov | 0d7323a | 2018-01-29 20:59:15 +0300 | [diff] [blame] | 173 | assertTrue(e is TestException, "Caught $e") |
Roman Elizarov | bcdd8e1 | 2017-10-20 16:42:06 +0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | expect(2) |
| 177 | yield() // to the launched job |
| 178 | expect(4) |
| 179 | yield() // again to the job |
| 180 | expect(6) |
| 181 | yield() // again to exception handler |
| 182 | finish(8) |
| 183 | } |
| 184 | |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 185 | @Test |
| 186 | fun testRunSelfCancellation() = runTest { |
| 187 | expect(1) |
| 188 | var job: Job? = null |
| 189 | job = launch(coroutineContext) { |
| 190 | try { |
| 191 | expect(3) |
| 192 | withContext(wrapperDispatcher(coroutineContext)) { |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 193 | require(isActive) |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 194 | expect(5) |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 195 | require(job!!.cancel()) // cancel itself |
| 196 | require(!job!!.cancel(AssertionError())) // cancel again, no success here |
| 197 | require(!isActive) |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 198 | } |
| 199 | } catch (e: Throwable) { |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 200 | expect(7) |
Vsevolod Tolstopyatov | 4aa18aa | 2018-04-17 15:43:12 +0300 | [diff] [blame^] | 201 | // make sure TestException, not CancellationException or AssertionError is thrown! |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 202 | assertTrue(e is JobCancellationException, "Caught $e") |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | expect(2) |
| 207 | yield() // to the launched job |
| 208 | expect(4) |
| 209 | yield() // again to the job |
| 210 | expect(6) |
| 211 | yield() // again to exception handler |
| 212 | finish(8) |
| 213 | } |
| 214 | |
Roman Elizarov | 4eae2a8 | 2017-05-17 20:55:27 +0300 | [diff] [blame] | 215 | private fun wrapperDispatcher(context: CoroutineContext): CoroutineContext { |
| 216 | val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher |
| 217 | return object : CoroutineDispatcher() { |
| 218 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
| 219 | dispatcher.dispatch(context, block) |
| 220 | } |
| 221 | } |
| 222 | } |
Roman Elizarov | 9d5abcd | 2017-12-21 16:54:30 +0300 | [diff] [blame] | 223 | |
| 224 | private class TestException : Exception() |
Vsevolod Tolstopyatov | 931587a | 2018-04-16 17:51:12 +0300 | [diff] [blame] | 225 | } |