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