Search in sources :

Example 1 with JobHandler

use of io.camunda.zeebe.client.api.worker.JobHandler in project zeebe-test-container by camunda-community-hub.

the class TriggerTimerCatchEventTest method shouldTriggerTimerStartEvent.

@Test
@Timeout(value = 5, unit = TimeUnit.MINUTES)
void shouldTriggerTimerStartEvent() {
    // given
    final ZeebeClock clock = ZeebeClock.newDefaultClock(zeebeContainer);
    final BpmnModelInstance process = Bpmn.createExecutableProcess("process").startEvent().intermediateCatchEvent().timerWithDate(TIMER_DATE.toString()).serviceTask("task", b -> b.zeebeJobType(JOB_TYPE)).endEvent().done();
    final List<ActivatedJob> activatedJobs = new CopyOnWriteArrayList<>();
    final Instant brokerTime;
    // when
    final JobHandler handler = (client, job) -> activatedJobs.add(job);
    try (final ZeebeClient client = newZeebeClient(zeebeContainer);
        final JobWorker worker = newJobWorker(handler, client)) {
        client.newDeployCommand().addProcessModel(process, "process.bpmn").send().join();
        client.newCreateInstanceCommand().bpmnProcessId("process").latestVersion().send().join();
        brokerTime = clock.addTime(TIME_OFFSET);
        Awaitility.await("until a job has been activated by the worker").untilAsserted(() -> Assertions.assertThat(activatedJobs).hasSize(1));
    }
    // then
    Assertions.assertThat(activatedJobs).as("the timer event was triggered and a job is now available").hasSize(1);
    Assertions.assertThat(brokerTime).as("the modified time is at least equal to one day from now").isAfterOrEqualTo(TIMER_DATE);
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) ZeebeContainer(io.zeebe.containers.ZeebeContainer) Bpmn(io.camunda.zeebe.model.bpmn.Bpmn) Instant(java.time.Instant) ZeebeClock(io.zeebe.containers.clock.ZeebeClock) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Duration(java.time.Duration) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) Assertions(org.assertj.core.api.Assertions) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Awaitility(org.awaitility.Awaitility) Timeout(org.junit.jupiter.api.Timeout) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Container(org.testcontainers.junit.jupiter.Container) ZeebeClock(io.zeebe.containers.clock.ZeebeClock) Instant(java.time.Instant) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JobWorker(io.camunda.zeebe.client.api.worker.JobWorker) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with JobHandler

use of io.camunda.zeebe.client.api.worker.JobHandler in project zeebe by camunda.

the class RollingUpdateTest method shouldPerformRollingUpdate.

