4
4
5
5
package kotlinx.coroutines
6
6
7
+ import kotlinx.coroutines.scheduling.*
7
8
import org.junit.*
8
9
import org.junit.Test
9
10
import java.util.concurrent.*
@@ -36,7 +37,7 @@ class RejectedExecutionTest : TestBase() {
36
37
val job = launch(executor.asCoroutineDispatcher(), start = CoroutineStart .ATOMIC ) {
37
38
expect(2 )
38
39
assertEquals(true , coroutineContext[Job ]?.isCancelled)
39
- assertNotSame(threadName, Thread .currentThread().name) // should have got dispatched on the DefaultExecutor
40
+ assertIoThread() // was rejected on start, but start was atomic
40
41
}
41
42
assertEquals(1 , executor.submittedTasks)
42
43
job.join()
@@ -60,14 +61,19 @@ class RejectedExecutionTest : TestBase() {
60
61
expect(1 )
61
62
executor.acceptTasks = 1 // accept one task
62
63
assertFailsWith<CancellationException > {
63
- withContext(executor.asCoroutineDispatcher()) {
64
- expect(2 )
65
- withContext(Dispatchers .Default ) {
66
- expect(3 )
64
+ withContext(executor.asCoroutineDispatcher()) {
65
+ expect(2 )
66
+ assertExecutorThread()
67
+ try {
68
+ withContext(Dispatchers .Default ) {
69
+ expect(3 )
70
+ }
71
+ // cancelled on resume back
72
+ } finally {
73
+ assertIoThread()
74
+ }
75
+ expectUnreached()
67
76
}
68
- // cancelled on resume back
69
- expectUnreached()
70
- }
71
77
}
72
78
assertEquals(2 , executor.submittedTasks)
73
79
finish(4 )
@@ -80,7 +86,13 @@ class RejectedExecutionTest : TestBase() {
80
86
assertFailsWith<CancellationException > {
81
87
withContext(executor.asCoroutineDispatcher()) {
82
88
expect(2 )
83
- delay(10 ) // cancelled
89
+ assertExecutorThread()
90
+ try {
91
+ delay(10 ) // cancelled
92
+ } finally {
93
+ // Since it was cancelled on attempt to delay, it still stays on the same thread
94
+ assertExecutorThread()
95
+ }
84
96
expectUnreached()
85
97
}
86
98
}
@@ -95,6 +107,7 @@ class RejectedExecutionTest : TestBase() {
95
107
assertFailsWith<CancellationException > {
96
108
withContext(executor.asCoroutineDispatcher()) {
97
109
expect(2 )
110
+ assertExecutorThread()
98
111
withTimeout(1000 ) {
99
112
expect(3 ) // atomic entry into the block (legacy behavior, it seem to be Ok with way)
100
113
assertEquals(true , coroutineContext[Job ]?.isCancelled) // but the job is already cancelled
@@ -116,4 +129,15 @@ class RejectedExecutionTest : TestBase() {
116
129
return super .schedule(command, delay, unit)
117
130
}
118
131
}
132
+
133
+ private fun assertExecutorThread () {
134
+ val thread = Thread .currentThread()
135
+ if (! thread.name.startsWith(threadName)) error(" Not an executor thread: $thread " )
136
+ }
137
+
138
+ private fun assertIoThread () {
139
+ val thread = Thread .currentThread()
140
+ if (thread !is CoroutineScheduler .Worker ) error(" Not a thread from Dispatchers.IO: $thread " )
141
+ assertEquals(CoroutineScheduler .WorkerState .BLOCKING , thread.state)
142
+ }
119
143
}
0 commit comments