754e3e436be62e6d32fc2af72a2cc5dd96d4491d
[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 import akka.dispatch.Dispatchers;
15 import com.google.common.base.Function;
16 import org.junit.After;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort.OnComplete;
19 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
20
21 /**
22  * Unit tests for RaftActorLeadershipTransferCohort.
23  *
24  * @author Thomas Pantelis
25  */
26 public class RaftActorLeadershipTransferCohortTest extends AbstractActorTest {
27     private final TestActorFactory factory = new TestActorFactory(getSystem());
28     private MockRaftActor mockRaftActor;
29     private RaftActorLeadershipTransferCohort cohort;
30     private final OnComplete onComplete = mock(OnComplete.class);
31     private final DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
32     private Function<Runnable, Void> pauseLeaderFunction;
33
34     @After
35     public void tearDown() {
36         factory.close();
37     }
38
39     private void setup(String testName) {
40         String persistenceId = factory.generateActorId(testName + "-leader-");
41         mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(
42                 config).pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
43                 persistenceId).underlyingActor();
44         cohort = new RaftActorLeadershipTransferCohort(mockRaftActor);
45         cohort.addOnComplete(onComplete);
46         mockRaftActor.waitForInitializeBehaviorComplete();
47     }
48
49     @Test
50     public void testOnNewLeader() {
51         setup("testOnNewLeader");
52         cohort.setNewLeaderTimeoutInMillis(20000);
53
54         cohort.onNewLeader("new-leader");
55         verify(onComplete, never()).onSuccess(mockRaftActor.self());
56
57         cohort.transferComplete();
58
59         cohort.onNewLeader(null);
60         verify(onComplete, never()).onSuccess(mockRaftActor.self());
61
62         cohort.onNewLeader("new-leader");
63         verify(onComplete).onSuccess(mockRaftActor.self());
64     }
65
66     @Test
67     public void testNewLeaderTimeout() {
68         setup("testNewLeaderTimeout");
69         cohort.setNewLeaderTimeoutInMillis(200);
70         cohort.transferComplete();
71         verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self());
72     }
73
74     @Test
75     public void testNotLeaderOnDoTransfer() {
76         config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
77         setup("testNotLeaderOnDoTransfer");
78         cohort.doTransfer();
79         verify(onComplete).onSuccess(mockRaftActor.self());
80     }
81
82     @Test
83     public void testAbortTransfer() {
84         setup("testAbortTransfer");
85         cohort.abortTransfer();
86         verify(onComplete).onFailure(mockRaftActor.self());
87     }
88
89     @Test
90     public void testPauseLeaderTimeout() {
91         pauseLeaderFunction = new Function<Runnable, Void>() {
92             @Override
93             public Void apply(Runnable input) {
94                 return null;
95             }
96         };
97
98         setup("testPauseLeaderTimeout");
99         cohort.init();
100         verify(onComplete, timeout(2000)).onFailure(mockRaftActor.self());
101     }
102 }

©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.