Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +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 | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.selects.* |
| 20 | import kotlinx.coroutines.experimental.sync.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 21 | import kotlin.coroutines.experimental.* |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 22 | import kotlin.test.* |
| 23 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 24 | class AtomicCancellationCommonTest : TestBase() { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 25 | @Test |
Roman Elizarov | 1097bc8 | 2017-12-26 14:11:42 +0300 | [diff] [blame] | 26 | fun testCancellableLaunch() = runTest { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 27 | expect(1) |
| 28 | val job = launch(coroutineContext) { |
| 29 | expectUnreached() // will get cancelled before start |
| 30 | } |
| 31 | expect(2) |
| 32 | job.cancel() |
| 33 | finish(3) |
| 34 | } |
| 35 | |
| 36 | @Test |
Roman Elizarov | 1097bc8 | 2017-12-26 14:11:42 +0300 | [diff] [blame] | 37 | fun testAtomicLaunch() = runTest { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 38 | expect(1) |
| 39 | val job = launch(coroutineContext, start = CoroutineStart.ATOMIC) { |
| 40 | finish(4) // will execute even after it was cancelled |
| 41 | } |
| 42 | expect(2) |
| 43 | job.cancel() |
| 44 | expect(3) |
| 45 | } |
| 46 | |
| 47 | @Test |
Roman Elizarov | 1097bc8 | 2017-12-26 14:11:42 +0300 | [diff] [blame] | 48 | fun testDeferredAwaitCancellable() = runTest { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 49 | expect(1) |
| 50 | val deferred = async(coroutineContext) { // deferred, not yet complete |
| 51 | expect(4) |
| 52 | "OK" |
| 53 | } |
| 54 | assertEquals(false, deferred.isCompleted) |
| 55 | var job: Job? = null |
| 56 | launch(coroutineContext) { // will cancel job as soon as deferred completes |
| 57 | expect(5) |
| 58 | assertEquals(true, deferred.isCompleted) |
| 59 | job!!.cancel() |
| 60 | } |
| 61 | job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
| 62 | expect(2) |
| 63 | try { |
| 64 | deferred.await() // suspends |
| 65 | expectUnreached() // will not execute -- cancelled while dispatched |
| 66 | } finally { |
| 67 | finish(7) // but will execute finally blocks |
| 68 | } |
| 69 | } |
| 70 | expect(3) // continues to execute when job suspends |
| 71 | yield() // to deferred & canceller |
| 72 | expect(6) |
| 73 | } |
| 74 | |
| 75 | @Test |
Roman Elizarov | 1097bc8 | 2017-12-26 14:11:42 +0300 | [diff] [blame] | 76 | fun testJobJoinCancellable() = runTest { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 77 | expect(1) |
| 78 | val jobToJoin = launch(coroutineContext) { // not yet complete |
| 79 | expect(4) |
| 80 | } |
| 81 | assertEquals(false, jobToJoin.isCompleted) |
| 82 | var job: Job? = null |
| 83 | launch(coroutineContext) { // will cancel job as soon as jobToJoin completes |
| 84 | expect(5) |
| 85 | assertEquals(true, jobToJoin.isCompleted) |
| 86 | job!!.cancel() |
| 87 | } |
| 88 | job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
| 89 | expect(2) |
| 90 | try { |
| 91 | jobToJoin.join() // suspends |
| 92 | expectUnreached() // will not execute -- cancelled while dispatched |
| 93 | } finally { |
| 94 | finish(7) // but will execute finally blocks |
| 95 | } |
| 96 | } |
| 97 | expect(3) // continues to execute when job suspends |
| 98 | yield() // to jobToJoin & canceller |
| 99 | expect(6) |
| 100 | } |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 101 | |
| 102 | @Test |
| 103 | fun testLockAtomicCancel() = runTest { |
| 104 | expect(1) |
| 105 | val mutex = Mutex(true) // locked mutex |
| 106 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
| 107 | expect(2) |
| 108 | mutex.lock() // suspends |
| 109 | expect(4) // should execute despite cancellation |
| 110 | } |
| 111 | expect(3) |
| 112 | mutex.unlock() // unlock mutex first |
| 113 | job.cancel() // cancel the job next |
| 114 | yield() // now yield |
| 115 | finish(5) |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | fun testSelectLockAtomicCancel() = runTest { |
| 120 | expect(1) |
| 121 | val mutex = Mutex(true) // locked mutex |
| 122 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
| 123 | expect(2) |
| 124 | val result = select<String> { // suspends |
| 125 | mutex.onLock { |
| 126 | expect(4) |
| 127 | "OK" |
| 128 | } |
| 129 | } |
| 130 | assertEquals("OK", result) |
| 131 | expect(5) // should execute despite cancellation |
| 132 | } |
| 133 | expect(3) |
| 134 | mutex.unlock() // unlock mutex first |
| 135 | job.cancel() // cancel the job next |
| 136 | yield() // now yield |
| 137 | finish(6) |
| 138 | } |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 139 | } |