Skip to content

Commit c702d2a

Browse files
committed
Move tests around
also fix wrong package names
1 parent 3561c13 commit c702d2a

6 files changed

+97
-111
lines changed

kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherLivenessStressTest.kt

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import kotlinx.coroutines.testing.*
44
import kotlinx.coroutines.*
55
import org.junit.*
66
import org.junit.Test
7-
import org.junit.runner.*
8-
import org.junit.runners.*
9-
import java.util.LinkedList
10-
import java.util.concurrent.*
117
import java.util.concurrent.atomic.*
128
import kotlin.test.*
139

kotlinx-coroutines-core/jvm/test/scheduling/BlockingCoroutineDispatcherTest.kt

+1-46
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package kotlinx.coroutines.scheduling
22

3+
import kotlinx.coroutines.testing.*
34
import kotlinx.coroutines.*
45
import org.junit.*
56
import org.junit.rules.*
67
import java.util.concurrent.*
7-
import java.util.concurrent.atomic.*
88

99
class BlockingCoroutineDispatcherTest : SchedulerTestBase() {
1010

@@ -171,49 +171,4 @@ class BlockingCoroutineDispatcherTest : SchedulerTestBase() {
171171
fun testZeroParallelism() {
172172
blockingDispatcher(0)
173173
}
174-
175-
@Test
176-
fun testNoCpuStarvationRunBlockingOnDefaultDispatcherThread() {
177-
val maxDepth = CORES_COUNT * 3 + 3
178-
fun body(depth: Int) {
179-
if (depth == maxDepth) return
180-
runBlocking(dispatcher) {
181-
launch(dispatcher) {
182-
body(depth + 1)
183-
}
184-
}
185-
}
186-
187-
body(1)
188-
checkPoolThreadsCreated(maxDepth..maxDepth + 1)
189-
}
190-
191-
@Test
192-
fun testNoStarvationOfLimitedDispatcherWithRunBlocking() {
193-
val taskCount = 5
194-
val dispatcher = blockingDispatcher(2)
195-
val barrier = CompletableDeferred<Unit>()
196-
val count = AtomicInteger(0)
197-
fun blockingCode() {
198-
runBlocking {
199-
count.incrementAndGet()
200-
barrier.await()
201-
count.decrementAndGet()
202-
}
203-
}
204-
runBlocking {
205-
repeat(taskCount) {
206-
launch(dispatcher) {
207-
blockingCode()
208-
}
209-
}
210-
while (count.get() != taskCount) {
211-
Thread.sleep(1)
212-
}
213-
barrier.complete(Unit)
214-
while (count.get() != 0) {
215-
Thread.sleep(1)
216-
}
217-
}
218-
}
219174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package kotlinx.coroutines.scheduling
2+
3+
import kotlinx.coroutines.*
4+
import org.junit.*
5+
import java.util.concurrent.atomic.*
6+
7+
class RunBlockingCoroutineDispatcherTest : SchedulerTestBase() {
8+
@Test
9+
fun testRecursiveRunBlockingCanExceedDefaultDispatcherLimit() {
10+
val maxDepth = CORES_COUNT * 3 + 3
11+
fun body(depth: Int) {
12+
if (depth == maxDepth) return
13+
runBlocking(Dispatchers.Default) {
14+
launch(Dispatchers.Default) {
15+
body(depth + 1)
16+
}
17+
}
18+
}
19+
20+
body(1)
21+
checkPoolThreadsCreated(maxDepth..maxDepth + 1)
22+
}
23+
24+
@Test
25+
fun testNoDefaultDispatcherStarvationWithRunBlocking() = testRunBlockingCanExceedDispatchersLimit(dispatcher, CORE_POOL_SIZE * 3 + 3)
26+
27+
@Test
28+
fun testNoIoDispatcherStarvationWithRunBlocking() = testRunBlockingCanExceedDispatchersLimit(blockingDispatcher(2), 5)
29+
30+
private fun testRunBlockingCanExceedDispatchersLimit(targetDispatcher: CoroutineDispatcher, threadsToReach: Int) {
31+
val barrier = CompletableDeferred<Unit>()
32+
val count = AtomicInteger(0)
33+
fun blockingCode() {
34+
runBlocking {
35+
count.incrementAndGet()
36+
barrier.await()
37+
count.decrementAndGet()
38+
}
39+
}
40+
runBlocking {
41+
repeat(threadsToReach) {
42+
launch(targetDispatcher) {
43+
blockingCode()
44+
}
45+
}
46+
while (count.get() != threadsToReach) {
47+
Thread.sleep(1)
48+
}
49+
async(targetDispatcher) {
50+
yield()
51+
42
52+
}.join()
53+
barrier.complete(Unit)
54+
while (count.get() != 0) {
55+
Thread.sleep(1)
56+
}
57+
}
58+
}
59+
}

kotlinx-coroutines-core/jvm/test/scheduling/RunBlockingCoroutineSchedulerLivenessTestBase.kt renamed to kotlinx-coroutines-core/jvm/test/scheduling/RunBlockingCoroutineSchedulerLivenessStressTest.kt

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
1-
package scheduling
1+
package kotlinx.coroutines.scheduling
22

33
import kotlinx.coroutines.*
4-
import kotlinx.coroutines.scheduling.*
54
import kotlinx.coroutines.testing.*
5+
import org.junit.*
6+
import org.junit.runner.*
7+
import org.junit.runners.*
68
import java.util.*
79
import java.util.concurrent.*
810

9-
open class RunBlockingCoroutineSchedulerLivenessTestBase : SchedulerTestBase() {
10-
protected fun testSchedulerLiveness(targetDispatcher: CoroutineDispatcher, yieldMask: Int = 0b1111): Unit = runBlocking {
11+
@RunWith(Parameterized::class)
12+
class RunBlockingCoroutineSchedulerLivenessStressTest(private val yieldMask: Int) : SchedulerTestBase() {
13+
init {
14+
corePoolSize = 1
15+
}
16+
17+
companion object {
18+
@JvmStatic
19+
@Parameterized.Parameters
20+
fun data(): Array<Array<Any?>> {
21+
return Array(16 * stressTestMultiplierSqrt) { arrayOf(it) }
22+
}
23+
}
24+
25+
@Test
26+
fun testLivenessOfDefaultDispatcher(): Unit = testSchedulerLiveness(dispatcher, yieldMask)
27+
28+
@Test
29+
fun testLivenessOfIoDispatcher(): Unit = testSchedulerLiveness(blockingDispatcher(1), yieldMask)
30+
31+
@Test
32+
fun testLivenessOfLimitedDispatcherOnTopOfDefaultDispatcher() =
33+
testSchedulerLiveness(dispatcher.limitedParallelism(1), yieldMask)
34+
35+
@Test
36+
fun testLivenessOfLimitedDispatcherOnTopOfIoDispatcher() = testSchedulerLiveness(
37+
// Important: inner limitedDispatcher will be on top of this LimitedDispatcher, so there are two Workers from
38+
// two different LimitedDispatchers that must coordinate their permits, not just one.
39+
// In other words, LimitedDispatcher's Worker should also respect BlockingDispatchAware on its inner tasks
40+
blockingDispatcher.value.limitedParallelism(1), yieldMask
41+
)
42+
43+
private fun testSchedulerLiveness(targetDispatcher: CoroutineDispatcher, yieldMask: Int = 0b1111): Unit = runBlocking {
1144
val oldRunBlockings = LinkedList<Job>()
1245
var maxOldRunBlockings = 0
1346
var busyWaits = 0

kotlinx-coroutines-core/jvm/test/scheduling/RunBlockingDefaultDispatcherLivenessStressTest.kt

-24
This file was deleted.

kotlinx-coroutines-core/jvm/test/scheduling/RunBlockingLimitedDispatcherLivenessStressTest.kt

-33
This file was deleted.

0 commit comments

Comments
 (0)