diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api index 546bfe3507..7f99a31afd 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api @@ -545,6 +545,15 @@ public final class kotlinx/coroutines/TimeoutKt { public static final fun withTimeoutOrNull-KLykuaI (JLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } +public final class kotlinx/coroutines/YieldContext : kotlin/coroutines/AbstractCoroutineContextElement { + public static final field Key Lkotlinx/coroutines/YieldContext$Key; + public field dispatcherWasUnconfined Z + public fun ()V +} + +public final class kotlinx/coroutines/YieldContext$Key : kotlin/coroutines/CoroutineContext$Key { +} + public final class kotlinx/coroutines/YieldKt { public static final fun yield (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } diff --git a/kotlinx-coroutines-core/common/src/Unconfined.kt b/kotlinx-coroutines-core/common/src/Unconfined.kt index 24a401f702..5837ae83f3 100644 --- a/kotlinx-coroutines-core/common/src/Unconfined.kt +++ b/kotlinx-coroutines-core/common/src/Unconfined.kt @@ -38,6 +38,7 @@ internal object Unconfined : CoroutineDispatcher() { /** * Used to detect calls to [Unconfined.dispatch] from [yield] function. */ +@PublishedApi internal class YieldContext : AbstractCoroutineContextElement(Key) { companion object Key : CoroutineContext.Key diff --git a/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api b/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api index 15d894e18d..f024f2105f 100644 --- a/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api +++ b/kotlinx-coroutines-test/api/kotlinx-coroutines-test.api @@ -9,15 +9,6 @@ public abstract interface class kotlinx/coroutines/test/DelayController { public abstract fun runCurrent ()V } -public final class kotlinx/coroutines/test/StandardTestDispatcher : kotlinx/coroutines/test/TestDispatcher, kotlinx/coroutines/Delay { - public fun ()V - public fun (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;)V - public synthetic fun (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V - public fun getScheduler ()Lkotlinx/coroutines/test/TestCoroutineScheduler; - public fun toString ()Ljava/lang/String; -} - public final class kotlinx/coroutines/test/TestBuildersKt { public static final fun runBlockingTest (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;)V public static final fun runBlockingTest (Lkotlinx/coroutines/test/TestCoroutineDispatcher;Lkotlin/jvm/functions/Function2;)V @@ -49,6 +40,13 @@ public final class kotlinx/coroutines/test/TestCoroutineDispatcher : kotlinx/cor public fun toString ()Ljava/lang/String; } +public final class kotlinx/coroutines/test/TestCoroutineDispatchersKt { + public static final fun StandardTestDispatcher (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;)Lkotlinx/coroutines/test/TestDispatcher; + public static synthetic fun StandardTestDispatcher$default (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;ILjava/lang/Object;)Lkotlinx/coroutines/test/TestDispatcher; + public static final fun UnconfinedTestDispatcher (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;)Lkotlinx/coroutines/test/TestDispatcher; + public static synthetic fun UnconfinedTestDispatcher$default (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;ILjava/lang/Object;)Lkotlinx/coroutines/test/TestDispatcher; +} + public final class kotlinx/coroutines/test/TestCoroutineExceptionHandler : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/CoroutineExceptionHandler, kotlinx/coroutines/test/UncaughtExceptionCaptor { public fun ()V public fun cleanupTestCoroutinesCaptor ()V @@ -88,7 +86,6 @@ public final class kotlinx/coroutines/test/TestCoroutineScopeKt { } public abstract class kotlinx/coroutines/test/TestDispatcher : kotlinx/coroutines/CoroutineDispatcher, kotlinx/coroutines/Delay { - public fun ()V public fun delay (JLkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun getScheduler ()Lkotlinx/coroutines/test/TestCoroutineScheduler; public fun invokeOnTimeout (JLjava/lang/Runnable;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/DisposableHandle; @@ -105,13 +102,3 @@ public abstract interface class kotlinx/coroutines/test/UncaughtExceptionCaptor public abstract fun getUncaughtExceptions ()Ljava/util/List; } -public final class kotlinx/coroutines/test/UnconfinedTestDispatcher : kotlinx/coroutines/test/TestDispatcher, kotlinx/coroutines/Delay { - public fun ()V - public fun (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;)V - public synthetic fun (Lkotlinx/coroutines/test/TestCoroutineScheduler;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun dispatch (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Runnable;)V - public fun getScheduler ()Lkotlinx/coroutines/test/TestCoroutineScheduler; - public fun isDispatchNeeded (Lkotlin/coroutines/CoroutineContext;)Z - public fun toString ()Ljava/lang/String; -} - diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineDispatcher.kt b/kotlinx-coroutines-test/common/src/TestCoroutineDispatcher.kt index 32fe1829d3..31249ee6e4 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineDispatcher.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineDispatcher.kt @@ -36,12 +36,6 @@ public class TestCoroutineDispatcher(public override val scheduler: TestCoroutin } } - /** @suppress */ - override fun processEvent(time: Long, marker: Any) { - check(marker is Runnable) - marker.run() - } - /** @suppress */ override fun dispatch(context: CoroutineContext, block: Runnable) { checkSchedulerInContext(scheduler, context) diff --git a/kotlinx-coroutines-test/common/src/TestCoroutineDispatchers.kt b/kotlinx-coroutines-test/common/src/TestCoroutineDispatchers.kt index 7f3faf0211..bd04c046e7 100644 --- a/kotlinx-coroutines-test/common/src/TestCoroutineDispatchers.kt +++ b/kotlinx-coroutines-test/common/src/TestCoroutineDispatchers.kt @@ -29,21 +29,19 @@ import kotlin.coroutines.* * scheduler in order for the tasks to run. */ @ExperimentalCoroutinesApi -public class UnconfinedTestDispatcher( - public override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler(), - private val name: String? = null -): TestDispatcher(), Delay { +@Suppress("FunctionName") +public fun UnconfinedTestDispatcher( + scheduler: TestCoroutineScheduler = TestCoroutineScheduler(), + name: String? = null +): TestDispatcher = UnconfinedTestDispatcherImpl(scheduler, name) - /** @suppress */ - override fun processEvent(time: Long, marker: Any) { - check(marker is Runnable) - marker.run() - } +private class UnconfinedTestDispatcherImpl( + override val scheduler: TestCoroutineScheduler, + private val name: String? = null +): TestDispatcher() { - /** @suppress */ override fun isDispatchNeeded(context: CoroutineContext): Boolean = false - /** @suppress */ @Suppress("INVISIBLE_MEMBER") override fun dispatch(context: CoroutineContext, block: Runnable) { checkSchedulerInContext(scheduler, context) @@ -64,9 +62,7 @@ public class UnconfinedTestDispatcher( ) } - /** @suppress */ override fun toString(): String = "${name ?: "UnconfinedTestDispatcher"}[scheduler=$scheduler]" - } /** @@ -88,26 +84,22 @@ public class UnconfinedTestDispatcher( * * @see UnconfinedTestDispatcher for a dispatcher that is not confined to any particular thread. */ +@Suppress("FunctionName") +public fun StandardTestDispatcher( + scheduler: TestCoroutineScheduler = TestCoroutineScheduler(), + name: String? = null +): TestDispatcher = StandardTestDispatcherImpl(scheduler, name) + @ExperimentalCoroutinesApi -public class StandardTestDispatcher( - public override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler(), +private class StandardTestDispatcherImpl( + override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler(), private val name: String? = null -): TestDispatcher(), Delay { +): TestDispatcher() { - /** @suppress */ - override fun processEvent(time: Long, marker: Any) { - check(marker is Runnable) - marker.run() - } - - /** @suppress */ - @Suppress("INVISIBLE_MEMBER") override fun dispatch(context: CoroutineContext, block: Runnable) { checkSchedulerInContext(scheduler, context) scheduler.registerEvent(this, 0, block) { false } } - /** @suppress */ - override fun toString(): String = "${name ?: "ConfinedTestDispatcher"}[scheduler=$scheduler]" - + override fun toString(): String = "${name ?: "StandardTestDispatcher"}[scheduler=$scheduler]" } diff --git a/kotlinx-coroutines-test/common/src/TestDispatcher.kt b/kotlinx-coroutines-test/common/src/TestDispatcher.kt index b37f10bfda..2ad34d90bd 100644 --- a/kotlinx-coroutines-test/common/src/TestDispatcher.kt +++ b/kotlinx-coroutines-test/common/src/TestDispatcher.kt @@ -12,13 +12,16 @@ import kotlin.jvm.* * A test dispatcher that can interface with a [TestCoroutineScheduler]. */ @ExperimentalCoroutinesApi -public abstract class TestDispatcher: CoroutineDispatcher(), Delay { +public sealed class TestDispatcher: CoroutineDispatcher(), Delay { /** The scheduler that this dispatcher is linked to. */ @ExperimentalCoroutinesApi public abstract val scheduler: TestCoroutineScheduler /** Notifies the dispatcher that it should process a single event marked with [marker] happening at time [time]. */ - internal abstract fun processEvent(time: Long, marker: Any) + internal open fun processEvent(time: Long, marker: Any) { + check(marker is Runnable) + marker.run() + } /** @suppress */ override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation) {