11@file:Suppress(" EXPERIMENTAL_API_USAGE" , " DEPRECATION" )
2+ @file:OptIn(ExperimentalCoroutinesApi ::class )
23
34package com.squareup.workflow1.internal
45
@@ -29,12 +30,13 @@ import kotlinx.coroutines.CancellationException
2930import kotlinx.coroutines.CoroutineName
3031import kotlinx.coroutines.CoroutineScope
3132import kotlinx.coroutines.Dispatchers.Unconfined
33+ import kotlinx.coroutines.ExperimentalCoroutinesApi
3234import kotlinx.coroutines.Job
3335import kotlinx.coroutines.cancel
3436import kotlinx.coroutines.flow.MutableStateFlow
35- import kotlinx.coroutines.runBlocking
3637import kotlinx.coroutines.selects.select
3738import kotlinx.coroutines.suspendCancellableCoroutine
39+ import kotlinx.coroutines.test.runTest
3840import kotlinx.coroutines.withTimeout
3941import kotlin.coroutines.CoroutineContext
4042import kotlin.test.AfterTest
@@ -173,14 +175,14 @@ internal class WorkflowNodeTest {
173175 )
174176 node.render(workflow, " " )(" event" )
175177
176- val result = runBlocking {
177- withTimeout(10 ) {
178+ runTest {
179+ val result = withTimeout(10 ) {
178180 select<ActionProcessingResult ?> {
179181 node.tick(this )
180182 } as WorkflowOutput <String >?
181183 }
184+ assertEquals(" tick:event" , result?.value)
182185 }
183- assertEquals(" tick:event" , result?.value)
184186 }
185187
186188 @Test fun `accepts events sent to stale renderings` () {
@@ -210,16 +212,16 @@ internal class WorkflowNodeTest {
210212 sink(" event" )
211213 sink(" event2" )
212214
213- val result = runBlocking {
214- withTimeout(10 ) {
215+ runTest {
216+ val result = withTimeout(10 ) {
215217 List (2 ) {
216218 select<ActionProcessingResult ?> {
217219 node.tick(this )
218220 } as WorkflowOutput <String >?
219221 }
220222 }
223+ assertEquals(listOf (" tick:event" , " tick:event2" ), result.map { it?.value })
221224 }
222- assertEquals(listOf (" tick:event" , " tick:event2" ), result.map { it?.value })
223225 }
224226
225227 @Test fun `send allows subsequent events on same rendering` () {
@@ -264,7 +266,7 @@ internal class WorkflowNodeTest {
264266 snapshot = null , baseContext = context
265267 )
266268
267- runBlocking {
269+ runTest {
268270 node.render(workflow.asStatefulWorkflow(), Unit )
269271 assertTrue(started)
270272 }
@@ -302,16 +304,15 @@ internal class WorkflowNodeTest {
302304 )
303305 node.render(workflow.asStatefulWorkflow(), Unit )
304306
305- val result = runBlocking {
307+ runTest {
306308 // Result should be available instantly, any delay at all indicates something is broken.
307- withTimeout(1 ) {
309+ val result = withTimeout(1 ) {
308310 select<ActionProcessingResult ?> {
309311 node.tick(this )
310312 } as WorkflowOutput <String >?
311313 }
314+ assertEquals(" result" , result?.value)
312315 }
313-
314- assertEquals(" result" , result?.value)
315316 }
316317
317318 @Test fun `sideEffect is cancelled when stops being ran` () {
@@ -331,7 +332,7 @@ internal class WorkflowNodeTest {
331332 snapshot = null , baseContext = context
332333 )
333334
334- runBlocking {
335+ runTest {
335336 node.render(workflow.asStatefulWorkflow(), true )
336337 assertNull(cancellationException)
337338
@@ -357,7 +358,7 @@ internal class WorkflowNodeTest {
357358 snapshot = null , baseContext = context
358359 )
359360
360- runBlocking {
361+ runTest {
361362 node.render(workflow.asStatefulWorkflow(), Unit )
362363 assertNull(cancellationException)
363364
@@ -383,7 +384,7 @@ internal class WorkflowNodeTest {
383384 snapshot = null , baseContext = context
384385 )
385386
386- runBlocking {
387+ runTest {
387388 node.render(workflow.asStatefulWorkflow(), 0 )
388389 assertFalse(cancelled)
389390 assertEquals(1 , renderPasses)
@@ -408,7 +409,7 @@ internal class WorkflowNodeTest {
408409 snapshot = null , baseContext = context
409410 )
410411
411- runBlocking {
412+ runTest {
412413 node.render(workflow.asStatefulWorkflow(), 0 )
413414 assertEquals(listOf (0 ), seenProps)
414415 assertEquals(1 , renderPasses)
@@ -1099,7 +1100,7 @@ internal class WorkflowNodeTest {
10991100
11001101 sink.send(" hello" )
11011102
1102- runBlocking {
1103+ runTest {
11031104 select<ActionProcessingResult ?> {
11041105 node.tick(this )
11051106 } as WorkflowOutput <String >?
@@ -1125,13 +1126,12 @@ internal class WorkflowNodeTest {
11251126
11261127 rendering.send(" hello" )
11271128
1128- val output = runBlocking {
1129- select<ActionProcessingResult ?> {
1129+ runTest {
1130+ val output = select<ActionProcessingResult ?> {
11301131 node.tick(this )
11311132 } as WorkflowOutput <String >?
1133+ assertEquals(" output:hello" , output?.value)
11321134 }
1133-
1134- assertEquals(" output:hello" , output?.value)
11351135 }
11361136
11371137 @Test fun `actionSink action allows null output` () {
@@ -1150,13 +1150,12 @@ internal class WorkflowNodeTest {
11501150
11511151 rendering.send(" hello" )
11521152
1153- val output = runBlocking {
1154- select<ActionProcessingResult ?> {
1153+ runTest {
1154+ val output = select<ActionProcessingResult ?> {
11551155 node.tick(this )
11561156 } as WorkflowOutput <String >?
1157+ assertNull(output?.value)
11571158 }
1158-
1159- assertNull(output?.value)
11601159 }
11611160
11621161 @Test fun `child action changes state` () {
@@ -1178,7 +1177,7 @@ internal class WorkflowNodeTest {
11781177 )
11791178 node.render(workflow.asStatefulWorkflow(), Unit )
11801179
1181- runBlocking {
1180+ runTest {
11821181 select<ActionProcessingResult ?> {
11831182 node.tick(this )
11841183 } as WorkflowOutput <String >?
@@ -1204,13 +1203,12 @@ internal class WorkflowNodeTest {
12041203 )
12051204 node.render(workflow.asStatefulWorkflow(), Unit )
12061205
1207- val output = runBlocking {
1208- select<ActionProcessingResult ?> {
1206+ runTest {
1207+ val output = select<ActionProcessingResult ?> {
12091208 node.tick(this )
12101209 } as WorkflowOutput <String >?
1210+ assertEquals(" output:child:hello" , output?.value)
12111211 }
1212-
1213- assertEquals(" output:child:hello" , output?.value)
12141212 }
12151213
12161214 @Test fun `child action allows null output` () {
@@ -1229,13 +1227,12 @@ internal class WorkflowNodeTest {
12291227 )
12301228 node.render(workflow.asStatefulWorkflow(), Unit )
12311229
1232- val output = runBlocking {
1233- select<ActionProcessingResult ?> {
1230+ runTest {
1231+ val output = select<ActionProcessingResult ?> {
12341232 node.tick(this )
12351233 } as WorkflowOutput <String >?
1234+ assertNull(output?.value)
12361235 }
1237-
1238- assertNull(output?.value)
12391236 }
12401237
12411238 private class TestSession (override val sessionId : Long = 0 ) : WorkflowSession {
0 commit comments