Skip to content

Commit

Permalink
Make WorkflowTester tests use the main context as the worker context.
Browse files Browse the repository at this point in the history
This is an enhancement to #940 that gives more reasonable behavior. The context
parameter to the test methods are primarily used to pass in test dispatchers, and
those dispatchers often really only need to be used for workers.

Since I don't know of a use case for specifying context separately just for workers,
this also removes the `WorkflowTestParams.workerContext` property.
  • Loading branch information
zach-klippenstein committed Feb 6, 2020
1 parent 8ed5265 commit ffa2556
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.squareup.workflow.testing.WorkflowTestParams.StartMode.StartFromWorkf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import org.jetbrains.annotations.TestOnly
import kotlin.coroutines.CoroutineContext

/**
* Launches the [workflow] in a new coroutine in [scope]. The workflow tree is seeded with
Expand All @@ -42,6 +43,7 @@ fun <PropsT, StateT, OutputT : Any, RenderingT, RunnerT> launchWorkflowForTestFr
workflow: StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>,
props: Flow<PropsT>,
testParams: WorkflowTestParams<StateT>,
workerContext: CoroutineContext,
beforeStart: CoroutineScope.(session: WorkflowSession<OutputT, RenderingT>) -> RunnerT
): RunnerT {
val initialState = (testParams.startFrom as? StartFromState)?.state
Expand All @@ -63,6 +65,6 @@ fun <PropsT, StateT, OutputT : Any, RenderingT, RunnerT> launchWorkflowForTestFr
initialState = initialState,
initialSnapshot = initialSnapshot,
beforeStart = beforeStart,
workerContext = testParams.workerContext
workerContext = workerContext
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import kotlin.coroutines.EmptyCoroutineContext
* It is recommended to leave this on, but if you need to debug a test and don't want to have to
* deal with the extra passes, you can temporarily set it to false.
* @param workerContext Used to customize the context in which workers are started for tests.
* Default is [EmptyCoroutineContext], which means the workers will use the context from their
* workflow and use the [Unconfined][kotlinx.coroutines.Dispatchers.Unconfined] dispatcher.
* Default is [EmptyCoroutineContext], which means the workers will use their workflow's context
* plus the base context passed into the test method. Any context element present in [workerContext]
* will override all other context elements for workers.
*/
@TestOnly
data class WorkflowTestParams<out StateT>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ fun <T, PropsT, StateT, OutputT : Any, RenderingT>
scope = CoroutineScope(Unconfined + context + uncaughtExceptionHandler),
workflow = this@test,
props = propsChannel.asFlow(),
testParams = testParams
testParams = testParams,
// Use the base context as the starting point for the worker context since it's often used
// to pass in test dispatchers, and that's reasonable behavior.
workerContext = context + testParams.workerContext
) { session ->
WorkflowTester(this, propsChannel, session.renderingsAndSnapshots, session.outputs)
.apply { collectFromWorkflow() }
Expand Down

0 comments on commit ffa2556

Please sign in to comment.