Make private methods static
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / FollowerLogInformationImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import com.google.common.base.Stopwatch;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.raft.FollowerLogInformation.FollowerState;
17 import scala.concurrent.duration.FiniteDuration;
18
19 public class FollowerLogInformationImplTest {
20
21     @Test
22     public void testIsFollowerActive() {
23
24         MockRaftActorContext context = new MockRaftActorContext();
25         context.setCommitIndex(10);
26
27         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
28         configParams.setHeartBeatInterval(new FiniteDuration(500, TimeUnit.MILLISECONDS));
29         configParams.setElectionTimeoutFactor(1);
30         context.setConfigParams(configParams);
31
32         FollowerLogInformation followerLogInformation =
33                 new FollowerLogInformationImpl("follower1", 9, context);
34
35         assertFalse("Follower should be termed inactive before stopwatch starts",
36                 followerLogInformation.isFollowerActive());
37
38         followerLogInformation.markFollowerActive();
39         if (sleepWithElaspsedTimeReturned(200) > 200) {
40             return;
41         }
42         assertTrue("Follower should be active", followerLogInformation.isFollowerActive());
43
44         if (sleepWithElaspsedTimeReturned(400) > 400) {
45             return;
46         }
47         assertFalse("Follower should be inactive after time lapsed",
48                 followerLogInformation.isFollowerActive());
49
50         followerLogInformation.markFollowerActive();
51         assertTrue("Follower should be active from inactive",
52                 followerLogInformation.isFollowerActive());
53     }
54
55     // we cannot rely comfortably that the sleep will indeed sleep for the desired time
56     // hence getting the actual elapsed time and do a match.
57     // if the sleep has spilled over, then return the test gracefully
58     private static long sleepWithElaspsedTimeReturned(long millis) {
59         Stopwatch stopwatch = Stopwatch.createStarted();
60         Uninterruptibles.sleepUninterruptibly(millis, TimeUnit.MILLISECONDS);
61         stopwatch.stop();
62         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
63     }
64
65     @Test
66     public void testOkToReplicate(){
67         MockRaftActorContext context = new MockRaftActorContext();
68         FollowerLogInformation followerLogInformation =
69                 new FollowerLogInformationImpl(
70                         "follower1", 10, context);
71
72         assertTrue(followerLogInformation.okToReplicate());
73         assertFalse(followerLogInformation.okToReplicate());
74
75         // wait for 150 milliseconds and it should work again
76         Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
77         assertTrue(followerLogInformation.okToReplicate());
78
79         //increment next index and try immediately and it should work again
80         followerLogInformation.incrNextIndex();
81         assertTrue(followerLogInformation.okToReplicate());
82     }
83
84     @Test
85     public void testVotingNotInitializedState() {
86         MockRaftActorContext context = new MockRaftActorContext();
87         FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl("follower1", -1, context);
88
89         followerLogInformation.setFollowerState(FollowerState.VOTING_NOT_INITIALIZED);
90         assertFalse(followerLogInformation.okToReplicate());
91         assertFalse(followerLogInformation.canParticipateInConsensus());
92
93         followerLogInformation.markFollowerActive();
94         assertFalse(followerLogInformation.isFollowerActive());
95
96         followerLogInformation.setFollowerState(FollowerState.VOTING);
97         assertTrue(followerLogInformation.okToReplicate());
98         assertTrue(followerLogInformation.canParticipateInConsensus());
99
100         followerLogInformation.markFollowerActive();
101         assertTrue(followerLogInformation.isFollowerActive());
102     }
103
104     @Test
105     public void testNonVotingState() {
106         MockRaftActorContext context = new MockRaftActorContext();
107         FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl("follower1", -1, context);
108
109         followerLogInformation.setFollowerState(FollowerState.NON_VOTING);
110         assertTrue(followerLogInformation.okToReplicate());
111         assertFalse(followerLogInformation.canParticipateInConsensus());
112
113         followerLogInformation.markFollowerActive();
114         assertTrue(followerLogInformation.isFollowerActive());
115     }
116 }