Skip to content

Commit c125c08

Browse files
qwwdfsadyorickhenning
authored andcommitted
Prevent LimitedParallelismStressTest from allocating too much memory, but still keep the pattern of the load roughly the same (Kotlin#3147)
1 parent 1d8cf6c commit c125c08

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

kotlinx-coroutines-core/jvm/test/LimitedParallelismStressTest.kt

+30-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LimitedParallelismStressTest(private val targetParallelism: Int) : TestBas
2323

2424
@get:Rule
2525
val executor = ExecutorRule(targetParallelism * 2)
26-
private val iterations = 100_000 * stressTestMultiplier
26+
private val iterations = 100_000
2727

2828
private val parallelism = AtomicInteger(0)
2929

@@ -37,27 +37,31 @@ class LimitedParallelismStressTest(private val targetParallelism: Int) : TestBas
3737
@Test
3838
fun testLimitedExecutor() = runTest {
3939
val view = executor.limitedParallelism(targetParallelism)
40-
repeat(iterations) {
41-
launch(view) {
42-
checkParallelism()
40+
doStress {
41+
repeat(iterations) {
42+
launch(view) {
43+
checkParallelism()
44+
}
4345
}
4446
}
4547
}
4648

4749
@Test
4850
fun testLimitedDispatchersIo() = runTest {
4951
val view = Dispatchers.IO.limitedParallelism(targetParallelism)
50-
repeat(iterations) {
51-
launch(view) {
52-
checkParallelism()
52+
doStress {
53+
repeat(iterations) {
54+
launch(view) {
55+
checkParallelism()
56+
}
5357
}
5458
}
5559
}
5660

5761
@Test
5862
fun testLimitedDispatchersIoDispatchYield() = runTest {
5963
val view = Dispatchers.IO.limitedParallelism(targetParallelism)
60-
repeat(iterations) {
64+
doStress {
6165
launch(view) {
6266
yield()
6367
checkParallelism()
@@ -68,16 +72,26 @@ class LimitedParallelismStressTest(private val targetParallelism: Int) : TestBas
6872
@Test
6973
fun testLimitedExecutorReachesTargetParallelism() = runTest {
7074
val view = executor.limitedParallelism(targetParallelism)
71-
repeat(iterations) {
72-
val barrier = CyclicBarrier(targetParallelism + 1)
73-
repeat(targetParallelism) {
74-
launch(view) {
75-
barrier.await()
75+
doStress {
76+
repeat(iterations) {
77+
val barrier = CyclicBarrier(targetParallelism + 1)
78+
repeat(targetParallelism) {
79+
launch(view) {
80+
barrier.await()
81+
}
7682
}
83+
// Successfully awaited parallelism + 1
84+
barrier.await()
85+
coroutineContext.job.children.toList().joinAll()
86+
}
87+
}
88+
}
89+
90+
private suspend inline fun doStress(crossinline block: suspend CoroutineScope.() -> Unit) {
91+
repeat(stressTestMultiplier) {
92+
coroutineScope {
93+
block()
7794
}
78-
// Successfully awaited parallelism + 1
79-
barrier.await()
80-
coroutineContext.job.children.toList().joinAll()
8195
}
8296
}
8397
}

0 commit comments

Comments
 (0)