Skip to content

Commit f86636f

Browse files
committed
~ Fixed race in testRejectOnResumeInContext
1 parent fda826c commit f86636f

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.flow.*
78
import kotlinx.coroutines.scheduling.*
89
import org.junit.*
910
import org.junit.Test
@@ -68,17 +69,22 @@ class RejectedExecutionTest : TestBase() {
6869
withContext(Dispatchers.Default) {
6970
expect(3)
7071
assertDefaultDispatcherThread()
72+
// We have to wait until caller executor thread had already suspended (if not running task),
73+
// so that we resume back to it a new task is posted
74+
executor.awaitNotRunningTask()
75+
expect(4)
76+
assertDefaultDispatcherThread()
7177
}
7278
// cancelled on resume back
7379
} finally {
74-
expect(4)
80+
expect(5)
7581
assertIoThread()
7682
}
7783
expectUnreached()
7884
}
7985
}
8086
assertEquals(2, executor.submittedTasks)
81-
finish(5)
87+
finish(6)
8288
}
8389

8490
@Test
@@ -124,12 +130,23 @@ class RejectedExecutionTest : TestBase() {
124130
private inner class RejectingExecutor : ScheduledThreadPoolExecutor(1, { r -> Thread(r, threadName) }) {
125131
var acceptTasks = 0
126132
var submittedTasks = 0
133+
val runningTask = MutableStateFlow(false)
127134

128135
override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> {
129136
submittedTasks++
130137
if (submittedTasks > acceptTasks) throw RejectedExecutionException()
131-
return super.schedule(command, delay, unit)
138+
val wrapper = Runnable {
139+
runningTask.value = true
140+
try {
141+
command.run()
142+
} finally {
143+
runningTask.value = false
144+
}
145+
}
146+
return super.schedule(wrapper, delay, unit)
132147
}
148+
149+
suspend fun awaitNotRunningTask() = runningTask.first { !it }
133150
}
134151

135152
private fun assertExecutorThread() {

0 commit comments

Comments
 (0)