diff --git a/kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt b/kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt index f04b491c6d..43f7976ffa 100644 --- a/kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt +++ b/kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt @@ -4,6 +4,8 @@ import kotlinx.coroutines.testing.* import kotlinx.coroutines.exceptions.* import kotlin.coroutines.* import kotlin.test.* +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds class RunBlockingTest : TestBase() { @@ -176,4 +178,20 @@ class RunBlockingTest : TestBase() { } } } + + /** Tests that the delayed tasks scheduled on a closed `runBlocking` event loop get processed in reasonable time. */ + @Test + fun testReschedulingDelayedTasks() { + val job = runBlocking { + val dispatcher = coroutineContext[ContinuationInterceptor]!! + GlobalScope.launch(dispatcher) { + delay(1.milliseconds) + } + } + runBlocking { + withTimeout(10.seconds) { + job.join() + } + } + } } diff --git a/kotlinx-coroutines-core/native/src/EventLoop.kt b/kotlinx-coroutines-core/native/src/EventLoop.kt index 4265f6094f..58128d52fd 100644 --- a/kotlinx-coroutines-core/native/src/EventLoop.kt +++ b/kotlinx-coroutines-core/native/src/EventLoop.kt @@ -15,7 +15,8 @@ internal actual abstract class EventLoopImplPlatform : EventLoop() { } protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) { - DefaultExecutor.invokeOnTimeout(now, delayedTask, EmptyCoroutineContext) + val delayTimeMillis = delayNanosToMillis(delayedTask.nanoTime - now) + DefaultExecutor.invokeOnTimeout(delayTimeMillis, delayedTask, EmptyCoroutineContext) } }