Signal not working in unit test

Hi

I am trying to send a signal in unit test below is my code.

WorkflowStub stub = workflowEnvironment.workflowClient.newUntypedWorkflowStub(
                  WORKFLOW_ID, Optional.empty(), Optional.empty());

stub.signal("signalReady",
                SignalReadyOperationsRequest.newBuilder()
                  .addAllOperations(
                      WorkflowTestUtils.buildMenuOperations(WORKFLOW_ID, createRequests))
                  .build());

  @io.temporal.workflow.WorkflowInterface
  public interface IMenuOperationTestWorkflow {
    @io.temporal.workflow.WorkflowMethod
    void main(String args);

    @io.temporal.workflow.SignalMethod
    void signalReady(SignalReadyOperationsRequest request);
  }

However, when i setup checkpoint, i can’t see signalReady being triggered, did i do anything wrong ?

Could you post the code of your unit test? At which point the signal is triggered?

Thanks Maxim.

Please see below my full unit test. It is triggered asynchronously in a separate thread after assure workflow has started running.

  @Test
  void run_successfullyHandlesSignaledOperations() {
    createRequests = DEFAULT_CREATE_REQUESTS;
    shouldSignal = true;
    mockMenuOperations(
        ImmutableList.of(
            ImmutableOperationResult.of("0", Status.getDefaultInstance()),
            ImmutableOperationResult.of("1", Status.getDefaultInstance())));

    mockMenuOperations(
        createRequests,
        ImmutableList.of(
            ImmutableOperationResult.of("0", Status.getDefaultInstance()),
            ImmutableOperationResult.of("1", Status.getDefaultInstance())),
        true,
        1);

    // Create a thread that waits for the workflow to start before attempting to signal the
    // workflow.
    Runnable signalReady =
        () -> {
          try {
            started.await();
          } catch (InterruptedException e) {
            Assertions.fail("Unexpected error awaiting workflow start");
          }
          WorkflowStub stub =
              workflowEnvironment.workflowClient.newUntypedWorkflowStub(
                  WORKFLOW_ID, Optional.empty(), Optional.empty());
           .build());

          stub.signal(
              "signalReady",
              SignalReadyOperationsRequest.newBuilder()
                  .addAllOperations(
                      WorkflowTestUtils.buildMenuOperations(WORKFLOW_ID, createRequests))
                  .build());
        };

    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.execute(signalReady);
    runWorkflowWithArgsAndNoAssertions(newArgs());
    executorService.shutdownNow();
  }

// Below is the code for kicking off the workflow

  /** Runs workflow with no mocks or assertions */
  @CanIgnoreReturnValue
  protected Object runWorkflowWithArgsAndNoAssertions(A workflowArgs) {

    workflowEnvironment.start(workflowImplFactories(), newActivityImpls());

    T workflow = newStub(persistentClass, WORKFLOW_ID, Duration.ofMinutes(2));
    WorkflowClient.start(newProc().apply(workflow), workflowArgs);
    WorkflowStub workflowStub =
        workflowEnvironment.workflowClient.newUntypedWorkflowStub(
            WORKFLOW_ID, Optional.empty(), Optional.empty());
    return assertDoesNotThrow(() -> workflowStub.getResult(getResponseClazz()));
  }

  protected T newStub(Class<T> clazz, String workflowId, Duration executionStartToCloseTimeout) {
    return workflowEnvironment.workflowClient.newWorkflowStub(
        clazz,
        WorkflowOptions.newBuilder()
            .setTaskQueue(TestWorkflowEnvironmentExtension.TASK_QUEUE)
            .setWorkflowExecutionTimeout(Duration.ofMinutes(2))
            .setWorkflowId(workflowId)
            .build());
  }

However , If i kick off signaling in the same thread. I can see the signal being received successfully , any idea why ?

  protected Object runWorkflowSignalWithArgsAndNoAssertions(
      A workflowArgs) {

    workflowEnvironment.start(workflowImplFactories(), newActivityImpls());

    T workflow = newStub(persistentClass, WORKFLOW_ID, Duration.ofMinutes(2));
    WorkflowClient.start(newProc().apply(workflow), workflowArgs);
    WorkflowStub workflowStub =
        workflowEnvironment.workflowClient.newUntypedWorkflowStub(
            WORKFLOW_ID, Optional.empty(), Optional.empty());
      workflowStub.signal(
          "signalReady",
          SignalReadyOperationsRequest.newBuilder().build());
    }
    return assertDoesNotThrow(() -> workflowStub.getResult(getResponseClazz()));
  }

How is a workflow stub created? It should use a workflowClient from the TestWorkflowEnvironment. Something like:

    AccountTransferWorkflow workflow =
        testWorkflowRule
            .getWorkflowClient()
            .newWorkflowStub(AccountTransferWorkflow.class, options);