Skip to content

Commit fb6f240

Browse files
committed
Make scheduler stress test pass on small machines (Amazon i3)
1 parent ec05e8a commit fb6f240

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class BlockingCoroutineDispatcherTest : SchedulerTestBase() {
131131
barrier.await()
132132
blockingTasks.joinAll()
133133
// There may be race when multiple CPU threads are trying to lazily created one more
134-
checkPoolThreadsCreated(104..110)
134+
checkPoolThreadsCreated(104..120)
135135
}
136136

137137
@Test

core/kotlinx-coroutines-core/test/scheduling/CoroutineSchedulerStressTest.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ class CoroutineSchedulerStressTest : TestBase() {
1717

1818
private var dispatcher: ExperimentalCoroutineDispatcher = ExperimentalCoroutineDispatcher()
1919
private val observedThreads = ConcurrentHashMap<Thread, Long>()
20-
private val tasksNum = 2_000_000 * stressTestMultiplier
20+
private val tasksNum = 2_000_000 * stressMemoryMultiplier()
21+
22+
private fun stressMemoryMultiplier(): Int {
23+
return if (isStressTest) {
24+
Runtime.getRuntime().availableProcessors() * 4
25+
} else {
26+
1
27+
}
28+
}
29+
2130
private val processed = AtomicInteger(0)
2231
private val finishLatch = CountDownLatch(1)
2332

@@ -48,27 +57,20 @@ class CoroutineSchedulerStressTest : TestBase() {
4857
// Submit million tasks
4958
blockingThread = Thread.currentThread()
5059
var submittedTasks = 0
51-
val processedCounter = AtomicLong(0)
52-
while (submittedTasks <= tasksNum) {
53-
for (i in 1..120) {
54-
if (++submittedTasks > tasksNum) {
55-
// Block current thread
56-
finishLatch.await()
57-
return@Runnable
58-
}
59-
60-
dispatcher.dispatch(EmptyCoroutineContext, Runnable {
61-
processTask()
62-
processedCounter.incrementAndGet()
63-
})
64-
}
60+
while (submittedTasks < tasksNum) {
6561

66-
while (processedCounter.get() < 100) {
62+
++submittedTasks
63+
dispatcher.dispatch(EmptyCoroutineContext, Runnable {
64+
processTask()
65+
})
66+
67+
while (submittedTasks - processed.get() > 100) {
6768
Thread.yield()
6869
}
69-
70-
processedCounter.set(0L)
7170
}
71+
72+
// Block current thread
73+
finishLatch.await()
7274
})
7375

7476
finishLatch.await()
@@ -92,7 +94,7 @@ class CoroutineSchedulerStressTest : TestBase() {
9294
})
9395

9496
finishLatch.await()
95-
assertEquals(Runtime.getRuntime().availableProcessors(), observedThreads.size)
97+
assertTrue(Runtime.getRuntime().availableProcessors() in (observedThreads.size - 1)..observedThreads.size)
9698
validateResults()
9799
}
98100

0 commit comments

Comments
 (0)