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 5680a0b..44b0d2b 100644 (file)
@@ -11,10 +11,14 @@ 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;
 
 /**
  * Unit tests for RaftActorLeadershipTransferCohort.
@@ -26,59 +30,78 @@ public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
     private MockRaftActor mockRaftActor;
     private RaftActorLeadershipTransferCohort cohort;
     private final OnComplete onComplete = mock(OnComplete.class);
-    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
+    private final DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
+    private Function<Runnable, Void> pauseLeaderFunction;
 
     @After
     public void tearDown() {
         factory.close();
     }
 
-    private void setup() {
-        String persistenceId = factory.generateActorId("leader-");
-        mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(
-                config).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId).underlyingActor();
-        cohort = new RaftActorLeadershipTransferCohort(mockRaftActor, null);
+    private void setup(String testName) {
+        String persistenceId = factory.generateActorId(testName + "-leader-");
+        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);
         cohort.addOnComplete(onComplete);
         mockRaftActor.waitForInitializeBehaviorComplete();
     }
 
     @Test
     public void testOnNewLeader() {
-        setup();
+        setup("testOnNewLeader");
         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
     public void testNewLeaderTimeout() {
-        setup();
+        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 testNotLeaderOnRun() {
-        config.setElectionTimeoutFactor(10000);
-        setup();
-        cohort.run();
-        verify(onComplete).onSuccess(mockRaftActor.self(), null);
+    public void testNotLeaderOnDoTransfer() {
+        setup("testNotLeaderOnDoTransfer");
+        cohort.doTransfer();
+        verify(onComplete).onSuccess(mockRaftActor.self());
     }
 
     @Test
     public void testAbortTransfer() {
-        setup();
+        setup("testAbortTransfer");
         cohort.abortTransfer();
-        verify(onComplete).onFailure(mockRaftActor.self(), null);
+        verify(onComplete).onFailure(mockRaftActor.self());
+    }
+
+    @Test
+    public void testPauseLeaderTimeout() {
+        pauseLeaderFunction = input -> null;
+        setup("testPauseLeaderTimeout");
+
+        Leader leader = new Leader(mockRaftActor.getRaftActorContext()) {
+            @Override
+            public void transferLeadership(RaftActorLeadershipTransferCohort leadershipTransferCohort) {
+                leadershipTransferCohort.transferComplete();
+            }
+        };
+        mockRaftActor.setCurrentBehavior(leader);
+
+        cohort.init();
+        verify(onComplete, timeout(2000)).onSuccess(mockRaftActor.self());
     }
 }