-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRunBlockingJvmTest.kt
184 lines (172 loc) · 5.68 KB
/
RunBlockingJvmTest.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
package kotlinx.coroutines
import kotlinx.coroutines.testing.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.thread
import kotlin.test.*
import kotlin.time.Duration
class RunBlockingJvmTest : TestBase() {
/** Tests that the [runBlocking] coroutine runs to completion even it was interrupted. */
@Test
fun testFinishingWhenInterrupted() {
startInSeparateThreadAndInterrupt { mayInterrupt ->
expect(1)
try {
runBlocking {
try {
mayInterrupt()
expect(2)
delay(Duration.INFINITE)
} finally {
withContext(NonCancellable) {
expect(3)
repeat(10) { yield() }
expect(4)
}
}
}
} catch (_: InterruptedException) {
expect(5)
}
}
finish(6)
}
/** Tests that [runBlocking] will exit if it gets interrupted. */
@Test
fun testCancellingWhenInterrupted() {
startInSeparateThreadAndInterrupt { mayInterrupt ->
expect(1)
try {
runBlocking {
try {
mayInterrupt()
expect(2)
delay(Duration.INFINITE)
} catch (_: CancellationException) {
expect(3)
}
}
} catch (_: InterruptedException) {
expect(4)
}
}
finish(5)
}
/** Tests that [runBlocking] does not check for interruptions before the first attempt to suspend,
* as no blocking actually happens. */
@Test
fun testInitialPortionRunningDespiteInterruptions() {
Thread.currentThread().interrupt()
runBlocking {
expect(1)
try {
Thread.sleep(Long.MAX_VALUE)
} catch (_: InterruptedException) {
expect(2)
}
}
assertFalse(Thread.interrupted())
finish(3)
}
/**
* Tests that [runBlockingNonInterruptible] is going to run its job to completion even if it gets interrupted
* or if thread switches occur.
*/
@Test
fun testNonInterruptibleRunBlocking() {
startInSeparateThreadAndInterrupt { mayInterrupt ->
val v = runBlockingNonInterruptible {
mayInterrupt()
repeat(10) {
expect(it + 1)
delay(1)
}
42
}
assertTrue(Thread.interrupted())
assertEquals(42, v)
expect(11)
}
finish(12)
}
/**
* Tests that [runBlockingNonInterruptible] is going to run its job to completion even if it gets interrupted
* or if thread switches occur, and then will rethrow the exception thrown by the job.
*/
@Test
fun testNonInterruptibleRunBlockingFailure() {
val exception = AssertionError()
startInSeparateThreadAndInterrupt { mayInterrupt ->
val exception2 = assertFailsWith<AssertionError> {
runBlockingNonInterruptible {
mayInterrupt()
repeat(10) {
expect(it + 1)
// even thread switches should not be a problem
withContext(Dispatchers.IO) {
delay(1)
}
}
throw exception
}
}
assertTrue(Thread.interrupted())
assertSame(exception, exception2)
expect(11)
}
finish(12)
}
/**
* Tests that [runBlockingNonInterruptible] is going to run its job to completion even if it gets interrupted
* or if thread switches occur.
*/
@Test
fun testNonInterruptibleRunBlockingPropagatingInterruptions() {
val exception = AssertionError()
startInSeparateThreadAndInterrupt { mayInterrupt ->
runBlockingNonInterruptible {
mayInterrupt()
try {
Thread.sleep(Long.MAX_VALUE)
} catch (_: InterruptedException) {
expect(1)
}
}
expect(2)
assertFalse(Thread.interrupted())
}
finish(3)
}
/**
* Tests that starting [runBlockingNonInterruptible] in an interrupted thread does not affect the result.
*/
@Test
fun testNonInterruptibleRunBlockingStartingInterrupted() {
Thread.currentThread().interrupt()
val v = runBlockingNonInterruptible { 42 }
assertEquals(42, v)
assertTrue(Thread.interrupted())
}
private fun startInSeparateThreadAndInterrupt(action: (mayInterrupt: () -> Unit) -> Unit) {
val latch = CountDownLatch(1)
val thread = thread {
action { latch.countDown() }
}
latch.await()
thread.interrupt()
thread.join()
}
private fun <T> runBlockingNonInterruptible(action: suspend () -> T): T {
val result = AtomicReference<Result<T>>()
try {
runBlocking {
withContext(NonCancellable) {
result.set(runCatching { action() })
}
}
} catch (_: InterruptedException) {
Thread.currentThread().interrupt() // restore the interrupted flag
}
return result.get().getOrThrow()
}
}