/
LimitedParallelismStressTest.kt
77 lines (66 loc) · 2 KB
/
LimitedParallelismStressTest.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
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.*
import org.junit.Test
import org.junit.runner.*
import org.junit.runners.*
import java.util.concurrent.atomic.*
import kotlin.test.*
@RunWith(Parameterized::class)
class LimitedParallelismStressTest(private val targetParallelism: Int) : TestBase() {
companion object {
@Parameterized.Parameters(name = "{0}")
@JvmStatic
fun params(): Collection<Array<Any>> = listOf(1, 2, 3, 4).map { arrayOf(it) }
}
@get:Rule
val executor = ExecutorRule(targetParallelism * 2)
private val iterations = 100_000 * stressTestMultiplier
private val parallelism = AtomicInteger(0)
private fun checkParallelism() {
val value = parallelism.incrementAndGet()
assertTrue { value <= targetParallelism }
parallelism.decrementAndGet()
}
@Test
fun testLimitedExecutor() = runTest {
val view = executor.limitedParallelism(targetParallelism)
repeat(iterations) {
launch(view) {
checkParallelism()
}
}
}
@Test
fun testLimitedDispatchersIo() = runTest {
val view = Dispatchers.IO.limitedParallelism(targetParallelism)
repeat(iterations) {
launch(view) {
checkParallelism()
}
}
}
@Test
fun testLimitedDispatchersIoDispatchYield() = runTest {
val view = Dispatchers.IO.limitedParallelism(targetParallelism)
repeat(iterations) {
launch(view) {
yield()
checkParallelism()
}
}
}
@Test
fun testUnconfined() = runTest {
val view = Dispatchers.Unconfined.limitedParallelism(targetParallelism)
repeat(iterations) {
launch(executor) {
withContext(view) {
checkParallelism()
}
}
}
}
}