Eliminate use of deprecated mockito methods
[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(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         assertTrue(followerLogInformation.okToReplicate());
74         assertFalse(followerLogInformation.okToReplicate());
75
76         // wait for 150 milliseconds and it should work again
77         Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
78         assertTrue(followerLogInformation.okToReplicate());
79
80         //increment next index and try immediately and it should work again
81         followerLogInformation.incrNextIndex();
82         assertTrue(followerLogInformation.okToReplicate());
83     }
84
85     @Test
86     public void testVotingNotInitializedState() {
87         final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.VOTING_NOT_INITIALIZED);
88         MockRaftActorContext context = new MockRaftActorContext();
89         context.setCommitIndex(0);
90         FollowerLogInformation followerLogInformation = new FollowerLogInformation(peerInfo, context);
91
92         assertFalse(followerLogInformation.okToReplicate());
93
94         followerLogInformation.markFollowerActive();
95         assertFalse(followerLogInformation.isFollowerActive());
96
97         peerInfo.setVotingState(VotingState.VOTING);
98         assertTrue(followerLogInformation.okToReplicate());
99
100         followerLogInformation.markFollowerActive();
101         assertTrue(followerLogInformation.isFollowerActive());
102     }
103
104     @Test
105     public void testNonVotingState() {
106         final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.NON_VOTING);
107         MockRaftActorContext context = new MockRaftActorContext();
108         context.setCommitIndex(0);
109         FollowerLogInformation followerLogInformation = new FollowerLogInformation(peerInfo, context);
110
111         assertTrue(followerLogInformation.okToReplicate());
112
113         followerLogInformation.markFollowerActive();
114         assertTrue(followerLogInformation.isFollowerActive());
115     }
116
117     @Test
118     public void testDecrNextIndex() {
119         MockRaftActorContext context = new MockRaftActorContext();
120         context.setCommitIndex(1);
121         FollowerLogInformation followerLogInformation =
122                 new FollowerLogInformation(new PeerInfo("follower1", null, VotingState.VOTING), 1, context);
123
124         assertTrue(followerLogInformation.decrNextIndex());
125         assertEquals("getNextIndex", 0, followerLogInformation.getNextIndex());
126
127         assertTrue(followerLogInformation.decrNextIndex());
128         assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());
129
130         assertFalse(followerLogInformation.decrNextIndex());
131         assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());
132     }
133 }