@@ -17,7 +17,16 @@ class CoroutineSchedulerStressTest : TestBase() {
17
17
18
18
private var dispatcher: ExperimentalCoroutineDispatcher = ExperimentalCoroutineDispatcher ()
19
19
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
+
21
30
private val processed = AtomicInteger (0 )
22
31
private val finishLatch = CountDownLatch (1 )
23
32
@@ -48,27 +57,20 @@ class CoroutineSchedulerStressTest : TestBase() {
48
57
// Submit million tasks
49
58
blockingThread = Thread .currentThread()
50
59
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) {
65
61
66
- while (processedCounter.get() < 100 ) {
62
+ ++ submittedTasks
63
+ dispatcher.dispatch(EmptyCoroutineContext , Runnable {
64
+ processTask()
65
+ })
66
+
67
+ while (submittedTasks - processed.get() > 100 ) {
67
68
Thread .yield ()
68
69
}
69
-
70
- processedCounter.set(0L )
71
70
}
71
+
72
+ // Block current thread
73
+ finishLatch.await()
72
74
})
73
75
74
76
finishLatch.await()
@@ -92,7 +94,7 @@ class CoroutineSchedulerStressTest : TestBase() {
92
94
})
93
95
94
96
finishLatch.await()
95
- assertEquals (Runtime .getRuntime().availableProcessors(), observedThreads.size)
97
+ assertTrue (Runtime .getRuntime().availableProcessors() in (observedThreads.size - 1 ) .. observedThreads.size)
96
98
validateResults()
97
99
}
98
100
0 commit comments