Skip to content

Commit

Permalink
Fix K/N EvenLoop.reschedule time conversion (#4245)
Browse files Browse the repository at this point in the history
A "point of time" value was wrongly passed to a function that expects a "duration of time".
Additionally, the former was in nanos, while the latter is in milllis.
  • Loading branch information
qurbonzoda authored Oct 17, 2024
1 parent 9749b8c commit 1fcffbf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions kotlinx-coroutines-core/concurrent/test/RunBlockingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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()
}
}
}
}
3 changes: 2 additions & 1 deletion kotlinx-coroutines-core/native/src/EventLoop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 1fcffbf

Please sign in to comment.