Bug 8606: Continue leadership transfer on pauseLeader timeout
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorLeadershipTransferCohortTest.java
index 45c797c..44b0d2b 100644 (file)
@@ -11,11 +11,13 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
+
 import akka.dispatch.Dispatchers;
 import com.google.common.base.Function;
 import org.junit.After;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort.OnComplete;
+import org.opendaylight.controller.cluster.raft.behaviors.Leader;
 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
 
 /**
@@ -38,10 +40,11 @@ public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
 
     private void setup(String testName) {
         String persistenceId = factory.generateActorId(testName + "-leader-");
-        mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(
-                config).pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
+        config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
+        mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(config)
+                .pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
                 persistenceId).underlyingActor();
-        cohort = new RaftActorLeadershipTransferCohort(mockRaftActor, null);
+        cohort = new RaftActorLeadershipTransferCohort(mockRaftActor);
         cohort.addOnComplete(onComplete);
         mockRaftActor.waitForInitializeBehaviorComplete();
     }
@@ -52,15 +55,15 @@ public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
         cohort.setNewLeaderTimeoutInMillis(20000);
 
         cohort.onNewLeader("new-leader");
-        verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
+        verify(onComplete, never()).onSuccess(mockRaftActor.self());
 
         cohort.transferComplete();
 
         cohort.onNewLeader(null);
-        verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
+        verify(onComplete, never()).onSuccess(mockRaftActor.self());
 
         cohort.onNewLeader("new-leader");
-        verify(onComplete).onSuccess(mockRaftActor.self(), null);
+        verify(onComplete).onSuccess(mockRaftActor.self());
     }
 
     @Test
@@ -68,35 +71,37 @@ public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
         setup("testNewLeaderTimeout");
         cohort.setNewLeaderTimeoutInMillis(200);
         cohort.transferComplete();
-        verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self(), null);
+        verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self());
     }
 
     @Test
     public void testNotLeaderOnDoTransfer() {
-        config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
         setup("testNotLeaderOnDoTransfer");
         cohort.doTransfer();
-        verify(onComplete).onSuccess(mockRaftActor.self(), null);
+        verify(onComplete).onSuccess(mockRaftActor.self());
     }
 
     @Test
     public void testAbortTransfer() {
         setup("testAbortTransfer");
         cohort.abortTransfer();
-        verify(onComplete).onFailure(mockRaftActor.self(), null);
+        verify(onComplete).onFailure(mockRaftActor.self());
     }
 
     @Test
     public void testPauseLeaderTimeout() {
-        pauseLeaderFunction = new Function<Runnable, Void>() {
+        pauseLeaderFunction = input -> null;
+        setup("testPauseLeaderTimeout");
+
+        Leader leader = new Leader(mockRaftActor.getRaftActorContext()) {
             @Override
-            public Void apply(Runnable input) {
-                return null;
+            public void transferLeadership(RaftActorLeadershipTransferCohort leadershipTransferCohort) {
+                leadershipTransferCohort.transferComplete();
             }
         };
+        mockRaftActor.setCurrentBehavior(leader);
 
-        setup("testPauseLeaderTimeout");
         cohort.init();
-        verify(onComplete, timeout(2000)).onFailure(mockRaftActor.self(), null);
+        verify(onComplete, timeout(2000)).onSuccess(mockRaftActor.self());
     }
 }