Best way to delay/pause workflow execution for testing

I’m wondering if there are any established ways to pause or delay a workflow execution?

I have implemented a query method that has a different response depending on the overall progress of the workflow itself.

I would like to be able to control the progress of the workflow in order to deterministically control what the query method should be expected to return

I’ve been able to implement something like this by mocking an activity response with Thread.sleep, but feel this is a brittle/less than ideal solution.

So I was wondering if there were any better solutions offered by the framework?

1 Like

You can make queries from the mocked activities. This way you can verify that at the point that activity is invoked the query returns the expected value. Here is the code that worked for me:

  @Test
  public void testMockedActivity() {
    String id = "id1";
    GreetingActivities activities = mock(GreetingActivities.class);
    when(activities.composeGreeting("Hello", "World"))
        .thenAnswer(
            (invocation) -> {
              String queryResult = client.newWorkflowStub(GreetingWorkflow.class, id).query1();
              assertEquals("queryResult1", queryResult);
              return "Hello World!";
            });
    worker.registerActivitiesImplementations(activities);
    testEnv.start();

    // Get a workflow stub using the same task queue the worker uses.
    GreetingWorkflow workflow =
        client.newWorkflowStub(
            GreetingWorkflow.class,
            WorkflowOptions.newBuilder().setWorkflowId(id).setTaskQueue(TASK_QUEUE).build());
    // Execute a workflow waiting for it to complete.
    String greeting = workflow.getGreeting("World");
    assertEquals("Hello World!", greeting);
  }

Also, you can always call testEnv.sleep to move forward to some time in the workflow future.