diff --git a/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerTest.kt b/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerTest.kt index 3433783b9..25581e0b8 100644 --- a/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerTest.kt +++ b/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerTest.kt @@ -1,5 +1,6 @@ package com.squareup.workflow1 +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.conflate @@ -7,13 +8,14 @@ import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotSame import kotlin.test.assertTrue +@ExperimentalCoroutinesApi @OptIn(ExperimentalStdlibApi::class) class WorkerTest { @@ -62,14 +64,11 @@ class WorkerTest { assertFalse(transformed1.doesSameWorkAs(transformed2)) } - @Test fun transformed_workers_transform_flows() { + @Test fun transformed_workers_transform_flows() = runTest { val source = flowOf(1, 2, 3).asWorker() val transformed = source.transform { flow -> flow.map { it.toString() } } - val transformedValues = runBlocking { - transformed.run() - .toList() - } + val transformedValues = transformed.run().toList() assertEquals(listOf("1", "2", "3"), transformedValues) } diff --git a/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerWorkflowTest.kt b/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerWorkflowTest.kt index c42a60644..ff548a2e1 100644 --- a/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerWorkflowTest.kt +++ b/workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkerWorkflowTest.kt @@ -1,29 +1,29 @@ package com.squareup.workflow1 import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest import kotlin.coroutines.coroutineContext import kotlin.test.Test import kotlin.test.assertEquals +@ExperimentalCoroutinesApi class WorkerWorkflowTest { - @Test fun runWorker_coroutine_is_named_without_key() { + @Test fun runWorker_coroutine_is_named_without_key() = runTest { val worker = CoroutineNameWorker() - runBlocking { - runWorker(worker, renderKey = "", actionSink = NoopSink) - } + + runWorker(worker, renderKey = "", actionSink = NoopSink) assertEquals("CoroutineNameWorker.toString", worker.recordedName) } - @Test fun runWorker_coroutine_is_named_with_key() { + @Test fun runWorker_coroutine_is_named_with_key() = runTest { val worker = CoroutineNameWorker() - runBlocking { - runWorker(worker, renderKey = "foo", actionSink = NoopSink) - } + + runWorker(worker, renderKey = "foo", actionSink = NoopSink) assertEquals("CoroutineNameWorker.toString:foo", worker.recordedName) } diff --git a/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/WorkflowInterceptorTest.kt b/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/WorkflowInterceptorTest.kt index 874fd3b0b..0ab204617 100644 --- a/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/WorkflowInterceptorTest.kt +++ b/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/WorkflowInterceptorTest.kt @@ -7,8 +7,9 @@ import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext.Key import kotlin.test.Test @@ -122,7 +123,7 @@ internal class WorkflowInterceptorTest { ) } - @Test fun intercept_intercepts_side_effects() { + @Test fun intercept_intercepts_side_effects() = runTest { val recorder = RecordingWorkflowInterceptor() val workflow = TestSideEffectWorkflow() val intercepted = recorder.intercept(workflow, workflow.session) @@ -140,7 +141,8 @@ internal class WorkflowInterceptorTest { key: String, sideEffect: suspend CoroutineScope.() -> Unit ) { - runBlocking { sideEffect() } + launch { sideEffect() } + advanceUntilIdle() } } @@ -157,7 +159,7 @@ internal class WorkflowInterceptorTest { ) } - @Test fun intercept_uses_interceptors_context_for_side_effect() { + @Test fun intercept_uses_interceptors_context_for_side_effect() = runTest { val recorder = object : RecordingWorkflowInterceptor() { override fun
onRender(
renderProps: P,
@@ -200,7 +202,8 @@ internal class WorkflowInterceptorTest {
key: String,
sideEffect: suspend CoroutineScope.() -> Unit
) {
- runBlocking { sideEffect() }
+ launch { sideEffect() }
+ advanceUntilIdle()
}
}
diff --git a/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/ChainedWorkflowInterceptorTest.kt b/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/ChainedWorkflowInterceptorTest.kt
index 772a6abc9..441687de1 100644
--- a/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/ChainedWorkflowInterceptorTest.kt
+++ b/workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/ChainedWorkflowInterceptorTest.kt
@@ -18,6 +18,8 @@ import com.squareup.workflow1.rendering
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -55,7 +57,7 @@ internal class ChainedWorkflowInterceptorTest {
@OptIn(ExperimentalCoroutinesApi::class)
@Test
- fun chains_calls_to_onInstanceStarted_in_left_to_right_order() {
+ fun chains_calls_to_onInstanceStarted_in_left_to_right_order() = runTest {
val events = mutableListOf