/
RejectedExecutionTest.kt
119 lines (107 loc) · 3.57 KB
/
RejectedExecutionTest.kt
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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
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)
assertNotSame(threadName, Thread.currentThread().name) // should have got dispatched on the DefaultExecutor
}
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)
withContext(Dispatchers.Default) {
expect(3)
}
// cancelled on resume back
expectUnreached()
}
}
assertEquals(2, executor.submittedTasks)
finish(4)
}
@Test
fun testRejectOnDelay() = runTest {
expect(1)
executor.acceptTasks = 1 // accept one task
assertFailsWith<CancellationException> {
withContext(executor.asCoroutineDispatcher()) {
expect(2)
delay(10) // cancelled
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)
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
override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> {
submittedTasks++
if (submittedTasks > acceptTasks) throw RejectedExecutionException()
return super.schedule(command, delay, unit)
}
}
}