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