Skip to content

Commit cdf7847

Browse files
Merge pull request #1514 from square/codex/render-for-test-teardown-default
Make renderForTest teardown waiting opt-in
2 parents 2bf7d44 + 78af35f commit cdf7847

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

workflow-testing/src/main/java/com/squareup/workflow1/testing/WorkflowRuntimeTeardown.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import kotlin.time.Duration.Companion.seconds
99
public sealed interface WorkflowRuntimeTeardown {
1010
/**
1111
* Cancel the workflow runtime and return without waiting for cancellation cleanup to complete.
12+
* This is the default `renderForTest` behavior.
1213
*/
1314
public data object Cancel : WorkflowRuntimeTeardown
1415

1516
/**
16-
* Cancel the workflow runtime and wait for cancellation cleanup to complete.
17+
* Cancel the workflow runtime and wait for cancellation cleanup to complete. Pass this to
18+
* `renderForTest` when a test needs to observe effects that happen during runtime cancellation.
1719
*
1820
* @param timeout The maximum time to wait for the runtime job to complete after cancellation.
1921
* @param drainSchedulerAfterCancel Whether to drain the test scheduler after cancelling the

workflow-testing/src/main/java/com/squareup/workflow1/testing/WorkflowTurbine.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ import kotlin.time.Duration.Companion.milliseconds
4747
* A [testTimeout] may be specified to override the default [WORKFLOW_TEST_DEFAULT_TIMEOUT_MS] for
4848
* any particular test. This is the max amount of time the test could spend waiting on a rendering.
4949
*
50-
* [WorkflowRuntimeTeardown.CancelAndAwait] means `renderForTest` will not return until the workflow
51-
* runtime has been cancelled and cancellation cleanup has either completed or timed out. This is the
52-
* default so callers can run external teardown checks after this function returns.
50+
* By default, `renderForTest` cancels the workflow runtime and returns without waiting for
51+
* cancellation cleanup. Pass [WorkflowRuntimeTeardown.CancelAndAwait] to wait until cancellation
52+
* cleanup has either completed or timed out.
5353
*
5454
* This will start the Workflow runtime (with params as passed) rooted at whatever Workflow
5555
* it is called on and then create a [WorkflowTurbine] for its renderings and run [testCase] on that.
@@ -63,7 +63,7 @@ public fun <PropsT, StateT, OutputT, RenderingT>
6363
props: StateFlow<PropsT>,
6464
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
6565
coroutineContext: CoroutineContext = StandardTestDispatcher(),
66-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
66+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
6767
onOutput: suspend (OutputT) -> Unit = {},
6868
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
6969
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
@@ -207,7 +207,7 @@ public fun <StateT, OutputT, RenderingT>
207207
StatefulWorkflow<Unit, StateT, OutputT, RenderingT>.renderForTest(
208208
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
209209
coroutineContext: CoroutineContext = StandardTestDispatcher(),
210-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
210+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
211211
onOutput: suspend (OutputT) -> Unit = {},
212212
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
213213
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
@@ -240,7 +240,7 @@ public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.r
240240
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
241241
coroutineContext: CoroutineContext = StandardTestDispatcher(),
242242
interceptors: List<WorkflowInterceptor> = emptyList(),
243-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
243+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
244244
onOutput: suspend (OutputT) -> Unit = {},
245245
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
246246
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
@@ -264,7 +264,7 @@ public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.renderForTe
264264
coroutineContext: CoroutineContext = StandardTestDispatcher(),
265265
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
266266
interceptors: List<WorkflowInterceptor> = emptyList(),
267-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
267+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
268268
onOutput: suspend (OutputT) -> Unit = {},
269269
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
270270
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
@@ -299,7 +299,7 @@ public fun <PropsT, StateT, OutputT, RenderingT>
299299
props: StateFlow<PropsT>,
300300
initialState: StateT,
301301
coroutineContext: CoroutineContext = StandardTestDispatcher(),
302-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
302+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
303303
onOutput: suspend (OutputT) -> Unit = {},
304304
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
305305
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
@@ -329,7 +329,7 @@ public fun <StateT, OutputT, RenderingT>
329329
StatefulWorkflow<Unit, StateT, OutputT, RenderingT>.renderForTestFromStateWith(
330330
initialState: StateT,
331331
coroutineContext: CoroutineContext = StandardTestDispatcher(),
332-
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.CancelAndAwait(),
332+
teardown: WorkflowRuntimeTeardown = WorkflowRuntimeTeardown.Cancel,
333333
onOutput: suspend (OutputT) -> Unit = {},
334334
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
335335
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit

workflow-testing/src/test/java/com/squareup/workflow1/testing/WorkflowTurbineTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,45 +144,45 @@ class WorkflowTurbineTest {
144144

145145
@OptIn(ExperimentalCoroutinesApi::class)
146146
@Test
147-
fun `default teardown waits for runtime cancellation cleanup`() {
147+
fun `default teardown returns without awaiting runtime cancellation cleanup`() {
148148
val dispatcher = StandardTestDispatcher()
149149
val cleanupStarted = CompletableDeferred<Unit>()
150+
val cleanupCanFinish = CompletableDeferred<Unit>()
150151
var cleanupFinished = false
151152

152153
workflowWithCancellationCleanup(cleanupStarted) {
153-
delay(1)
154+
cleanupCanFinish.await()
154155
cleanupFinished = true
155156
}.renderForTest(
156157
coroutineContext = dispatcher
157158
) {
158159
cleanupStarted.await()
159-
assertFalse(cleanupFinished)
160160
}
161161

162+
assertFalse(cleanupFinished)
163+
cleanupCanFinish.complete(Unit)
164+
dispatcher.scheduler.advanceUntilIdle()
162165
assertTrue(cleanupFinished)
163166
}
164167

165168
@OptIn(ExperimentalCoroutinesApi::class)
166169
@Test
167-
fun `cancel teardown returns without awaiting runtime cancellation cleanup`() {
170+
fun `cancel and await teardown waits for runtime cancellation cleanup`() {
168171
val dispatcher = StandardTestDispatcher()
169172
val cleanupStarted = CompletableDeferred<Unit>()
170-
val cleanupCanFinish = CompletableDeferred<Unit>()
171173
var cleanupFinished = false
172174

173175
workflowWithCancellationCleanup(cleanupStarted) {
174-
cleanupCanFinish.await()
176+
delay(1)
175177
cleanupFinished = true
176178
}.renderForTest(
177179
coroutineContext = dispatcher,
178-
teardown = WorkflowRuntimeTeardown.Cancel
180+
teardown = WorkflowRuntimeTeardown.CancelAndAwait()
179181
) {
180182
cleanupStarted.await()
183+
assertFalse(cleanupFinished)
181184
}
182185

183-
assertFalse(cleanupFinished)
184-
cleanupCanFinish.complete(Unit)
185-
dispatcher.scheduler.advanceUntilIdle()
186186
assertTrue(cleanupFinished)
187187
}
188188

0 commit comments

Comments
 (0)