Temporal Signals unit test in java

Hi @maxim May I Know, how can I write a unit test in java keeping the workflow active until a signal is received?

Here’s a sample code going over how to await a signal for your workflow.

Thanks for the reply Actually I have tried the same code but it is not able to find the task queue

.setTaskQueue(testWorkflowRule.getTaskQueue())

I tried hard coding task queue but my workflow is just keep running with these 3 logs appearing.

22:25:03.759 [main] INFO io.temporal.serviceclient.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///2745e72a-cffc-4184-862b-ad845c45afd6}}
22:25:03.760 [main] INFO io.temporal.serviceclient.TestServiceStubsImpl - Created TestServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///2745e72a-cffc-4184-862b-ad845c45afd6}}
22:25:03.760 [main] INFO io.temporal.serviceclient.OperatorServiceStubsImpl - Created OperatorServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///2745e72a-cffc-4184-862b-ad845c45afd6}}

This is my code

    WorkflowOptions workflowOptions =
            WorkflowOptions.newBuilder()
                    .setTaskQueue("test")
                    .setWorkflowIdReusePolicy(WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE)
                    .build();

   MyWorkflow workflow =
            testWorkflowRule.getWorkflowClient().newWorkflowStub(MyWorkflow.class, workflowOptions);

    workflow.myWorkflowMethod(activityRequest);
    testWorkflowRule.getTestEnvironment().sleep(Duration.ofDays(1));
    workflow.myWorkflowSignal(params1, params2 , params3, params4);

You call myWorkflowMethod synchronously. So when this call returns, the workflow is already completed. Note that the sample referenced above starts the workflow asynchronously and then waits for its completion after sending the signal.

Thanks for the reply maxim. I have modified my code but still I can see these 3 logs lines and my workflow is running continuously without stopping.

Just to add we are using Junit 5

23:44:50.874 [main] INFO io.temporal.serviceclient.WorkflowServiceStubsImpl - Created WorkflowServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///96d5f41d-524e-4a8e-a0ed-c19fd37dbf3e}}
23:44:50.875 [main] INFO io.temporal.serviceclient.TestServiceStubsImpl - Created TestServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///96d5f41d-524e-4a8e-a0ed-c19fd37dbf3e}}
23:44:50.876 [main] INFO io.temporal.serviceclient.OperatorServiceStubsImpl - Created OperatorServiceStubs for channel: ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=5, target=directaddress:///96d5f41d-524e-4a8e-a0ed-c19fd37dbf3e}}

My modified code

        WorkflowClient.start(workflow:: myWorkflowMethod, myWorkflowRequest);
        testWorkflowRule.getTestEnvironment().sleep(Duration.ofDays(1));
        workflow.myWorkflowSignal(params1, params2 , params3, params4);

Try adding timeout to the unit test. It might give more info.

Hi,
I was able to resolve the issue following this

I have a follow up query. Can we retry a signal just like activities?

What do you mean by “retry a signal”?

As in when we receive a signal and for some reason the processing which needs to done fails. Like publishing some message to Kafka. Can it be retried instead of retriggering the signal again.

You cannot publish to Kafka from workflow code as all IO is prohibited there. So you have to use an activity for this. Activities are already retried on failure.

Okay, thank you so much.