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
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.never;
12 import static org.mockito.Mockito.timeout;
13 import static org.mockito.Mockito.verify;
14
15 import akka.dispatch.Dispatchers;
16 import com.google.common.base.Function;
17 import org.junit.After;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort.OnComplete;
20 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
21 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
22
23 /**
24  * Unit tests for RaftActorLeadershipTransferCohort.
25  *
26  * @author Thomas Pantelis
27  */
28 public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
29     private final TestActorFactory factory = new TestActorFactory(getSystem());
30     private MockRaftActor mockRaftActor;
31     private RaftActorLeadershipTransferCohort cohort;
32     private final OnComplete onComplete = mock(OnComplete.class);
33     private final DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
34     private Function<Runnable, Void> pauseLeaderFunction;
35
36     @After
37     public void tearDown() {
38         factory.close();
39     }
40
41     private void setup(String testName) {
42         String persistenceId = factory.generateActorId(testName + "-leader-");
43         config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
44         mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(config)
45                 .pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
46                 persistenceId).underlyingActor();
47         cohort = new RaftActorLeadershipTransferCohort(mockRaftActor);
48         cohort.addOnComplete(onComplete);
49         mockRaftActor.waitForInitializeBehaviorComplete();
50     }
51
52     @Test
53     public void testOnNewLeader() {
54         setup("testOnNewLeader");
55         cohort.setNewLeaderTimeoutInMillis(20000);
56
57         cohort.onNewLeader("new-leader");
58         verify(onComplete, never()).onSuccess(mockRaftActor.self());
59
60         cohort.transferComplete();
61
62         cohort.onNewLeader(null);
63         verify(onComplete, never()).onSuccess(mockRaftActor.self());
64
65         cohort.onNewLeader("new-leader");
66         verify(onComplete).onSuccess(mockRaftActor.self());
67     }
68
69     @Test
70     public void testNewLeaderTimeout() {
71         setup("testNewLeaderTimeout");
72         cohort.setNewLeaderTimeoutInMillis(200);
73         cohort.transferComplete();
74         verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self());
75     }
76
77     @Test
78     public void testNotLeaderOnDoTransfer() {
79         setup("testNotLeaderOnDoTransfer");
80         cohort.doTransfer();
81         verify(onComplete).onSuccess(mockRaftActor.self());
82     }
83
84     @Test
85     public void testAbortTransfer() {
86         setup("testAbortTransfer");
87         cohort.abortTransfer();
88         verify(onComplete).onFailure(mockRaftActor.self());
89     }
90
91     @Test
92     public void testPauseLeaderTimeout() {
93         pauseLeaderFunction = input -> null;
94         setup("testPauseLeaderTimeout");
95
96         Leader leader = new Leader(mockRaftActor.getRaftActorContext()) {
97             @Override
98             public void transferLeadership(RaftActorLeadershipTransferCohort leadershipTransferCohort) {
99                 leadershipTransferCohort.transferComplete();
100             }
101         };
102         mockRaftActor.setCurrentBehavior(leader);
103
104         cohort.init();
105         verify(onComplete, timeout(2000)).onSuccess(mockRaftActor.self());
106     }
107 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.