Issues with Unit Testing a Workflow with signal method

Hello
I am using java temporal-sdk v1.0.6.
I am trying to test a Workflow with signal method using mocks for Activity.
Sample code is here Comparing temporalio:master...ansujohn:SignalWF_Test · temporalio/samples-java · GitHub

Couple of issues that i get in samples-java/HelloSignalWorkflowTest.java at SignalWF_Test · ansujohn/samples-java · GitHub

  1. Test failing with error “Actually, there were zero interactions with this mock
  2. Though signalChange method is called twice, processing is done in the Workflow for the first call only samples-java/HelloSignalWorkflowTest.java at SignalWF_Test · ansujohn/samples-java · GitHub
  3. Without a sleep in the test - samples-java/HelloSignalWorkflowTest.java at SignalWF_Test · ansujohn/samples-java · GitHub, tasks simply run and complete without any processing, doesn’t wait for signals, no debug outputs. Why is the sleep required, what is the recommended value, wouldn’t this increase the test execution time?

Am i missing some configuration?
Appreciate the help.
Thank you

The following test passes:

  @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.

Thank you for the response.

So introducing testEnv.sleep(Duration.ofSeconds(2)); after/between signalChange calls allow the workflow to process them, is that right?

Will adding WorkflowStub.fromTyped(workflow).getResult(Void.class); before assertions help?

Yes there is an issue with testEnv.sleep as it doesn’t skip time and hence increases test run time.

Yes, I think adding getResult to wait for the workflow completion would help assuming that the workflow completes.