Class TestCluster

java.lang.Object
io.camunda.zeebe.qa.util.cluster.TestCluster
All Implemented Interfaces:
CloseableSilently, AutoCloseable

public final class TestCluster extends Object implements CloseableSilently
A convenience class representing a one or more Spring applications that form a Zeebe cluster.

It's recommended to use the TestClusterBuilder to build one.

As the cluster is not started automatically, the nodes can still be modified/configured beforehand. Be aware however that the replication factor and the partitions count cannot be modified: if you configure different values directly on your brokers, then you may run into issues. Keep in mind as well that the gateways and brokers should be treated as immutable collections.

Example usage:


 final class MyClusteredTest {
   private TestCluster cluster;

   @BeforeEach
   void beforeEach() {
     cluster = TestCluster.builder()
         .withBrokersCount(3)
         .withReplicationFactor(3)
         .withPartitionsCount(1)
         .useEmbeddedGateway(true)
         .build();
   }

   @AfterEach
   void afterEach() {
     cluster.stop();
   }

   @Test
   void shouldConnectToCluster() {
     // given
     cluster.start();
     cluster.awaitCompleteTopology();

     // when
     final Topology topology;
     try (final ZeebeClient client = cluster.newClientBuilder().build()) {
       topology = c.newTopologyRequest().send().join();
     }

     // then
     assertThat(topology.getClusterSize()).isEqualTo(3);
   }
 }
 
  • Constructor Details

    • TestCluster

      public TestCluster(String name, int replicationFactor, int partitionsCount, Map<MemberId,TestStandaloneBroker> brokers, Map<MemberId,TestStandaloneGateway> gateways)
      Creates a new cluster from the given parameters.
      Parameters:
      name - the name of the cluster (should also be configured on the nodes)
      replicationFactor - the replication factor of the cluster
      partitionsCount - the number of partitions in the cluster
      brokers - the set of broker nodes, identified by their node ID
      gateways - the set of gateway nodes, identified by their member ID
  • Method Details

    • builder

      public static TestClusterBuilder builder()
      Returns a new cluster builder
    • start

      public TestCluster start()
      Starts all applications in the cluster.

      NOTE: This is a blocking method which returns when all applications are started from the Spring point of view. This means the broker startup has begun, but is not necessarily yet finished, such that it may not be ready.

      You can use await(TestHealthProbe, Duration) to wait until a specific probe (e.g. TestHealthProbe.READY succeeds on all nodes, or awaitCompleteTopology(Duration) to wait until the topology is complete for all gateways.

    • shutdown

      public TestCluster shutdown()
      Stops all containers in the cluster.

      NOTE: this method is blocking, and returns when all nodes are completely shut down.

    • replicationFactor

      public int replicationFactor()
      Returns the replication factor configured for the brokers; also used to check for a complete topology.
    • partitionsCount

      public int partitionsCount()
      Returns the partition count configured for the brokers; also used to check for a complete topology.
    • name

      public String name()
      Returns the cluster name.
    • availableGateway

      public TestGateway<?> availableGateway()
      Returns the first gateway which can accept requests from a Zeebe client.

      NOTE: this includes brokers with embedded gateways.

      Returns:
      a gateway ready to accept requests
      Throws:
      NoSuchElementException - if there are no such gateways (e.g. none are started, or they are dead, etc.)
    • anyGateway

      public TestGateway<?> anyGateway()
      Returns the first gateway which is running. The gateway may not be ready to accept requests.

      NOTE: this includes brokers with embedded gateways.

      Returns:
      a gateway
      Throws:
      NoSuchElementException - if there are no such gateways (e.g. none are started, or they are dead, etc.)
    • gateways

      public Map<MemberId,TestStandaloneGateway> gateways()
      Returns a map of the gateways in the cluster, where the keys are the memberIds, and the values the gateway containers.

      NOTE: this does not include brokers which are embedded gateways.

      Returns:
      the standalone gateways in this cluster
    • brokers

      public Map<MemberId,TestStandaloneBroker> brokers()
      Returns a map of the brokers in the cluster, where the keys are the broker's nodeId, and the values the broker containers.
      Returns:
      the brokers in this cluster
    • nodes

      public Map<MemberId,TestApplication<?>> nodes()
      Returns a map of all nodes in the cluster, where the keys are the member IDs (for brokers, the node ID), and the values are the nodes.
      Returns:
      the nodes of this cluster
    • newClientBuilder

      public ZeebeClientBuilder newClientBuilder()
      Builds a new client builder by picking a random gateway started gateway for it and disabling transport security.

      NOTE: this will properly automatically configure the security level, but will not configure authentication.

      Returns:
      a new client builder with the gateway and transport security pre-configured
      Throws:
      NoSuchElementException - if there are no started gateways
    • awaitCompleteTopology

      public TestCluster awaitCompleteTopology()
      Convenience alias for awaitCompleteTopology(Duration) with a default value of 1 minute per brokers.
    • awaitCompleteTopology

      public TestCluster awaitCompleteTopology(Duration timeout)
      Waits until every gateway in the cluster sees a complete and healthy topology, as defined by the replicationFactor, partitionsCount, and count of brokers in this cluster.
      Parameters:
      timeout - maximum time to block before failing
      Throws:
      org.awaitility.core.ConditionTimeoutException - if timeout expires before the topology is complete
    • awaitCompleteTopology

      public TestCluster awaitCompleteTopology(int clusterSize, int partitionsCount, int replicationFactor, Duration timeout)
      Waits until every gateway in the cluster sees a complete and healthy topology, as defined by the replicationFactor, partitionsCount, and count of brokers in this cluster.
      Parameters:
      clusterSize - the expected brokers count
      partitionsCount - the expected partitions count
      replicationFactor - the expected number of replicas per partition
      timeout - maximum time to block before failing
      Throws:
      org.awaitility.core.ConditionTimeoutException - if timeout expires before the topology is complete
    • awaitHealthyTopology

      public TestCluster awaitHealthyTopology()
    • awaitHealthyTopology

      public TestCluster awaitHealthyTopology(Duration timeout)
    • await

      public TestCluster await(TestHealthProbe probe)
      Convenience method for await(TestHealthProbe, Duration) with a default timeout of 1 minute per node in the cluster.
      Parameters:
      probe - the probe to wait for
    • await

      public TestCluster await(TestHealthProbe probe, Duration timeout)
      Waits until the given probe succeeds, or until the timeout expires.
      Parameters:
      probe - the probe to check
      timeout - maximum time to block
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface CloseableSilently