|
4 | 4 |
|
5 | 5 | package kotlinx.coroutines
|
6 | 6 |
|
| 7 | +import kotlinx.coroutines.flow.* |
7 | 8 | import kotlinx.coroutines.scheduling.*
|
8 | 9 | import org.junit.*
|
9 | 10 | import org.junit.Test
|
@@ -68,17 +69,22 @@ class RejectedExecutionTest : TestBase() {
|
68 | 69 | withContext(Dispatchers.Default) {
|
69 | 70 | expect(3)
|
70 | 71 | 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() |
71 | 77 | }
|
72 | 78 | // cancelled on resume back
|
73 | 79 | } finally {
|
74 |
| - expect(4) |
| 80 | + expect(5) |
75 | 81 | assertIoThread()
|
76 | 82 | }
|
77 | 83 | expectUnreached()
|
78 | 84 | }
|
79 | 85 | }
|
80 | 86 | assertEquals(2, executor.submittedTasks)
|
81 |
| - finish(5) |
| 87 | + finish(6) |
82 | 88 | }
|
83 | 89 |
|
84 | 90 | @Test
|
@@ -124,12 +130,23 @@ class RejectedExecutionTest : TestBase() {
|
124 | 130 | private inner class RejectingExecutor : ScheduledThreadPoolExecutor(1, { r -> Thread(r, threadName) }) {
|
125 | 131 | var acceptTasks = 0
|
126 | 132 | var submittedTasks = 0
|
| 133 | + val runningTask = MutableStateFlow(false) |
127 | 134 |
|
128 | 135 | override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> {
|
129 | 136 | submittedTasks++
|
130 | 137 | 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) |
132 | 147 | }
|
| 148 | + |
| 149 | + suspend fun awaitNotRunningTask() = runningTask.first { !it } |
133 | 150 | }
|
134 | 151 |
|
135 | 152 | private fun assertExecutorThread() {
|
|
0 commit comments