9037f68b82b720b5e91d069066f36bfb27e576a7
[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.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.timeout;
15 import static org.mockito.Mockito.verify;
16 import akka.actor.ActorRef;
17 import akka.dispatch.Dispatchers;
18 import com.google.common.base.Function;
19 import org.junit.After;
20 import org.junit.Test;
21 import org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort.OnComplete;
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() {
42         doNothing().when(onComplete).onSuccess(any(ActorRef.class), any(ActorRef.class));
43         doNothing().when(onComplete).onFailure(any(ActorRef.class), any(ActorRef.class));
44
45         String persistenceId = factory.generateActorId("leader-");
46         mockRaftActor = factory.<MockRaftActor>createTestActor(MockRaftActor.builder().id(persistenceId).config(
47                 config).pauseLeaderFunction(pauseLeaderFunction).props().withDispatcher(Dispatchers.DefaultDispatcherId()),
48                 persistenceId).underlyingActor();
49         cohort = new RaftActorLeadershipTransferCohort(mockRaftActor, null);
50         cohort.addOnComplete(onComplete);
51         mockRaftActor.waitForInitializeBehaviorComplete();
52     }
53
54     @Test
55     public void testOnNewLeader() {
56         setup();
57         cohort.setNewLeaderTimeoutInMillis(20000);
58
59         cohort.onNewLeader("new-leader");
60         verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
61
62         cohort.transferComplete();
63
64         cohort.onNewLeader(null);
65         verify(onComplete, never()).onSuccess(mockRaftActor.self(), null);
66
67         cohort.onNewLeader("new-leader");
68         verify(onComplete).onSuccess(mockRaftActor.self(), null);
69     }
70
71     @Test
72     public void testNewLeaderTimeout() {
73         setup();
74         cohort.setNewLeaderTimeoutInMillis(200);
75         cohort.transferComplete();
76         verify(onComplete, timeout(3000)).onSuccess(mockRaftActor.self(), null);
77     }
78
79     @Test
80     public void testNotLeaderOnRun() {
81         config.setElectionTimeoutFactor(10000);
82         setup();
83         cohort.doTransfer();
84         verify(onComplete).onSuccess(mockRaftActor.self(), null);
85     }
86
87     @Test
88     public void testAbortTransfer() {
89         setup();
90         cohort.abortTransfer();
91         verify(onComplete).onFailure(mockRaftActor.self(), null);
92     }
93
94     @Test
95     public void testPauseLeaderTimeout() {
96         pauseLeaderFunction = new Function<Runnable, Void>() {
97             @Override
98             public Void apply(Runnable input) {
99                 return null;
100             }
101         };
102
103         setup();
104         cohort.init();
105         verify(onComplete, timeout(2000)).onFailure(mockRaftActor.self(), null);
106     }
107 }