2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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;
20 public class FollowerLogInformationTest {
23 public void testIsFollowerActive() {
25 MockRaftActorContext context = new MockRaftActorContext();
26 context.setCommitIndex(10);
28 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
29 configParams.setHeartBeatInterval(new FiniteDuration(500, TimeUnit.MILLISECONDS));
30 configParams.setElectionTimeoutFactor(1);
31 context.setConfigParams(configParams);
33 FollowerLogInformation followerLogInformation =
34 new FollowerLogInformation(new PeerInfo("follower1", null, VotingState.VOTING), 9, context);
36 assertFalse("Follower should be termed inactive before stopwatch starts",
37 followerLogInformation.isFollowerActive());
39 followerLogInformation.markFollowerActive();
40 if (sleepWithElaspsedTimeReturned(200) > 200) {
43 assertTrue("Follower should be active", followerLogInformation.isFollowerActive());
45 if (sleepWithElaspsedTimeReturned(400) > 400) {
48 assertFalse("Follower should be inactive after time lapsed",
49 followerLogInformation.isFollowerActive());
51 followerLogInformation.markFollowerActive();
52 assertTrue("Follower should be active from inactive",
53 followerLogInformation.isFollowerActive());
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);
63 return stopwatch.elapsed(TimeUnit.MILLISECONDS);
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);
73 followerLogInformation.setSentCommitIndex(0);
74 assertTrue(followerLogInformation.okToReplicate(0));
75 assertFalse(followerLogInformation.okToReplicate(0));
77 // wait for 150 milliseconds and it should work again
78 Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
79 assertTrue(followerLogInformation.okToReplicate(0));
81 //increment next index and try immediately and it should work again
82 followerLogInformation.incrNextIndex();
83 assertTrue(followerLogInformation.okToReplicate(0));
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);
93 assertFalse(followerLogInformation.okToReplicate(0));
95 followerLogInformation.markFollowerActive();
96 assertFalse(followerLogInformation.isFollowerActive());
98 peerInfo.setVotingState(VotingState.VOTING);
99 assertTrue(followerLogInformation.okToReplicate(0));
101 followerLogInformation.markFollowerActive();
102 assertTrue(followerLogInformation.isFollowerActive());
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);
112 assertTrue(followerLogInformation.okToReplicate(0));
114 followerLogInformation.markFollowerActive();
115 assertTrue(followerLogInformation.isFollowerActive());
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);
125 assertTrue(followerLogInformation.decrNextIndex(1));
126 assertEquals("getNextIndex", 0, followerLogInformation.getNextIndex());
128 assertTrue(followerLogInformation.decrNextIndex(1));
129 assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());
131 assertFalse(followerLogInformation.decrNextIndex(1));
132 assertEquals("getNextIndex", -1, followerLogInformation.getNextIndex());