Class MockRaftCluster
- Direct Known Subclasses:
PartitionedRaftCluster,RaftCluster
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.
- Since:
- 1.0.12
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected BlockingMessageInterceptorprotected final Executor -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <T extends MockRaftCluster>
TAdd a new member to the cluster.addCommandInterceptor(Predicate<org.jgroups.Message> predicate) Intercept messages before sending.booleanasync()Check whether the cluster is in asynchronous mode.<T extends MockRaftCluster>
Tasync(boolean b) Update the cluster mode between synchronous and asynchronous.abstract <T extends MockRaftCluster>
Tclear()Remove all members from the cluster.protected ExecutorcreateThreadPool(long max_idle_ms) Create theExecutorto submit tasks during asynchronous mode.protected voiddeliverAsync(RaftNode node, org.jgroups.Message msg) Asynchronously sends a message up the node stack.abstract voidhandleView(org.jgroups.View view) Emit the view update to all cluster members.abstract <T extends MockRaftCluster>
Tremove(org.jgroups.Address addr) Remove a member from the cluster.protected final <T extends MockRaftCluster>
Tself()Utility to create a fluent use.abstract voidsend(org.jgroups.Message msg) Send a message in the cluster.abstract intsize()The number of member in the cluster.
-
Field Details
-
thread_pool
-
async
protected boolean async -
interceptor
-
-
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 newViewinstance 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
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 inRaftNode.- Returns:
- The fluent current class.
-
remove
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
Remove all members from the cluster.- Type Parameters:
T- : The current instance type.- Returns:
- The fluent current class.
-
addCommandInterceptor
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 passingtrue. This will not block the invoking thread.- Parameters:
predicate- : The predicate to check whether to block.- Returns:
- A new
BlockingMessageInterceptorinstance to control the blocking mechanism.
-
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:
trueif asynchronous,false, otherwise.
-
async
Update the cluster mode between synchronous and asynchronous.- Type Parameters:
T- : The current instance type.- Parameters:
b- :trueto run asynchronous,falseto run synchronous.- Returns:
- The fluent current class.
-
createThreadPool
Create theExecutorto submit tasks during asynchronous mode.- Parameters:
max_idle_ms- : Executor configuration parameter.- Returns:
- The
Executorinstance to utilize in the cluster abstraction.
-
deliverAsync
Asynchronously sends a message up the node stack.- Parameters:
node- : The node to handle the message.msg- : The message to send.
-