From 67661d99a747ecc38efd13cb46199c2a92b342b1 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Wed, 27 Oct 2021 10:29:25 +0300 Subject: [PATCH] Fixes --- .../common/src/DelayController.kt | 11 ++--------- kotlinx-coroutines-test/common/src/TestBuilders.kt | 2 +- .../common/src/TestCoroutineScheduler.kt | 7 ++++--- .../common/src/TestCoroutineScope.kt | 12 ++++++++---- kotlinx-coroutines-test/common/src/TestDispatcher.kt | 1 - 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/kotlinx-coroutines-test/common/src/DelayController.kt b/kotlinx-coroutines-test/common/src/DelayController.kt index b86955db31..5d8686da6b 100644 --- a/kotlinx-coroutines-test/common/src/DelayController.kt +++ b/kotlinx-coroutines-test/common/src/DelayController.kt @@ -84,11 +84,11 @@ public interface DelayController { /** * Call after test code completes to ensure that the dispatcher is properly cleaned up. * - * @throws UncompletedCoroutinesError if any pending tasks are active, however it will not throw for suspended + * @throws AssertionError if any pending tasks are active, however it will not throw for suspended * coroutines. */ @ExperimentalCoroutinesApi - @Throws(UncompletedCoroutinesError::class) + @Throws(AssertionError::class) public fun cleanupTestCoroutines() /** @@ -123,13 +123,6 @@ public interface DelayController { public fun resumeDispatcher() } -/** - * Thrown when a test has completed and there are tasks that are not completed or cancelled. - */ -// todo: maybe convert into non-public class in 1.3.0 (need use-cases for a public exception type) -@ExperimentalCoroutinesApi -public class UncompletedCoroutinesError(message: String): AssertionError(message) - internal interface SchedulerAsDelayController: DelayController { public val scheduler: TestCoroutineScheduler diff --git a/kotlinx-coroutines-test/common/src/TestBuilders.kt b/kotlinx-coroutines-test/common/src/TestBuilders.kt index ebe22050f1..f66f962be7 100644 --- a/kotlinx-coroutines-test/common/src/TestBuilders.kt +++ b/kotlinx-coroutines-test/common/src/TestBuilders.kt @@ -34,7 +34,7 @@ import kotlin.coroutines.* * * Unhandled exceptions thrown by coroutines in the test will be re-thrown at the end of the test. * - * @throws UncompletedCoroutinesError If the [testBody] does not complete (or cancel) all coroutines that it launches + * @throws AssertionError If the [testBody] does not complete (or cancel) all coroutines that it launches * (including coroutines suspended on join/await). * * @param context additional context elements. If [context] contains [CoroutineDispatcher] or [CoroutineExceptionHandler], diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt b/kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt index 0d0716999c..2f5e1fd216 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt @@ -24,10 +24,11 @@ import kotlin.jvm.* */ @ExperimentalCoroutinesApi // TODO: maybe make this a `TimeSource`? -public class TestCoroutineScheduler: AbstractCoroutineContextElement(TestCoroutineScheduler), CoroutineContext.Element { +public class TestCoroutineScheduler : AbstractCoroutineContextElement(TestCoroutineScheduler), + CoroutineContext.Element { /** @suppress */ - public companion object Key: CoroutineContext.Key + public companion object Key : CoroutineContext.Key /** This heap stores the knowledge about which dispatchers are interested in which moments of virtual time. */ // TODO: all the synchronization is done via a separate lock, so a non-thread-safe priority queue can be used. @@ -55,7 +56,7 @@ public class TestCoroutineScheduler: AbstractCoroutineContextElement(TestCorouti dispatcher: TestDispatcher, timeDeltaMillis: Long, marker: T, - isCancelled : (T) -> Boolean + isCancelled: (T) -> Boolean ): DisposableHandle { require(timeDeltaMillis >= 0) { "Attempted scheduling an event earlier in time (with the time delta $timeDeltaMillis)" } val count = count.getAndIncrement() diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt index fae427aa62..e4e92bd486 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineScope.kt @@ -11,13 +11,13 @@ import kotlin.coroutines.* * A scope which provides detailed control over the execution of coroutines for tests. */ @ExperimentalCoroutinesApi -public interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCaptor { +public sealed interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCaptor { /** * Call after the test completes. * Calls [UncaughtExceptionCaptor.cleanupTestCoroutinesCaptor] and [DelayController.cleanupTestCoroutines]. * * @throws Throwable the first uncaught exception, if there are any uncaught exceptions. - * @throws UncompletedCoroutinesError if any pending tasks are active, however it will not throw for suspended + * @throws AssertionError if any pending tasks are active, however it will not throw for suspended * coroutines. */ @ExperimentalCoroutinesApi @@ -28,8 +28,6 @@ public interface TestCoroutineScope: CoroutineScope, UncaughtExceptionCaptor { */ @ExperimentalCoroutinesApi public val testScheduler: TestCoroutineScheduler - get() = coroutineContext[TestCoroutineScheduler] - ?: throw UnsupportedOperationException("This scope does not have a TestCoroutineScheduler linked to it") } private class TestCoroutineScopeImpl ( @@ -158,3 +156,9 @@ public fun TestCoroutineScope.resumeDispatcher() { private val TestCoroutineScope.delayControllerForPausing: DelayController get() = coroutineContext.delayController ?: throw IllegalStateException("This scope isn't able to pause its dispatchers") + +/** + * Thrown when a test has completed and there are tasks that are not completed or cancelled. + */ +@ExperimentalCoroutinesApi +internal class UncompletedCoroutinesError(message: String): AssertionError(message) diff --git a/kotlinx-coroutines-test/common/src/TestDispatcher.kt b/kotlinx-coroutines-test/common/src/TestDispatcher.kt index 549eddc34f..b37f10bfda 100644 --- a/kotlinx-coroutines-test/common/src/TestDispatcher.kt +++ b/kotlinx-coroutines-test/common/src/TestDispatcher.kt @@ -18,7 +18,6 @@ public abstract class TestDispatcher: CoroutineDispatcher(), Delay { public abstract val scheduler: TestCoroutineScheduler /** Notifies the dispatcher that it should process a single event marked with [marker] happening at time [time]. */ - @ExperimentalCoroutinesApi internal abstract fun processEvent(time: Long, marker: Any) /** @suppress */