Skip to content

Commit

Permalink
Fix tests failing when DefaultExecutor and Dispatchers.Unconfined sha…
Browse files Browse the repository at this point in the history
…re the event loop
  • Loading branch information
dkhalanskyjb committed Apr 8, 2024
1 parent 908fad8 commit 61f8461
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion kotlinx-coroutines-core/common/test/flow/VirtualTime.kt
Expand Up @@ -22,7 +22,21 @@ internal class VirtualTimeDispatcher(enclosingScope: CoroutineScope) : Coroutine
val delayNanos = ThreadLocalEventLoop.currentOrNull()?.processNextEvent()
?: error("Event loop is missing, virtual time source works only as part of event loop")
if (delayNanos <= 0) continue
if (delayNanos > 0 && delayNanos != Long.MAX_VALUE) error("Unexpected external delay: $delayNanos")
if (delayNanos > 0 && delayNanos != Long.MAX_VALUE) {
if (usesSharedEventLoop) {
val targetTime = currentTime + delayNanos
while (currentTime < targetTime) {
val nextTask = heap.minByOrNull { it.deadline } ?: break
if (nextTask.deadline > targetTime) break
heap.remove(nextTask)
currentTime = nextTask.deadline
nextTask.run()
}
currentTime = maxOf(currentTime, targetTime)
} else {
error("Unexpected external delay: $delayNanos")
}
}
val nextTask = heap.minByOrNull { it.deadline } ?: return@launch
heap.remove(nextTask)
currentTime = nextTask.deadline
Expand Down
5 changes: 5 additions & 0 deletions test-utils/common/src/TestBase.common.kt
Expand Up @@ -288,3 +288,8 @@ public expect val isNative: Boolean
* and run such tests only on JVM and K/N.
*/
public expect val isBoundByJsTestTimeout: Boolean

/**
* `true` if this platform has the same event loop for `DefaultExecutor` and [Dispatchers.Unconfined]
*/
public expect val usesSharedEventLoop: Boolean
2 changes: 2 additions & 0 deletions test-utils/js/src/TestBase.kt
Expand Up @@ -95,3 +95,5 @@ actual val isNative = false
actual val isBoundByJsTestTimeout = true

actual val isJavaAndWindows: Boolean get() = false

actual val usesSharedEventLoop: Boolean = false
2 changes: 2 additions & 0 deletions test-utils/jvm/src/TestBase.kt
Expand Up @@ -186,3 +186,5 @@ actual val isBoundByJsTestTimeout = false
* which makes such tests flaky.
*/
actual val isJavaAndWindows: Boolean = System.getProperty("os.name")!!.contains("Windows")

actual val usesSharedEventLoop: Boolean = false
2 changes: 2 additions & 0 deletions test-utils/native/src/TestBase.kt
Expand Up @@ -61,3 +61,5 @@ public actual val isNative = true
public actual val isBoundByJsTestTimeout = false

public actual val isJavaAndWindows: Boolean get() = false

actual val usesSharedEventLoop: Boolean = false
2 changes: 2 additions & 0 deletions test-utils/wasmJs/src/TestBase.kt
Expand Up @@ -96,3 +96,5 @@ actual val isNative = false
actual val isBoundByJsTestTimeout = true

actual val isJavaAndWindows: Boolean get() = false

actual val usesSharedEventLoop: Boolean = false
2 changes: 2 additions & 0 deletions test-utils/wasmWasi/src/TestBase.kt
Expand Up @@ -66,3 +66,5 @@ actual val isNative = false
actual val isBoundByJsTestTimeout = true

actual val isJavaAndWindows: Boolean get() = false

actual val usesSharedEventLoop: Boolean = true

0 comments on commit 61f8461

Please sign in to comment.