JUnit test of parallel gateway with async before

Hello,

I was trying to create a process with a section where four streams are processed in parallel. See diagram CamundaParallelExecution.bpmn (13.5 KB)

The send processes to send a message to a queue, the wait states are triggered when the queued request is answered using following code fragment:

The consume process does add the results to a list in the Camunda variables.

In the first approach we did not set the Consume tasks to async before and got an OptimisticLockingException. This had been circumvented by flagging the task as “async before” and now the process works fine.

Unfortunately this does not apply to our JUnit tests. The following test works fine without async before but does not work in async mode any more:

  @Test
  @Deployment(resources = "processes/CamundaParallelExecution.bpmn")
  public void testSendRequest() {

    List<String> response = new ArrayList<>();

    ProcessInstance processInstance =
        runtimeService().startProcessInstanceByKey(SERVICE_NAME, withVariables("response", response));

    assertThat(processInstance).isStarted();

    // Prozessflow bis zum Warten auf RĂĽckmeldung von Grundinfo
    assertThat(processInstance)
        .hasPassed(EVENT_START, ACTIVITY_BRANCH_1, ACTIVITY_BRANCH_2, ACTIVITY_BRANCH_3, ACTIVITY_BRANCH_4)
        .hasNotPassed(ACTIVITY_CONSUME_1, ACTIVITY_CONSUME_2, ACTIVITY_CONSUME_3, ACTIVITY_CONSUME_4)
        .isWaitingAt(EVENT_WAIT_1, EVENT_WAIT_2, EVENT_WAIT_3, EVENT_WAIT_4);

    runtimeService().createMessageCorrelation("Message_Branch_1").processInstanceId(processInstance.getId())
        .correlate();
    assertThat(processInstance).hasPassed(EVENT_WAIT_1, ACTIVITY_CONSUME_1).hasNotPassed(ACTIVITY_SEND_RESPONSE);

    runtimeService().createMessageCorrelation("Message_Branch_2").processInstanceId(processInstance.getId())
        .correlate();
    assertThat(processInstance).hasPassed(EVENT_WAIT_2, ACTIVITY_CONSUME_2).hasNotPassed(ACTIVITY_SEND_RESPONSE);

    runtimeService().createMessageCorrelation("Message_Branch_3").processInstanceId(processInstance.getId())
        .correlate();
    assertThat(processInstance).hasPassed(EVENT_WAIT_3, ACTIVITY_CONSUME_3).hasNotPassed(ACTIVITY_SEND_RESPONSE);

    runtimeService().createMessageCorrelation("Message_Branch_4").processInstanceId(processInstance.getId())
        .correlate();
    assertThat(processInstance).hasPassed(EVENT_WAIT_4, ACTIVITY_CONSUME_4, ACTIVITY_SEND_RESPONSE, EVENT_STOP);
  }

I assume this is because the work is done by the JobExecutor. Is there a way to test my flow in JUnit?

Best regards
Carl-Peter

Hi @cpkrause,

in the JUnit Test, the job executor is disabled by default. The reason for this is to make execution of the process instance as explicit as possible.

You can execute a job explicitly with

execute(job());

if you have only one job. You can add this after each correlation.

If you have multiple jobs in the database, which will be case if you contine after the 4th message, you have to query for the joblist before:

    List<Job> jobList = jobQuery().list();
    for (Job job : jobList) {
      execute(job);
    }

Hope this helps, Ingo

1 Like