@Test
void shouldPerformRollingUpdate() {
    // given
    cluster.start();
    // when
    final long firstProcessInstanceKey;
    ZeebeGatewayNode<?> availableGateway = cluster.getGateways().get("0");
    try (final var client = newZeebeClient(availableGateway)) {
        deployProcess(client);
        // potentially retry in case we're faster than the deployment distribution
        firstProcessInstanceKey = Awaitility.await("process instance creation").atMost(Duration.ofSeconds(5)).pollInterval(Duration.ofMillis(100)).ignoreExceptions().until(() -> createProcessInstance(client), Objects::nonNull).getProcessInstanceKey();
    }
    for (int i = cluster.getBrokers().size() - 1; i >= 0; i--) {
        try (final ZeebeClient client = newZeebeClient(availableGateway)) {
            final var brokerId = i;
            final ZeebeBrokerNode<?> broker = cluster.getBrokers().get(i);
            broker.stop();
            Awaitility.await("broker is removed from topology").atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)).untilAsserted(() -> assertTopologyDoesNotContainerBroker(client, brokerId));
            updateBroker(broker);
            broker.start();
            Awaitility.await("updated broker is added to topology").atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)).untilAsserted(() -> assertTopologyContainsUpdatedBroker(client, brokerId));
            availableGateway = cluster.getGateways().get(String.valueOf(i));
        }
    }
    // then
    final Map<Long, List<String>> activatedJobs = new HashMap<>();
    final var expectedOrderedJobs = List.of("firstTask", "secondTask");
    final JobHandler jobHandler = (jobClient, job) -> {
        jobClient.newCompleteCommand(job.getKey()).send().join();
        activatedJobs.compute(job.getProcessInstanceKey(), (ignored, list) -> {
            final var appendedList = Optional.ofNullable(list).orElse(new CopyOnWriteArrayList<>());
            appendedList.add(job.getType());
            return appendedList;
        });
    };
    try (final var client = newZeebeClient(availableGateway)) {
        final var secondProcessInstanceKey = createProcessInstance(client).getProcessInstanceKey();
        final var expectedActivatedJobs = Map.of(firstProcessInstanceKey, expectedOrderedJobs, secondProcessInstanceKey, expectedOrderedJobs);
        client.newWorker().jobType("firstTask").handler(jobHandler).open();
        client.newWorker().jobType("secondTask").handler(jobHandler).open();
        Awaitility.await("all jobs have been activated").atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertThat(activatedJobs).as("the expected number of jobs has been activated").isEqualTo(expectedActivatedJobs));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Bpmn(io.camunda.zeebe.model.bpmn.Bpmn) HashMap(java.util.HashMap) Network(org.testcontainers.containers.Network) ZeebeBrokerNode(io.zeebe.containers.ZeebeBrokerNode) PartitionStatus(io.camunda.zeebe.test.util.actuator.PartitionsActuatorClient.PartitionStatus) ZeebeGatewayNode(io.zeebe.containers.ZeebeGatewayNode) Duration(java.time.Duration) Map(java.util.Map) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) Either(io.camunda.zeebe.util.Either) VersionUtil(io.camunda.zeebe.util.VersionUtil) CloseHelper(org.agrona.CloseHelper) ZeebeVolume(io.zeebe.containers.ZeebeVolume) ContainerState(org.testcontainers.containers.ContainerState) ProcessInstanceEvent(io.camunda.zeebe.client.api.response.ProcessInstanceEvent) EitherAssert(io.camunda.zeebe.test.util.asserts.EitherAssert) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ZeebeTestContainerDefaults(io.camunda.zeebe.test.util.testcontainers.ZeebeTestContainerDefaults) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) PartitionsActuatorClient(io.camunda.zeebe.test.util.actuator.PartitionsActuatorClient) Optional(java.util.Optional) TopologyAssert(io.camunda.zeebe.test.util.asserts.TopologyAssert) ZeebeCluster(io.zeebe.containers.cluster.ZeebeCluster) Awaitility(org.awaitility.Awaitility) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) Objects(java.util.Objects) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with JobHandler

use of io.camunda.zeebe.client.api.worker.JobHandler in project zeebe by camunda.

the class RecordingJobHandler method handle.

@Override
public void handle(final JobClient client, final ActivatedJob job) throws Exception {
    final JobHandler handler = jobHandlers[nextJobHandler];
    nextJobHandler = Math.min(nextJobHandler + 1, jobHandlers.length - 1);
    try {
        handler.handle(client, job);
    } finally {
        handledJobs.add(job);
    }
}
Also used : JobHandler(io.camunda.zeebe.client.api.worker.JobHandler)

Example 4 with JobHandler

use of io.camunda.zeebe.client.api.worker.JobHandler in project zeebe by zeebe-io.

the class QueryApiIT method activateJob.

