From 523db20e4a0e47d4a78b414a55811010fdb8d005 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 22 Jul 2024 11:45:50 +0300 Subject: [PATCH] Fix flaky coroutines tests --- .../kscience/dataforge/data/ActionsTest.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt b/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt index d608cbd3..ee4b05be 100644 --- a/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt +++ b/dataforge-data/src/jvmTest/kotlin/space/kscience/dataforge/data/ActionsTest.kt @@ -1,6 +1,11 @@ package space.kscience.dataforge.data +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext import space.kscience.dataforge.actions.Action import space.kscience.dataforge.actions.invoke import space.kscience.dataforge.actions.mapping @@ -9,7 +14,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.time.Duration.Companion.milliseconds -@OptIn(DFExperimental::class) +@OptIn(DFExperimental::class, ExperimentalCoroutinesApi::class) internal class ActionsTest { @Test fun testStaticMapAction() = runTest(timeout = 500.milliseconds) { @@ -24,6 +29,8 @@ internal class ActionsTest { } val result = plusOne(data) + + advanceUntilIdle() assertEquals(2, result["1"]?.await()) } @@ -38,8 +45,12 @@ internal class ActionsTest { val result = plusOne(source) - repeat(10) { - source.updateValue(it.toString(), it) + withContext(Dispatchers.Default) { + repeat(10) { + source.updateValue(it.toString(), it) + } + + delay(50) } // result.updates.take(10).onEach { println(it.name) }.collect()