Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkhalanskyjb committed Oct 27, 2021
1 parent 2f818a3 commit 67661d9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
11 changes: 2 additions & 9 deletions kotlinx-coroutines-test/common/src/DelayController.kt
Expand Up @@ -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()

/**
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-test/common/src/TestBuilders.kt
Expand Up @@ -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],
Expand Down
7 changes: 4 additions & 3 deletions kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt
Expand Up @@ -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<TestCoroutineScheduler>
public companion object Key : CoroutineContext.Key<TestCoroutineScheduler>

/** 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.
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 8 additions & 4 deletions kotlinx-coroutines-test/common/src/TestCoroutineScope.kt
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
1 change: 0 additions & 1 deletion kotlinx-coroutines-test/common/src/TestDispatcher.kt
Expand Up @@ -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 */
Expand Down

0 comments on commit 67661d9

Please sign in to comment.