Search in sources :

Example 21 with ZeebeClient

use of io.camunda.zeebe.client.ZeebeClient in project zeebe by camunda.

the class LongPollingActivateJobsTest method sendActivateRequestsAndClose.

private void sendActivateRequestsAndClose(final String jobType, final int count) throws InterruptedException {
    for (int i = 0; i < count; i++) {
        final ZeebeClient client = ZeebeClient.newClientBuilder().gatewayAddress(NetUtil.toSocketAddressString(BROKER_RULE.getGatewayAddress())).usePlaintext().build();
        client.newActivateJobsCommand().jobType(jobType).maxJobsToActivate(5).workerName("closed-" + i).send();
        Thread.sleep(100);
        client.close();
    }
}
Also used : ZeebeClient(io.camunda.zeebe.client.ZeebeClient)

Example 22 with ZeebeClient

use of io.camunda.zeebe.client.ZeebeClient 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 23 with ZeebeClient

use of io.camunda.zeebe.client.ZeebeClient in project zeebe by camunda.

the class TopologyViewer method main.

public static void main(final String[] args) {
    final String defaultAddress = "localhost:26500";
    final String envVarAddress = System.getenv("ZEEBE_ADDRESS");
    final ZeebeClientBuilder clientBuilder;
    final String contactPoint;
    if (envVarAddress != null) {
        /* Connect to Camunda Cloud Cluster, assumes that credentials are set in environment variables.
       * See JavaDoc on class level for details
       */
        contactPoint = envVarAddress;
        clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(envVarAddress);
    } else {
        // connect to local deployment; assumes that authentication is disabled
        contactPoint = defaultAddress;
        clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(defaultAddress).usePlaintext();
    }
    try (final ZeebeClient client = clientBuilder.build()) {
        System.out.println("Requesting topology with initial contact point " + contactPoint);
        final Topology topology = client.newTopologyRequest().send().join();
        System.out.println("Topology:");
        topology.getBrokers().forEach(b -> {
            System.out.println("    " + b.getAddress());
            b.getPartitions().forEach(p -> System.out.println("      " + p.getPartitionId() + " - " + p.getRole()));
        });
        System.out.println("Done.");
    }
}
Also used : ZeebeClient(io.camunda.zeebe.client.ZeebeClient) ZeebeClientBuilder(io.camunda.zeebe.client.ZeebeClientBuilder) Topology(io.camunda.zeebe.client.api.response.Topology)

Example 24 with ZeebeClient

use of io.camunda.zeebe.client.ZeebeClient in project zeebe-process-test by camunda-cloud.

the class ZeebeProcessTestExtension method afterEach.

/**
 * After each test the client will be closed. At this point we will reset the engine. This will
 * stop the current engine and create a new one. The new engine will not be started yet as that is
 * done in the beforeEach method.
 *
 * @param extensionContext jUnit5 extension context
 */
@Override
public void afterEach(final ExtensionContext extensionContext) {
    final Object clientContent = getStore(extensionContext).get(KEY_ZEEBE_CLIENT);
    final ZeebeClient client = (ZeebeClient) clientContent;
    client.close();
    final Object engineContent = getStore(extensionContext.getParent().get()).get(KEY_ZEEBE_ENGINE);
    final ContainerizedEngine engine = (ContainerizedEngine) engineContent;
    engine.reset();
}
Also used : ZeebeClient(io.camunda.zeebe.client.ZeebeClient)

Example 25 with ZeebeClient

use of io.camunda.zeebe.client.ZeebeClient in project zeebe-process-test by camunda-cloud.

the class ZeebeProcessTestExtension method beforeEach.

/**
 * Before each test a new test engine gets created and started. A client to communicate with the
 * engine will also be created. Together with a {@link RecordStream} these will be injected in the
 * fields of the test class, if they are available.
 *
 * @param extensionContext jUnit5 extension context
 */
@Override
public void beforeEach(final ExtensionContext extensionContext) {
    final ZeebeTestEngine engine = EngineFactory.create();
    engine.start();
    final ZeebeClient client = engine.createClient();
    final RecordStream recordStream = RecordStream.of(engine.getRecordStreamSource());
    try {
        injectFields(extensionContext, engine, client, recordStream);
    } catch (final Exception ex) {
        client.close();
        engine.stop();
        throw ex;
    }
    BpmnAssert.initRecordStream(recordStream);
    getStore(extensionContext).put(KEY_ZEEBE_CLIENT, client);
    getStore(extensionContext).put(KEY_ZEEBE_ENGINE, engine);
}
Also used : ZeebeTestEngine(io.camunda.zeebe.process.test.api.ZeebeTestEngine) RecordStream(io.camunda.zeebe.process.test.filters.RecordStream) ZeebeClient(io.camunda.zeebe.client.ZeebeClient)

Aggregations

ZeebeClient (io.camunda.zeebe.client.ZeebeClient)90 ZeebeClientBuilder (io.camunda.zeebe.client.ZeebeClientBuilder)27 Test (org.junit.jupiter.api.Test)23 Topology (io.camunda.zeebe.client.api.response.Topology)16 ProcessInstanceEvent (io.camunda.zeebe.client.api.response.ProcessInstanceEvent)13 JobWorker (io.camunda.zeebe.client.api.worker.JobWorker)13 BpmnModelInstance (io.camunda.zeebe.model.bpmn.BpmnModelInstance)13 Test (org.junit.Test)12 Timeout (org.junit.jupiter.api.Timeout)11 Duration (java.time.Duration)10 DeploymentEvent (io.camunda.zeebe.client.api.response.DeploymentEvent)9 Bpmn (io.camunda.zeebe.model.bpmn.Bpmn)9 Awaitility (org.awaitility.Awaitility)8 Map (java.util.Map)7 BrokerInfo (io.camunda.zeebe.client.api.response.BrokerInfo)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Optional (java.util.Optional)6 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 Future (java.util.concurrent.Future)6