Skip to content

Commit

Permalink
~ Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed May 25, 2020
1 parent 240b7da commit d16a209
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
6 changes: 5 additions & 1 deletion kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines
Expand Down Expand Up @@ -29,6 +29,8 @@ class ExecutorsTest : TestBase() {
val context = newFixedThreadPoolContext(2, "TestPool")
runBlocking(context) {
checkThreadName("TestPool")
delay(10)
checkThreadName("TestPool") // should dispatch on the right thread
}
context.close()
}
Expand All @@ -38,6 +40,8 @@ class ExecutorsTest : TestBase() {
val executor = Executors.newSingleThreadExecutor { r -> Thread(r, "TestExecutor") }
runBlocking(executor.asCoroutineDispatcher()) {
checkThreadName("TestExecutor")
delay(10)
checkThreadName("TestExecutor") // should dispatch on the right thread
}
executor.shutdown()
}
Expand Down
42 changes: 33 additions & 9 deletions kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt
Expand Up @@ -4,6 +4,7 @@

package kotlinx.coroutines

import kotlinx.coroutines.scheduling.*
import org.junit.*
import org.junit.Test
import java.util.concurrent.*
Expand Down Expand Up @@ -36,7 +37,7 @@ class RejectedExecutionTest : TestBase() {
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
assertIoThread() // was rejected on start, but start was atomic
}
assertEquals(1, executor.submittedTasks)
job.join()
Expand All @@ -60,14 +61,19 @@ class RejectedExecutionTest : TestBase() {
expect(1)
executor.acceptTasks = 1 // accept one task
assertFailsWith<CancellationException> {
withContext(executor.asCoroutineDispatcher()) {
expect(2)
withContext(Dispatchers.Default) {
expect(3)
withContext(executor.asCoroutineDispatcher()) {
expect(2)
assertExecutorThread()
try {
withContext(Dispatchers.Default) {
expect(3)
}
// cancelled on resume back
} finally {
assertIoThread()
}
expectUnreached()
}
// cancelled on resume back
expectUnreached()
}
}
assertEquals(2, executor.submittedTasks)
finish(4)
Expand All @@ -80,7 +86,13 @@ class RejectedExecutionTest : TestBase() {
assertFailsWith<CancellationException> {
withContext(executor.asCoroutineDispatcher()) {
expect(2)
delay(10) // cancelled
assertExecutorThread()
try {
delay(10) // cancelled
} finally {
// Since it was cancelled on attempt to delay, it still stays on the same thread
assertExecutorThread()
}
expectUnreached()
}
}
Expand All @@ -95,6 +107,7 @@ class RejectedExecutionTest : TestBase() {
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
Expand All @@ -116,4 +129,15 @@ class RejectedExecutionTest : TestBase() {
return super.schedule(command, delay, unit)
}
}

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

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)
}
}

0 comments on commit d16a209

Please sign in to comment.