Skip to content

Commit 2e3df14

Browse files
committed
~ Better tests
1 parent 53a20d1 commit 2e3df14

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.coroutines
@@ -29,6 +29,8 @@ class ExecutorsTest : TestBase() {
2929
val context = newFixedThreadPoolContext(2, "TestPool")
3030
runBlocking(context) {
3131
checkThreadName("TestPool")
32+
delay(10)
33+
checkThreadName("TestPool") // should dispatch on the right thread
3234
}
3335
context.close()
3436
}
@@ -38,6 +40,8 @@ class ExecutorsTest : TestBase() {
3840
val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") }
3941
runBlocking(executor.asCoroutineDispatcher()) {
4042
checkThreadName("TestExecutor")
43+
delay(10)
44+
checkThreadName("TestExecutor") // should dispatch on the right thread
4145
}
4246
executor.shutdown()
4347
}

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

+33-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.scheduling.*
78
import org.junit.*
89
import org.junit.Test
910
import java.util.concurrent.*
@@ -36,7 +37,7 @@ class RejectedExecutionTest : TestBase() {
3637
val job = launch(executor.asCoroutineDispatcher(), start = CoroutineStart.ATOMIC) {
3738
expect(2)
3839
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
4041
}
4142
assertEquals(1, executor.submittedTasks)
4243
job.join()
@@ -60,14 +61,19 @@ class RejectedExecutionTest : TestBase() {
6061
expect(1)
6162
executor.acceptTasks = 1 // accept one task
6263
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()
6776
}
68-
// cancelled on resume back
69-
expectUnreached()
70-
}
7177
}
7278
assertEquals(2, executor.submittedTasks)
7379
finish(4)
@@ -80,7 +86,13 @@ class RejectedExecutionTest : TestBase() {
8086
assertFailsWith<CancellationException> {
8187
withContext(executor.asCoroutineDispatcher()) {
8288
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+
}
8496
expectUnreached()
8597
}
8698
}
@@ -95,6 +107,7 @@ class RejectedExecutionTest : TestBase() {
95107
assertFailsWith<CancellationException> {
96108
withContext(executor.asCoroutineDispatcher()) {
97109
expect(2)
110+
assertExecutorThread()
98111
withTimeout(1000) {
99112
expect(3) // atomic entry into the block (legacy behavior, it seem to be Ok with way)
100113
assertEquals(true, coroutineContext[Job]?.isCancelled) // but the job is already cancelled
@@ -116,4 +129,15 @@ class RejectedExecutionTest : TestBase() {
116129
return super.schedule(command, delay, unit)
117130
}
118131
}
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+
}
119143
}

0 commit comments

Comments
 (0)