aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt
blob: a6f4dd6b1858af90b1be64337424e5eccb029a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

import kotlinx.coroutines.flow.*
import kotlinx.coroutines.scheduling.*
import org.junit.*
import org.junit.Test
import java.util.concurrent.*
import kotlin.test.*

class RejectedExecutionTest : TestBase() {
    private val threadName = "RejectedExecutionTest"
    private val executor = RejectingExecutor()

    @After
    fun tearDown() {
        executor.shutdown()
        executor.awaitTermination(10, TimeUnit.SECONDS)
    }

    @Test
    fun testRejectOnLaunch() = runTest {
        expect(1)
        val job = launch(executor.asCoroutineDispatcher()) {
            expectUnreached()
        }
        assertEquals(1, executor.submittedTasks)
        assertTrue(job.isCancelled)
        finish(2)
    }

    @Test
    fun testRejectOnLaunchAtomic() = runTest {
        expect(1)
        val job = launch(executor.asCoroutineDispatcher(), start = CoroutineStart.ATOMIC) {
            expect(2)
            assertEquals(true, coroutineContext[Job]?.isCancelled)
            assertIoThread() // was rejected on start, but start was atomic
        }
        assertEquals(1, executor.submittedTasks)
        job.join()
        finish(3)
    }

    @Test
    fun testRejectOnWithContext() = runTest {
        expect(1)
        assertFailsWith<CancellationException> {
            withContext(executor.asCoroutineDispatcher()) {
                expectUnreached()
            }
        }
        assertEquals(1, executor.submittedTasks)
        finish(2)
    }

    @Test
    fun testRejectOnResumeInContext() = runTest {
        expect(1)
        executor.acceptTasks = 1 // accept one task
        assertFailsWith<CancellationException> {
                withContext(executor.asCoroutineDispatcher()) {
                    expect(2)
                    assertExecutorThread()
                    try {
                        withContext(Dispatchers.Default) {
                            expect(3)
                            assertDefaultDispatcherThread()
                            // We have to wait until caller executor thread had already suspended (if not running task),
                            // so that we resume back to it a new task is posted
                            executor.awaitNotRunningTask()
                            expect(4)
                            assertDefaultDispatcherThread()
                        }
                        // cancelled on resume back
                    } finally {
                        expect(5)
                        assertIoThread()
                    }
                    expectUnreached()
                }
        }
        assertEquals(2, executor.submittedTasks)
        finish(6)
    }

    @Test
    fun testRejectOnDelay() = runTest {
        expect(1)
        executor.acceptTasks = 1 // accept one task
        assertFailsWith<CancellationException> {
            withContext(executor.asCoroutineDispatcher()) {
                expect(2)
                assertExecutorThread()
                try {
                    delay(10) // cancelled
                } finally {
                    // Since it was cancelled on attempt to delay, it still stays on the same thread
                    assertExecutorThread()
                }
                expectUnreached()
            }
        }
        assertEquals(2, executor.submittedTasks)
        finish(3)
    }

    @Test
    fun testRejectWithTimeout() = runTest {
        expect(1)
        executor.acceptTasks = 1 // accept one task
        assertFailsWith<CancellationException> {
            withContext(executor.asCoroutineDispatcher()) {
                expect(2)
                assertExecutorThread()
                withTimeout(1000) {
                    expect(3) // atomic entry into the block (legacy behavior, it seem to be Ok with way)
                    assertEquals(true, coroutineContext[Job]?.isCancelled) // but the job is already cancelled
                }
                expectUnreached()
            }
        }
        assertEquals(2, executor.submittedTasks)
        finish(4)
    }

    private inner class RejectingExecutor : ScheduledThreadPoolExecutor(1, { r -> Thread(r, threadName) }) {
        var acceptTasks = 0
        var submittedTasks = 0
        val runningTask = MutableStateFlow(false)

        override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> {
            submittedTasks++
            if (submittedTasks > acceptTasks) throw RejectedExecutionException()
            val wrapper = Runnable {
                runningTask.value = true
                try {
                    command.run()
                } finally {
                    runningTask.value = false
                }
            }
            return super.schedule(wrapper, delay, unit)
        }

        suspend fun awaitNotRunningTask() = runningTask.first { !it }
    }

    private fun assertExecutorThread() {
        val thread = Thread.currentThread()
        if (!thread.name.startsWith(threadName)) error("Not an executor thread: $thread")
    }

    private fun assertDefaultDispatcherThread() {
        val thread = Thread.currentThread()
        if (thread !is CoroutineScheduler.Worker) error("Not a thread from Dispatchers.Default: $thread")
        assertEquals(CoroutineScheduler.WorkerState.CPU_ACQUIRED, thread.state)
    }

    private fun assertIoThread() {
        val thread = Thread.currentThread()
        if (thread !is CoroutineScheduler.Worker) error("Not a thread from Dispatchers.IO: $thread")
        assertEquals(CoroutineScheduler.WorkerState.BLOCKING, thread.state)
    }
}