Skip to content

Commit 268daed

Browse files
committed
Introduce test helper method
1 parent d987d7b commit 268daed

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

workflow-runtime/src/commonTest/kotlin/com/squareup/workflow1/internal/WorkflowRunnerTest.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.squareup.workflow1.internal
33
import com.squareup.workflow1.NoopWorkflowInterceptor
44
import com.squareup.workflow1.Worker
55
import com.squareup.workflow1.Workflow
6+
import com.squareup.workflow1.WorkflowOutput
67
import com.squareup.workflow1.action
78
import com.squareup.workflow1.runningWorker
89
import com.squareup.workflow1.stateful
@@ -52,10 +53,11 @@ internal class WorkflowRunnerTest {
5253
val props = MutableStateFlow("initial")
5354
val runner = WorkflowRunner(workflow, props)
5455
runner.nextRendering()
55-
val output = scope.async { runner.nextOutput() }
56+
57+
val outputDeferred = scope.async { runner.nextOutput() }
5658

5759
scope.runCurrent()
58-
assertTrue(output.isActive)
60+
assertTrue(outputDeferred.isActive)
5961
}
6062

6163
@Test fun `initial nextOutput() handles props changed after initialization`() {
@@ -100,10 +102,7 @@ internal class WorkflowRunnerTest {
100102
val initialRendering = runner.nextRendering().rendering
101103
assertEquals("initial", initialRendering)
102104

103-
val output = with(scope.async { runner.nextOutput() }) {
104-
scope.advanceUntilIdle()
105-
getCompleted()
106-
}
105+
val output = runner.runTillNextOutput()
107106
assertEquals("output: work", output?.value)
108107

109108
val updatedRendering = runner.nextRendering().rendering
@@ -131,19 +130,13 @@ internal class WorkflowRunnerTest {
131130

132131
// The order in which props update and workflow update are processed is deterministic, based
133132
// on the order they appear in the select block in nextOutput.
134-
val firstOutput = with(scope.async { runner.nextOutput() }) {
135-
scope.advanceUntilIdle()
136-
getCompleted()
137-
}
133+
val firstOutput = runner.runTillNextOutput()
138134
// First update will be props, so no output value.
139135
assertNull(firstOutput)
140136
val secondRendering = runner.nextRendering().rendering
141137
assertEquals("changed props|initial state(initial props)", secondRendering)
142138

143-
val secondOutput = with(scope.async { runner.nextOutput() }) {
144-
scope.advanceUntilIdle()
145-
getCompleted()
146-
}
139+
val secondOutput = runner.runTillNextOutput()
147140
assertEquals("output: work", secondOutput?.value)
148141
val thirdRendering = runner.nextRendering().rendering
149142
assertEquals("changed props|state: work", thirdRendering)
@@ -227,6 +220,13 @@ internal class WorkflowRunnerTest {
227220
assertEquals("foo", cancellationException!!.message)
228221
}
229222

223+
224+
private fun <T> WorkflowRunner<*, T, *>.runTillNextOutput(): WorkflowOutput<T>? = scope.run {
225+
val firstOutputDeferred = async { nextOutput() }
226+
runCurrent()
227+
firstOutputDeferred.getCompleted()
228+
}
229+
230230
@Suppress("TestFunctionName")
231231
private fun <P, O : Any, R> WorkflowRunner(
232232
workflow: Workflow<P, O, R>,

0 commit comments

Comments
 (0)