From d16a209df9a4dcdc80fa1135cc335cc07eaa73f3 Mon Sep 17 00:00:00 2001 From: Roman Elizarov Date: Fri, 15 May 2020 16:00:36 +0300 Subject: [PATCH] ~ Better tests --- .../jvm/test/ExecutorsTest.kt | 6 ++- .../jvm/test/RejectedExecutionTest.kt | 42 +++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt b/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt index 033b9b7bc9..ebf08a03d0 100644 --- a/kotlinx-coroutines-core/jvm/test/ExecutorsTest.kt +++ b/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 @@ -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() } @@ -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() } diff --git a/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt b/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt index 27eb2afeb1..8e5fea9b36 100644 --- a/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt +++ b/kotlinx-coroutines-core/jvm/test/RejectedExecutionTest.kt @@ -4,6 +4,7 @@ package kotlinx.coroutines +import kotlinx.coroutines.scheduling.* import org.junit.* import org.junit.Test import java.util.concurrent.* @@ -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() @@ -60,14 +61,19 @@ class RejectedExecutionTest : TestBase() { expect(1) executor.acceptTasks = 1 // accept one task assertFailsWith { - 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) @@ -80,7 +86,13 @@ class RejectedExecutionTest : TestBase() { assertFailsWith { 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() } } @@ -95,6 +107,7 @@ class RejectedExecutionTest : TestBase() { assertFailsWith { 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 @@ -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) + } } \ No newline at end of file