From 608783e7cccd39e6dfcaa23beccd7ad495e28324 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Fri, 15 Oct 2021 14:26:35 +0300 Subject: [PATCH] Fix build --- .../common/test/RunTestTest.kt | 33 ++++++++ .../common/test/TestCoroutineSchedulerTest.kt | 84 +++++++++++++++++-- .../common/test/TestCoroutineScopeTest.kt | 12 +-- .../common/test/TestDispatchersTest.kt | 2 +- .../jvm/test/{Helpers.kt => HelpersJvm.kt} | 2 - .../native/test/Helpers.kt | 2 - 6 files changed, 120 insertions(+), 15 deletions(-) rename kotlinx-coroutines-test/jvm/test/{Helpers.kt => HelpersJvm.kt} (89%) diff --git a/kotlinx-coroutines-test/common/test/RunTestTest.kt b/kotlinx-coroutines-test/common/test/RunTestTest.kt index b0411b9e68..68c9ab9eb5 100644 --- a/kotlinx-coroutines-test/common/test/RunTestTest.kt +++ b/kotlinx-coroutines-test/common/test/RunTestTest.kt @@ -120,4 +120,37 @@ class RunTestTest { } } + /** Tests that passing invalid contexts to [runTest] causes it to fail (on JS, without forking). */ + @Test + fun testRunTestWithIllegalContext() { + for (ctx in TestCoroutineScopeTest.invalidContexts) { + assertFailsWith { + runTest(ctx) { } + } + } + } + + /** Tests that throwing exceptions in [runTest] fails the test with them. */ + @Test + fun testThrowingInRunTestBody() = testResultMap({ + assertFailsWith { it() } + }) { + runTest { + throw RuntimeException() + } + } + + /** Tests that throwing exceptions in pending tasks [runTest] fails the test with them. */ + @Test + fun testThrowingInRunTestPendingTask() = testResultMap({ + assertFailsWith { it() } + }) { + runTest { + launch { + delay(SLOW) + throw RuntimeException() + } + } + } + } diff --git a/kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt b/kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt index f7d2ed13fd..8751f7d067 100644 --- a/kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt +++ b/kotlinx-coroutines-test/common/test/TestCoroutineSchedulerTest.kt @@ -5,13 +5,12 @@ package kotlinx.coroutines.test import kotlinx.coroutines.* -import kotlin.coroutines.* import kotlin.test.* class TestCoroutineSchedulerTest { /** Tests that `TestCoroutineScheduler` attempts to detect if there are several instances of it. */ @Test - fun testContextElement() = runBlockingTest { + fun testContextElement() = runTest { assertFailsWith { withContext(TestCoroutineDispatcher()) { } @@ -21,7 +20,7 @@ class TestCoroutineSchedulerTest { /** Tests that, as opposed to [DelayController.advanceTimeBy] or [TestCoroutineScope.advanceTimeBy], * [TestCoroutineScheduler.advanceTimeBy] doesn't run the tasks scheduled at the target moment. */ @Test - fun testAdvanceTimeByDoesNotRunCurrent() = runBlockingTest { + fun testAdvanceTimeByDoesNotRunCurrent() = runTest { var entered = false launch { delay(15) @@ -45,7 +44,7 @@ class TestCoroutineSchedulerTest { /** Tests that if [TestCoroutineScheduler.advanceTimeBy] encounters an arithmetic overflow, all the tasks scheduled * until the moment [Long.MAX_VALUE] get run. */ @Test - fun testAdvanceTimeByEnormousDelays() = runBlockingTest { + fun testAdvanceTimeByEnormousDelays() = runTest { val initialDelay = 10L delay(initialDelay) assertEquals(initialDelay, currentTime) @@ -99,7 +98,7 @@ class TestCoroutineSchedulerTest { /** Tests the basic functionality of [TestCoroutineScheduler.runCurrent]. */ @Test - fun testRunCurrent() = runBlockingTest { + fun testRunCurrent() = runTest { var stage = 0 launch { delay(1) @@ -182,4 +181,79 @@ class TestCoroutineSchedulerTest { stage = 1 scope.runCurrent() } + + private fun TestCoroutineScope.checkTimeout( + timesOut: Boolean, timeoutMillis: Long = SLOW, block: suspend () -> Unit + ) = assertRunsFast { + var caughtException = false + launch { + try { + withTimeout(timeoutMillis) { + block() + } + } catch (e: TimeoutCancellationException) { + caughtException = true + } + } + advanceUntilIdle() + cleanupTestCoroutines() + if (timesOut) + assertTrue(caughtException) + else + assertFalse(caughtException) + } + + /** Tests that timeouts get triggered. */ + @Test + fun testSmallTimeouts() { + val scope = TestCoroutineScope() + scope.checkTimeout(true) { + val half = SLOW / 2 + delay(half) + delay(SLOW - half) + } + } + + /** Tests that timeouts don't get triggered if the code finishes in time. */ + @Test + fun testLargeTimeouts() { + val scope = TestCoroutineScope() + scope.checkTimeout(false) { + val half = SLOW / 2 + delay(half) + delay(SLOW - half - 1) + } + } + + /** Tests that timeouts get triggered if the code fails to finish in time asynchronously. */ + @Test + fun testSmallAsynchronousTimeouts() { + val scope = TestCoroutineScope() + val deferred = CompletableDeferred() + scope.launch { + val half = SLOW / 2 + delay(half) + delay(SLOW - half) + deferred.complete(Unit) + } + scope.checkTimeout(true) { + deferred.await() + } + } + + /** Tests that timeouts don't get triggered if the code finishes in time, even if it does so asynchronously. */ + @Test + fun testLargeAsynchronousTimeouts() { + val scope = TestCoroutineScope() + val deferred = CompletableDeferred() + scope.launch { + val half = SLOW / 2 + delay(half) + delay(SLOW - half - 1) + deferred.complete(Unit) + } + scope.checkTimeout(false) { + deferred.await() + } + } } diff --git a/kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt b/kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt index 968b093c79..e31ed7aee7 100644 --- a/kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt +++ b/kotlinx-coroutines-test/common/test/TestCoroutineScopeTest.kt @@ -120,9 +120,11 @@ class TestCoroutineScopeTest { assertFalse(handlerCalled) } - private val invalidContexts = listOf( - Dispatchers.Default, // not a [TestDispatcher] - TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler - CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor` - ) + companion object { + internal val invalidContexts = listOf( + Dispatchers.Default, // not a [TestDispatcher] + TestCoroutineDispatcher() + TestCoroutineScheduler(), // the dispatcher is not linked to the scheduler + CoroutineExceptionHandler { _, _ -> }, // not an `UncaughtExceptionCaptor` + ) + } } diff --git a/kotlinx-coroutines-test/common/test/TestDispatchersTest.kt b/kotlinx-coroutines-test/common/test/TestDispatchersTest.kt index 77ee4f3a9f..3ec110ebcf 100644 --- a/kotlinx-coroutines-test/common/test/TestDispatchersTest.kt +++ b/kotlinx-coroutines-test/common/test/TestDispatchersTest.kt @@ -33,7 +33,7 @@ class TestDispatchersTest { } @Test - fun testImmediateDispatcher() = runBlockingTest { + fun testImmediateDispatcher() = runTest { Dispatchers.setMain(ImmediateDispatcher()) expect(1) withContext(Dispatchers.Main) { diff --git a/kotlinx-coroutines-test/jvm/test/Helpers.kt b/kotlinx-coroutines-test/jvm/test/HelpersJvm.kt similarity index 89% rename from kotlinx-coroutines-test/jvm/test/Helpers.kt rename to kotlinx-coroutines-test/jvm/test/HelpersJvm.kt index 017f2369bb..e9aa3ff747 100644 --- a/kotlinx-coroutines-test/jvm/test/Helpers.kt +++ b/kotlinx-coroutines-test/jvm/test/HelpersJvm.kt @@ -3,8 +3,6 @@ */ package kotlinx.coroutines.test -import kotlinx.coroutines.* - actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) { block { test() diff --git a/kotlinx-coroutines-test/native/test/Helpers.kt b/kotlinx-coroutines-test/native/test/Helpers.kt index 017f2369bb..e9aa3ff747 100644 --- a/kotlinx-coroutines-test/native/test/Helpers.kt +++ b/kotlinx-coroutines-test/native/test/Helpers.kt @@ -3,8 +3,6 @@ */ package kotlinx.coroutines.test -import kotlinx.coroutines.* - actual fun testResultMap(block: (() -> Unit) -> Unit, test: () -> TestResult) { block { test()