forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterruptibleTest.kt
163 lines (144 loc) · 5.13 KB
/
InterruptibleTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import java.io.IOException
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import kotlin.test.*
class InterruptibleTest: TestBase() {
@Test
fun testNormalRun() = runBlocking {
var result = runInterruptible {
var x = doSomethingUsefulBlocking(1, 1)
var y = doSomethingUsefulBlocking(1, 2)
x + y
}
assertEquals(3, result)
}
@Test
fun testExceptionThrow() = runBlocking {
try {
runInterruptible {
throw TestException()
}
} catch (e: Throwable) {
assertTrue(e is TestException)
return@runBlocking
}
fail()
}
@Test
fun testRunWithContext() = runBlocking {
var runThread =
runInterruptible (Dispatchers.IO) {
Thread.currentThread()
}
assertNotEquals(runThread, Thread.currentThread())
}
@Test
fun testInterrupt() {
val count = AtomicInteger(0)
try {
expect(1)
runBlocking {
launch(Dispatchers.IO) {
async {
try {
// `runInterruptible` makes a blocking block cancelable (become a cancellation point)
// by interrupting it on cancellation and throws CancellationException
runInterruptible {
try {
doSomethingUsefulBlocking(100, 1)
doSomethingUsefulBlocking(Long.MAX_VALUE, 0)
} catch (e: InterruptedException) {
expect(3)
throw e
}
}
} catch (e: CancellationException) {
expect(4)
}
}
async {
delay(500L)
expect(2)
throw IOException()
}
}
}
} catch (e: IOException) {
expect(5)
}
finish(6)
}
@Test
fun testNoInterruptLeak() = runBlocking {
var interrupted = true
var task = launch(Dispatchers.IO) {
try {
runInterruptible {
doSomethingUsefulBlocking(Long.MAX_VALUE, 0)
}
} finally {
interrupted = Thread.currentThread().isInterrupted
}
}
delay(500)
task.cancel()
task.join()
assertFalse(interrupted)
}
@Test
fun testStress() {
val REPEAT_TIMES = 2_000
Executors.newCachedThreadPool().asCoroutineDispatcher().use { dispatcher ->
val interruptLeak = AtomicBoolean(false)
val enterCount = AtomicInteger(0)
val interruptedCount = AtomicInteger(0)
val otherExceptionCount = AtomicInteger(0)
runBlocking {
repeat(REPEAT_TIMES) { repeat ->
var job = launch(start = CoroutineStart.LAZY, context = dispatcher) {
try {
runInterruptible {
enterCount.incrementAndGet()
try {
doSomethingUsefulBlocking(Long.MAX_VALUE, 0)
} catch (e: InterruptedException) {
interruptedCount.incrementAndGet()
throw e
}
}
} catch (e: CancellationException) {
} catch (e: Throwable) {
otherExceptionCount.incrementAndGet()
} finally {
interruptLeak.set(interruptLeak.get() || Thread.currentThread().isInterrupted)
}
}
var cancelJob = launch(start = CoroutineStart.LAZY, context = dispatcher) {
job.cancel()
}
launch (dispatcher) {
delay((REPEAT_TIMES - repeat).toLong())
job.start()
}
launch (dispatcher) {
delay(repeat.toLong())
cancelJob.start()
}
}
}
assertFalse(interruptLeak.get())
assertEquals(enterCount.get(), interruptedCount.get())
assertEquals(0, otherExceptionCount.get())
}
}
private fun doSomethingUsefulBlocking(timeUseMillis: Long, result: Int): Int {
Thread.sleep(timeUseMillis)
return result
}
private class TestException : Exception()
}