-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCancellableContinuationHandlersTest.kt
188 lines (174 loc) · 6.12 KB
/
CancellableContinuationHandlersTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
package kotlinx.coroutines
import kotlinx.coroutines.testing.*
import kotlinx.coroutines.internal.*
import kotlin.coroutines.*
import kotlin.test.*
class CancellableContinuationHandlersTest : TestBase() {
@Test
fun testDoubleSubscription() = runTest({ it is IllegalStateException }) {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation { finish(1) }
c.invokeOnCancellation { expectUnreached() }
}
}
@Test
fun testDoubleSubscriptionAfterCompletion() = runTest {
suspendCancellableCoroutine<Unit> { c ->
c.resume(Unit)
// First invokeOnCancellation is Ok
c.invokeOnCancellation { expectUnreached() }
// Second invokeOnCancellation is not allowed
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
}
@Test
fun testDoubleSubscriptionAfterCompletionWithException() = runTest {
assertFailsWith<TestException> {
suspendCancellableCoroutine<Unit> { c ->
c.resumeWithException(TestException())
// First invokeOnCancellation is Ok
c.invokeOnCancellation { expectUnreached() }
// Second invokeOnCancellation is not allowed
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
}
}
@Test
fun testDoubleSubscriptionAfterCancellation() = runTest {
try {
suspendCancellableCoroutine<Unit> { c ->
c.cancel()
c.invokeOnCancellation {
assertIs<CancellationException>(it)
expect(1)
}
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
} catch (e: CancellationException) {
finish(2)
}
}
@Test
fun testSecondSubscriptionAfterCancellation() = runTest {
try {
suspendCancellableCoroutine<Unit> { c ->
// Set IOC first
c.invokeOnCancellation {
assertNull(it)
expect(2)
}
expect(1)
// then cancel (it gets called)
c.cancel()
// then try to install another one
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
} catch (e: CancellationException) {
finish(3)
}
}
@Test
fun testSecondSubscriptionAfterResumeCancelAndDispatch() = runTest {
var cont: CancellableContinuation<Unit>? = null
val job = launch(start = CoroutineStart.UNDISPATCHED) {
// will be cancelled during dispatch
assertFailsWith<CancellationException> {
suspendCancellableCoroutine<Unit> { c ->
cont = c
// Set IOC first -- not called (completed)
c.invokeOnCancellation {
assertIs<CancellationException>(it)
expect(4)
}
expect(1)
}
}
expect(5)
}
expect(2)
// then resume it
cont!!.resume(Unit) // schedule cancelled continuation for dispatch
// then cancel the job during dispatch
job.cancel()
expect(3)
yield() // finish dispatching (will call IOC handler here!)
expect(6)
// then try to install another one after we've done dispatching it
assertFailsWith<IllegalStateException> {
cont!!.invokeOnCancellation { expectUnreached() }
}
finish(7)
}
@Test
fun testDoubleSubscriptionAfterCancellationWithCause() = runTest {
try {
suspendCancellableCoroutine<Unit> { c ->
c.cancel(AssertionError())
c.invokeOnCancellation {
require(it is AssertionError)
expect(1)
}
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
} catch (e: AssertionError) {
finish(2)
}
}
@Test
fun testDoubleSubscriptionMixed() = runTest {
try {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation {
require(it is IndexOutOfBoundsException)
expect(1)
}
c.cancel(IndexOutOfBoundsException())
assertFailsWith<IllegalStateException> { c.invokeOnCancellation { expectUnreached() } }
}
} catch (e: IndexOutOfBoundsException) {
finish(2)
}
}
@Test
fun testExceptionInHandler() = runTest(
unhandled = listOf({ it -> it is CompletionHandlerException })
) {
expect(1)
try {
suspendCancellableCoroutine<Unit> { c ->
c.invokeOnCancellation { throw AssertionError() }
c.cancel()
}
} catch (e: CancellationException) {
expect(2)
}
finish(3)
}
@Test
fun testSegmentAsHandler() = runTest {
class MySegment : Segment<MySegment>(0, null) {
override val numberOfSlots: Int get() = 0
override val isLeftmostOrProcessed: Boolean get() = false
var invokeOnCancellationCalled = false
override fun onCancellation(index: Int, cause: Throwable?, context: CoroutineContext) {
invokeOnCancellationCalled = true
}
}
val s = MySegment()
expect(1)
try {
suspendCancellableCoroutine<Unit> { c ->
expect(2)
c as CancellableContinuationImpl<*>
c.invokeOnCancellation(s, 0)
c.cancel()
}
} catch (e: CancellationException) {
expect(3)
}
expect(4)
check(s.invokeOnCancellationCalled)
finish(5)
}
}