blob: fe69a8a3a97db664575376857d4f1f763c205385 [file] [log] [blame]
Roman Elizarova7db8ec2017-12-21 22:45:12 +03001/*
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 Elizarovc0d71dc2017-12-21 22:12:43 +030017package kotlinx.coroutines.experimental
18
Roman Elizarovaa461cf2018-04-11 13:20:29 +030019import kotlinx.coroutines.experimental.selects.*
20import kotlinx.coroutines.experimental.sync.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030021import kotlin.coroutines.experimental.*
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030022import kotlin.test.*
23
Roman Elizarovaa461cf2018-04-11 13:20:29 +030024class AtomicCancellationCommonTest : TestBase() {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030025 @Test
Roman Elizarov1097bc82017-12-26 14:11:42 +030026 fun testCancellableLaunch() = runTest {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030027 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 Elizarov1097bc82017-12-26 14:11:42 +030037 fun testAtomicLaunch() = runTest {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030038 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 Elizarov1097bc82017-12-26 14:11:42 +030048 fun testDeferredAwaitCancellable() = runTest {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030049 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 Elizarov1097bc82017-12-26 14:11:42 +030076 fun testJobJoinCancellable() = runTest {
Roman Elizarovc0d71dc2017-12-21 22:12:43 +030077 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 Elizarovaa461cf2018-04-11 13:20:29 +0300101
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 Elizarovc0d71dc2017-12-21 22:12:43 +0300139}