11@file:Suppress(" EXPERIMENTAL_API_USAGE" , " DEPRECATION" )
2+ @file:OptIn(ExperimentalCoroutinesApi ::class )
23
34package com.squareup.workflow1.internal
45
@@ -28,12 +29,13 @@ import kotlinx.coroutines.CancellationException
2829import kotlinx.coroutines.CoroutineName
2930import kotlinx.coroutines.CoroutineScope
3031import kotlinx.coroutines.Dispatchers.Unconfined
32+ import kotlinx.coroutines.ExperimentalCoroutinesApi
3133import kotlinx.coroutines.Job
3234import kotlinx.coroutines.cancel
3335import kotlinx.coroutines.flow.MutableStateFlow
34- import kotlinx.coroutines.runBlocking
3536import kotlinx.coroutines.selects.select
3637import kotlinx.coroutines.suspendCancellableCoroutine
38+ import kotlinx.coroutines.test.runTest
3739import kotlinx.coroutines.withTimeout
3840import kotlin.coroutines.CoroutineContext
3941import kotlin.test.AfterTest
@@ -171,14 +173,14 @@ internal class WorkflowNodeTest {
171173 )
172174 node.render(workflow, " " )(" event" )
173175
174- val result = runBlocking {
175- withTimeout(10 ) {
176+ runTest {
177+ val result = withTimeout(10 ) {
176178 select<WorkflowOutput <String >? > {
177179 node.tick(this )
178180 }
179181 }
182+ assertEquals(" tick:event" , result?.value)
180183 }
181- assertEquals(" tick:event" , result?.value)
182184 }
183185
184186 @Test fun `accepts events sent to stale renderings` () {
@@ -208,16 +210,16 @@ internal class WorkflowNodeTest {
208210 sink(" event" )
209211 sink(" event2" )
210212
211- val result = runBlocking {
212- withTimeout(10 ) {
213+ runTest {
214+ val result = withTimeout(10 ) {
213215 List (2 ) {
214216 select<WorkflowOutput <String >? > {
215217 node.tick(this )
216218 }
217219 }
218220 }
221+ assertEquals(listOf (" tick:event" , " tick:event2" ), result.map { it?.value })
219222 }
220- assertEquals(listOf (" tick:event" , " tick:event2" ), result.map { it?.value })
221223 }
222224
223225 @Test fun `send allows subsequent events on same rendering` () {
@@ -262,7 +264,7 @@ internal class WorkflowNodeTest {
262264 snapshot = null , baseContext = context
263265 )
264266
265- runBlocking {
267+ runTest {
266268 node.render(workflow.asStatefulWorkflow(), Unit )
267269 assertTrue(started)
268270 }
@@ -300,16 +302,15 @@ internal class WorkflowNodeTest {
300302 )
301303 node.render(workflow.asStatefulWorkflow(), Unit )
302304
303- val result = runBlocking {
305+ runTest {
304306 // Result should be available instantly, any delay at all indicates something is broken.
305- withTimeout(1 ) {
307+ val result = withTimeout(1 ) {
306308 select<WorkflowOutput <String >? > {
307309 node.tick(this )
308310 }
309311 }
312+ assertEquals(" result" , result?.value)
310313 }
311-
312- assertEquals(" result" , result?.value)
313314 }
314315
315316 @Test fun `sideEffect is cancelled when stops being ran` () {
@@ -329,7 +330,7 @@ internal class WorkflowNodeTest {
329330 snapshot = null , baseContext = context
330331 )
331332
332- runBlocking {
333+ runTest {
333334 node.render(workflow.asStatefulWorkflow(), true )
334335 assertNull(cancellationException)
335336
@@ -355,7 +356,7 @@ internal class WorkflowNodeTest {
355356 snapshot = null , baseContext = context
356357 )
357358
358- runBlocking {
359+ runTest {
359360 node.render(workflow.asStatefulWorkflow(), Unit )
360361 assertNull(cancellationException)
361362
@@ -381,7 +382,7 @@ internal class WorkflowNodeTest {
381382 snapshot = null , baseContext = context
382383 )
383384
384- runBlocking {
385+ runTest {
385386 node.render(workflow.asStatefulWorkflow(), 0 )
386387 assertFalse(cancelled)
387388 assertEquals(1 , renderPasses)
@@ -406,7 +407,7 @@ internal class WorkflowNodeTest {
406407 snapshot = null , baseContext = context
407408 )
408409
409- runBlocking {
410+ runTest {
410411 node.render(workflow.asStatefulWorkflow(), 0 )
411412 assertEquals(listOf (0 ), seenProps)
412413 assertEquals(1 , renderPasses)
@@ -1097,7 +1098,7 @@ internal class WorkflowNodeTest {
10971098
10981099 sink.send(" hello" )
10991100
1100- runBlocking {
1101+ runTest {
11011102 select<WorkflowOutput <String >? > {
11021103 node.tick(this )
11031104 }
@@ -1123,13 +1124,12 @@ internal class WorkflowNodeTest {
11231124
11241125 rendering.send(" hello" )
11251126
1126- val output = runBlocking {
1127- select<WorkflowOutput <String >? > {
1127+ runTest {
1128+ val output = select<WorkflowOutput <String >? > {
11281129 node.tick(this )
11291130 }
1131+ assertEquals(" output:hello" , output?.value)
11301132 }
1131-
1132- assertEquals(" output:hello" , output?.value)
11331133 }
11341134
11351135 @Test fun `actionSink action allows null output` () {
@@ -1148,13 +1148,12 @@ internal class WorkflowNodeTest {
11481148
11491149 rendering.send(" hello" )
11501150
1151- val output = runBlocking {
1152- select<WorkflowOutput <String >? > {
1151+ runTest {
1152+ val output = select<WorkflowOutput <String >? > {
11531153 node.tick(this )
11541154 }
1155+ assertNull(output?.value)
11551156 }
1156-
1157- assertNull(output?.value)
11581157 }
11591158
11601159 @Test fun `child action changes state` () {
@@ -1176,7 +1175,7 @@ internal class WorkflowNodeTest {
11761175 )
11771176 node.render(workflow.asStatefulWorkflow(), Unit )
11781177
1179- runBlocking {
1178+ runTest {
11801179 select<WorkflowOutput <String >? > {
11811180 node.tick(this )
11821181 }
@@ -1202,13 +1201,12 @@ internal class WorkflowNodeTest {
12021201 )
12031202 node.render(workflow.asStatefulWorkflow(), Unit )
12041203
1205- val output = runBlocking {
1206- select<WorkflowOutput <String >? > {
1204+ runTest {
1205+ val output = select<WorkflowOutput <String >? > {
12071206 node.tick(this )
12081207 }
1208+ assertEquals(" output:child:hello" , output?.value)
12091209 }
1210-
1211- assertEquals(" output:child:hello" , output?.value)
12121210 }
12131211
12141212 @Test fun `child action allows null output` () {
@@ -1227,13 +1225,12 @@ internal class WorkflowNodeTest {
12271225 )
12281226 node.render(workflow.asStatefulWorkflow(), Unit )
12291227
1230- val output = runBlocking {
1231- select<WorkflowOutput <String >? > {
1228+ runTest {
1229+ val output = select<WorkflowOutput <String >? > {
12321230 node.tick(this )
12331231 }
1232+ assertNull(output?.value)
12341233 }
1235-
1236- assertNull(output?.value)
12371234 }
12381235
12391236 private class TestSession (override val sessionId : Long = 0 ) : WorkflowSession {
0 commit comments