Class MockRaftCluster

java.lang.Object
org.jgroups.raft.testfwk.MockRaftCluster
Direct Known Subclasses:
PartitionedRaftCluster, RaftCluster

public abstract class MockRaftCluster extends Object
Base class for the cluster implementations in the test framework.

The cluster abstraction facilitates the creation of a cluster during the tests. This approach avoids the need to create actual JChannel and complex configurations. This abstraction provides a simplified and controllable way to test the cluster, emitting events and sending messages.

The cluster abstraction works through View updates. Members are added and removed manually. When a new view is received, the members receive the update accordingly and follow the synchronous configuration. The user utilizing this abstraction has more control over when a member joins and which messages it receives.

Our test suite contains examples of uses of the cluster abstraction.
Since:
1.0.12
  • Field Details

  • Constructor Details

    • MockRaftCluster

      public MockRaftCluster()
  • Method Details

    • handleView

      public abstract void handleView(org.jgroups.View view)
      Emit the view update to all cluster members.

      How the view is updated might vary from implementation. The basic idea is to iterate over all members and invoke each protocol in the stack to handle the view.

      Parameters:
      view - : The new View instance to update the member.
    • send

      public abstract void send(org.jgroups.Message msg)
      Send a message in the cluster.
      Parameters:
      msg - : Message to send.
    • size

      public abstract int size()
      The number of member in the cluster.
      Returns:
      The size of the cluster.
    • add

      public abstract <T extends MockRaftCluster> T add(org.jgroups.Address addr, RaftNode node)
      Add a new member to the cluster.

      The new member is associated with the given address. A member is resolved by the address when sending a message. Also, note that a view update is necessary to propagate the member addition.

      Type Parameters:
      T - : The current instance type.
      Parameters:
      addr - : The new member's address.
      node - : The member abstraction wrapped in RaftNode.
      Returns:
      The fluent current class.
    • remove

      public abstract <T extends MockRaftCluster> T remove(org.jgroups.Address addr)
      Remove a member from the cluster.

      A view update is necessary to propagate the member removal.

      Type Parameters:
      T - : The current instance type.
      Parameters:
      addr - : The address of the member to remove.
      Returns:
      The fluent current class.
    • clear

      public abstract <T extends MockRaftCluster> T clear()
      Remove all members from the cluster.
      Type Parameters:
      T - : The current instance type.
      Returns:
      The fluent current class.
    • addCommandInterceptor

      public BlockingMessageInterceptor addCommandInterceptor(Predicate<org.jgroups.Message> predicate)
      Intercept messages before sending.

      Before sending each message, the interceptor verifies whether to block. The messages must be released utilizing the returned instance.

      Warning: Blocking a message in a synchronous cluster will block the calling thread.

      To simulate a delay in the message, utilize the async(boolean) method passing true. This will not block the invoking thread.

      Parameters:
      predicate - : The predicate to check whether to block.
      Returns:
      A new BlockingMessageInterceptor instance to control the blocking mechanism.
    • self

      protected final <T extends MockRaftCluster> T self()
      Utility to create a fluent use.
      Type Parameters:
      T - : The current instance type.
      Returns:
      The fluent current class.
    • async

      public boolean async()
      Check whether the cluster is in asynchronous mode.
      Returns:
      true if asynchronous, false, otherwise.
    • async

      public <T extends MockRaftCluster> T async(boolean b)
      Update the cluster mode between synchronous and asynchronous.
      Type Parameters:
      T - : The current instance type.
      Parameters:
      b - : true to run asynchronous, false to run synchronous.
      Returns:
      The fluent current class.
    • createThreadPool

      protected Executor createThreadPool(long max_idle_ms)
      Create the Executor to submit tasks during asynchronous mode.
      Parameters:
      max_idle_ms - : Executor configuration parameter.
      Returns:
      The Executor instance to utilize in the cluster abstraction.
    • deliverAsync

      protected void deliverAsync(RaftNode node, org.jgroups.Message msg)
      Asynchronously sends a message up the node stack.
      Parameters:
      node - : The node to handle the message.
      msg - : The message to send.