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

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