Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +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 | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.channels.* |
| 8 | import kotlinx.coroutines.experimental.selects.* |
| 9 | import kotlinx.coroutines.experimental.sync.* |
| 10 | import kotlin.coroutines.experimental.* |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 11 | import kotlin.test.* |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 12 | |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 13 | class AtomicCancellationTest : TestBase() { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 14 | @Test |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 15 | fun testSendAtomicCancel() = runBlocking { |
| 16 | expect(1) |
| 17 | val channel = Channel<Int>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 18 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 19 | expect(2) |
| 20 | channel.send(42) // suspends |
| 21 | expect(4) // should execute despite cancellation |
| 22 | } |
| 23 | expect(3) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 24 | assertEquals(42, channel.receive()) // will schedule sender for further execution |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 25 | job.cancel() // cancel the job next |
| 26 | yield() // now yield |
| 27 | finish(5) |
| 28 | } |
| 29 | |
| 30 | @Test |
| 31 | fun testSelectSendAtomicCancel() = runBlocking { |
| 32 | expect(1) |
| 33 | val channel = Channel<Int>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 34 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 35 | expect(2) |
| 36 | val result = select<String> { // suspends |
| 37 | channel.onSend(42) { |
| 38 | expect(4) |
| 39 | "OK" |
| 40 | } |
| 41 | } |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 42 | assertEquals("OK", result) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 43 | expect(5) // should execute despite cancellation |
| 44 | } |
| 45 | expect(3) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 46 | assertEquals(42, channel.receive()) // will schedule sender for further execution |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 47 | job.cancel() // cancel the job next |
| 48 | yield() // now yield |
| 49 | finish(6) |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun testReceiveAtomicCancel() = runBlocking { |
| 54 | expect(1) |
| 55 | val channel = Channel<Int>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 56 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 57 | expect(2) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 58 | assertEquals(42, channel.receive()) // suspends |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 59 | expect(4) // should execute despite cancellation |
| 60 | } |
| 61 | expect(3) |
| 62 | channel.send(42) // will schedule receiver for further execution |
| 63 | job.cancel() // cancel the job next |
| 64 | yield() // now yield |
| 65 | finish(5) |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | fun testSelectReceiveAtomicCancel() = runBlocking { |
| 70 | expect(1) |
| 71 | val channel = Channel<Int>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 72 | val job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 73 | expect(2) |
| 74 | val result = select<String> { // suspends |
| 75 | channel.onReceive { |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 76 | assertEquals(42, it) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 77 | expect(4) |
| 78 | "OK" |
| 79 | } |
| 80 | } |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 81 | assertEquals("OK", result) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 82 | expect(5) // should execute despite cancellation |
| 83 | } |
| 84 | expect(3) |
| 85 | channel.send(42) // will schedule receiver for further execution |
| 86 | job.cancel() // cancel the job next |
| 87 | yield() // now yield |
| 88 | finish(6) |
| 89 | } |
| 90 | |
| 91 | @Test |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 92 | fun testSelectDeferredAwaitCancellable() = runBlocking { |
| 93 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 94 | val deferred = async(coroutineContext) { // deferred, not yet complete |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 95 | expect(4) |
| 96 | "OK" |
| 97 | } |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 98 | assertEquals(false, deferred.isCompleted) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 99 | var job: Job? = null |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 100 | launch(coroutineContext) { // will cancel job as soon as deferred completes |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 101 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 102 | assertEquals(true, deferred.isCompleted) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 103 | job!!.cancel() |
| 104 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 105 | job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 106 | expect(2) |
| 107 | try { |
| 108 | select<Unit> { // suspends |
| 109 | deferred.onAwait { expectUnreached() } |
| 110 | } |
| 111 | expectUnreached() // will not execute -- cancelled while dispatched |
| 112 | } finally { |
| 113 | finish(7) // but will execute finally blocks |
| 114 | } |
| 115 | } |
| 116 | expect(3) // continues to execute when job suspends |
| 117 | yield() // to deferred & canceller |
| 118 | expect(6) |
| 119 | } |
| 120 | |
| 121 | @Test |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 122 | fun testSelectJobJoinCancellable() = runBlocking { |
| 123 | expect(1) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 124 | val jobToJoin = launch(coroutineContext) { // not yet complete |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 125 | expect(4) |
| 126 | } |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 127 | assertEquals(false, jobToJoin.isCompleted) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 128 | var job: Job? = null |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 129 | launch(coroutineContext) { // will cancel job as soon as jobToJoin completes |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 130 | expect(5) |
Roman Elizarov | c0d71dc | 2017-12-21 22:12:43 +0300 | [diff] [blame] | 131 | assertEquals(true, jobToJoin.isCompleted) |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 132 | job!!.cancel() |
| 133 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 134 | job = launch(coroutineContext, start = CoroutineStart.UNDISPATCHED) { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 135 | expect(2) |
| 136 | try { |
| 137 | select<Unit> { // suspends |
| 138 | jobToJoin.onJoin { expectUnreached() } |
| 139 | } |
| 140 | expectUnreached() // will not execute -- cancelled while dispatched |
| 141 | } finally { |
| 142 | finish(7) // but will execute finally blocks |
| 143 | } |
| 144 | } |
| 145 | expect(3) // continues to execute when job suspends |
| 146 | yield() // to jobToJoin & canceller |
| 147 | expect(6) |
| 148 | } |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 149 | } |