@Test
public void testSignal() {
GreetingActivity greetingActivity = mock(GreetingActivity.class);
when(greetingActivity.composeGreeting(anyString())).thenReturn("Hello Test");
worker.registerActivitiesImplementations(greetingActivity);
testEnv.start();
// Get a workflow stub using the same task queue the worker uses.
WorkflowOptions workflowOptions =
WorkflowOptions.newBuilder()
.setTaskQueue(TEST_QUEUE)
.setWorkflowTaskTimeout(Duration.ofSeconds(20))
.build();
HelloSignalWorkflow workflow =
client.newWorkflowStub(HelloSignalWorkflow.class, workflowOptions);
// Start workflow
WorkflowClient.start(workflow::start);
workflow.signalChange("INIT");
workflow.signalChange("STATE1");
testEnv.sleep(Duration.ofSeconds(2));
verify(greetingActivity, times(2)).composeGreeting(any());
}
}
Note that signals are asynchronous. The workflow.signalChange doesn’t wait for the workflow to process a signal. It returns immediately as soon as Temporal service queues up the signal for delivery. So your test sends two signals asynchronously and immediately checks for results without giving the workflow any chance to process them.
There is an issue that I need to troubleshoot. testEnv.sleep(Duration.ofSeconds(2)) doesn’t skip time in this scenario as it should. I’m looking into this.