private ActivatedJob activateJob() {
    final List<ActivatedJob> jobs = new CopyOnWriteArrayList<>();
    final JobHandler handler = (client, job) -> jobs.add(job);
    try (final var client = createZeebeClient("tenantA");
        final var worker = client.newWorker().jobType("type").handler(handler).open()) {
        client.newCreateInstanceCommand().processDefinitionKey(processDefinitionKey).send().join();
        Awaitility.await("until one job is activated").untilAsserted(() -> assertThat(jobs).isNotEmpty());
    }
    return jobs.get(0);
}
Also used : ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) TestAuthorizationClientInterceptor(io.camunda.zeebe.it.queryapi.util.TestAuthorizationClientInterceptor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ByteBuddy(net.bytebuddy.ByteBuddy) ZeebeContainer(io.zeebe.containers.ZeebeContainer) LoggerFactory(org.slf4j.LoggerFactory) Bpmn(io.camunda.zeebe.model.bpmn.Bpmn) CompleteJobResponse(io.camunda.zeebe.client.api.response.CompleteJobResponse) Future(java.util.concurrent.Future) TestAuthorizationServerInterceptor(io.camunda.zeebe.it.queryapi.util.TestAuthorizationServerInterceptor) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Duration(java.time.Duration) Map(java.util.Map) ActivatedJob(io.camunda.zeebe.client.api.response.ActivatedJob) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) Container(org.testcontainers.junit.jupiter.Container) Logger(org.slf4j.Logger) ProcessInstanceEvent(io.camunda.zeebe.client.api.response.ProcessInstanceEvent) Files(java.nio.file.Files) TestAuthorizationListener(io.camunda.zeebe.it.queryapi.util.TestAuthorizationListener) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) CloseAwareListener(io.camunda.zeebe.test.util.grpc.CloseAwareListener) IOException(java.io.IOException) ContainerLogsDumper(io.camunda.zeebe.test.util.testcontainers.ContainerLogsDumper) File(java.io.File) StatusRuntimeException(io.grpc.StatusRuntimeException) UncheckedIOException(java.io.UncheckedIOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) List(java.util.List) ZeebeTestContainerDefaults(io.camunda.zeebe.test.util.testcontainers.ZeebeTestContainerDefaults) ZeebeClient(io.camunda.zeebe.client.ZeebeClient) MountableFile(org.testcontainers.utility.MountableFile) JobHandler(io.camunda.zeebe.client.api.worker.JobHandler) Awaitility(org.awaitility.Awaitility) CancelProcessInstanceResponse(io.camunda.zeebe.client.api.response.CancelProcessInstanceResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with JobHandler

use of io.camunda.zeebe.client.api.worker.JobHandler in project zeebe by zeebe-io.

the class RecordingJobHandler method handle.

@Override
public void handle(final JobClient client, final ActivatedJob job) throws Exception {
    final JobHandler handler = jobHandlers[nextJobHandler];
    nextJobHandler = Math.min(nextJobHandler + 1, jobHandlers.length - 1);
    try {
        handler.handle(client, job);
    } finally {
        handledJobs.add(job);
    }
}
Also used : JobHandler(io.camunda.zeebe.client.api.worker.JobHandler)

Aggregations

JobHandler (io.camunda.zeebe.client.api.worker.JobHandler)11 ZeebeClient (io.camunda.zeebe.client.ZeebeClient)8 Bpmn (io.camunda.zeebe.model.bpmn.Bpmn)8 BpmnModelInstance (io.camunda.zeebe.model.bpmn.BpmnModelInstance)8 Duration (java.time.Duration)8 List (java.util.List)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 Awaitility (org.awaitility.Awaitility)8 Test (org.junit.jupiter.api.Test)8 ProcessInstanceEvent (io.camunda.zeebe.client.api.response.ProcessInstanceEvent)6 ZeebeTestContainerDefaults (io.camunda.zeebe.test.util.testcontainers.ZeebeTestContainerDefaults)6 Map (java.util.Map)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 ActivatedJob (io.camunda.zeebe.client.api.response.ActivatedJob)5 ZeebeContainer (io.zeebe.containers.ZeebeContainer)5 TimeUnit (java.util.concurrent.TimeUnit)5 Container (org.testcontainers.junit.jupiter.Container)5 Testcontainers (org.testcontainers.junit.jupiter.Testcontainers)5 CancelProcessInstanceResponse (io.camunda.zeebe.client.api.response.CancelProcessInstanceResponse)3 CompleteJobResponse (io.camunda.zeebe.client.api.response.CompleteJobResponse)3