package org.opendaylight.controller.cluster.raft.behaviors;
+import static org.junit.Assert.assertEquals;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.testkit.JavaTestKit;
import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
-import static org.junit.Assert.assertEquals;
-
public class CandidateTest extends AbstractRaftActorBehaviorTest {
private final ActorRef candidateActor = getSystem().actorOf(Props.create(
new JavaTestKit(getSystem()) {{
new Within(DefaultConfigParamsImpl.HEART_BEAT_INTERVAL.$times(6)) {
+ @Override
protected void run() {
Candidate candidate = new Candidate(createActorContext(getTestActor()));
final Boolean out = new ExpectMsg<Boolean>(DefaultConfigParamsImpl.HEART_BEAT_INTERVAL.$times(6), "ElectionTimeout") {
// do not put code outside this method, will run afterwards
+ @Override
protected Boolean match(Object in) {
if (in instanceof ElectionTimeout) {
return true;
@Test
public void testHandleElectionTimeoutWhenThereAreTwoNodesInCluster(){
MockRaftActorContext raftActorContext =
- (MockRaftActorContext) createActorContext();
+ createActorContext();
raftActorContext.setPeerAddresses(onePeer);
Candidate candidate =
new Candidate(raftActorContext);
@Test
public void testBecomeLeaderOnReceivingMajorityVotesInThreeNodesInCluster(){
MockRaftActorContext raftActorContext =
- (MockRaftActorContext) createActorContext();
+ createActorContext();
raftActorContext.setPeerAddresses(twoPeers);
Candidate candidate =
new Candidate(raftActorContext);
@Test
public void testBecomeLeaderOnReceivingMajorityVotesInFiveNodesInCluster(){
MockRaftActorContext raftActorContext =
- (MockRaftActorContext) createActorContext();
+ createActorContext();
raftActorContext.setPeerAddresses(fourPeers);
Candidate candidate =
new Candidate(raftActorContext);
new JavaTestKit(getSystem()) {{
new Within(duration("1 seconds")) {
+ @Override
protected void run() {
Candidate candidate = new Candidate(createActorContext(getTestActor()));
final Boolean out = new ExpectMsg<Boolean>(duration("1 seconds"), "AppendEntriesResponse") {
// do not put code outside this method, will run afterwards
+ @Override
protected Boolean match(Object in) {
if (in instanceof AppendEntriesReply) {
AppendEntriesReply reply = (AppendEntriesReply) in;
new JavaTestKit(getSystem()) {{
new Within(duration("1 seconds")) {
+ @Override
protected void run() {
Candidate candidate = new Candidate(createActorContext(getTestActor()));
final Boolean out = new ExpectMsg<Boolean>(duration("1 seconds"), "AppendEntriesResponse") {
// do not put code outside this method, will run afterwards
+ @Override
protected Boolean match(Object in) {
if (in instanceof RequestVoteReply) {
RequestVoteReply reply = (RequestVoteReply) in;
new JavaTestKit(getSystem()) {{
new Within(duration("1 seconds")) {
+ @Override
protected void run() {
RaftActorContext context = createActorContext(getTestActor());
final Boolean out = new ExpectMsg<Boolean>(duration("1 seconds"), "RequestVoteReply") {
// do not put code outside this method, will run afterwards
+ @Override
protected Boolean match(Object in) {
if (in instanceof RequestVoteReply) {
RequestVoteReply reply = (RequestVoteReply) in;
new JavaTestKit(getSystem()) {{
new Within(duration("1 seconds")) {
+ @Override
protected void run() {
RaftActorContext context = createActorContext(getTestActor());
final Boolean out = new ExpectMsg<Boolean>(duration("1 seconds"), "RequestVoteReply") {
// do not put code outside this method, will run afterwards
+ @Override
protected Boolean match(Object in) {
if (in instanceof RequestVoteReply) {
RequestVoteReply reply = (RequestVoteReply) in;
return new Candidate(actorContext);
}
- @Override protected RaftActorContext createActorContext() {
+ @Override protected MockRaftActorContext createActorContext() {
return new MockRaftActorContext("test", getSystem(), candidateActor);
}