/
CoroutineSchedulerTest.kt
173 lines (149 loc) · 4.71 KB
/
CoroutineSchedulerTest.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
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.scheduling
import kotlinx.coroutines.*
import org.junit.Test
import java.lang.Runnable
import java.util.concurrent.*
import kotlin.coroutines.*
import kotlin.test.*
class CoroutineSchedulerTest : TestBase() {
@Test
fun testModesExternalSubmission() { // Smoke
CoroutineScheduler(1, 1).use {
for (mode in TaskMode.values()) {
val latch = CountDownLatch(1)
it.dispatch(Runnable {
latch.countDown()
}, TaskContextImpl(mode))
latch.await()
}
}
}
@Test
fun testModesInternalSubmission() { // Smoke
CoroutineScheduler(2, 2).use {
val latch = CountDownLatch(TaskMode.values().size)
it.dispatch(Runnable {
for (mode in TaskMode.values()) {
it.dispatch(Runnable {
latch.countDown()
}, TaskContextImpl(mode))
}
})
latch.await()
}
}
@Test
fun testNonFairSubmission() {
CoroutineScheduler(1, 1).use {
val startLatch = CountDownLatch(1)
val finishLatch = CountDownLatch(2)
it.dispatch(Runnable {
it.dispatch(Runnable {
expect(2)
finishLatch.countDown()
})
it.dispatch(Runnable {
expect(1)
finishLatch.countDown()
})
})
startLatch.countDown()
finishLatch.await()
finish(3)
}
}
@Test
fun testFairSubmission() {
CoroutineScheduler(1, 1).use {
val startLatch = CountDownLatch(1)
val finishLatch = CountDownLatch(2)
it.dispatch(Runnable {
it.dispatch(Runnable {
expect(1)
finishLatch.countDown()
})
it.dispatch(Runnable {
expect(2)
finishLatch.countDown()
}, tailDispatch = true)
})
startLatch.countDown()
finishLatch.await()
finish(3)
}
}
@Test
fun testRngUniformDistribution() {
CoroutineScheduler(1, 128).use { scheduler ->
val worker = scheduler.Worker(1)
testUniformDistribution(worker, 2)
testUniformDistribution(worker, 4)
testUniformDistribution(worker, 8)
testUniformDistribution(worker, 12)
testUniformDistribution(worker, 16)
}
}
@Test(expected = IllegalArgumentException::class)
fun testNegativeCorePoolSize() {
ExperimentalCoroutineDispatcher(-1, 4)
}
@Test(expected = IllegalArgumentException::class)
fun testNegativeMaxPoolSize() {
ExperimentalCoroutineDispatcher(1, -4)
}
@Test(expected = IllegalArgumentException::class)
fun testCorePoolSizeGreaterThanMaxPoolSize() {
ExperimentalCoroutineDispatcher(4, 1)
}
@Test
fun testSelfClose() {
val dispatcher = ExperimentalCoroutineDispatcher(1, 1)
val latch = CountDownLatch(1)
dispatcher.dispatch(EmptyCoroutineContext, Runnable {
dispatcher.close(); latch.countDown()
})
latch.await()
}
@Test
fun testInterruptionCleanup() {
ExperimentalCoroutineDispatcher(1, 1).use {
val executor = it.executor
var latch = CountDownLatch(1)
executor.execute {
Thread.currentThread().interrupt()
latch.countDown()
}
latch.await()
Thread.sleep(100) // I am really sorry
latch = CountDownLatch(1)
executor.execute {
try {
assertFalse(Thread.currentThread().isInterrupted)
} finally {
latch.countDown()
}
}
latch.await()
}
}
private fun testUniformDistribution(worker: CoroutineScheduler.Worker, bound: Int) {
val result = IntArray(bound)
val iterations = 10_000_000
repeat(iterations) {
++result[worker.nextInt(bound)]
}
val bucketSize = iterations / bound
for (i in result) {
val ratio = i.toDouble() / bucketSize
// 10% deviation
check(ratio <= 1.1)
check(ratio >= 0.9)
}
}
private class TaskContextImpl(override val taskMode: TaskMode) : TaskContext {
override fun afterTask() {}
}
}