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.assertFalse;
11 import static org.junit.Assert.assertTrue;
13 import com.google.common.base.Stopwatch;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import java.util.concurrent.TimeUnit;
16 import org.junit.Test;
17 import scala.concurrent.duration.FiniteDuration;
19 public class FollowerLogInformationImplTest {
22 public void testIsFollowerActive() {
24 MockRaftActorContext context = new MockRaftActorContext();
25 context.setCommitIndex(10);
27 DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
28 configParams.setHeartBeatInterval(new FiniteDuration(500, TimeUnit.MILLISECONDS));
29 configParams.setElectionTimeoutFactor(1);
30 context.setConfigParams(configParams);
32 FollowerLogInformation followerLogInformation =
33 new FollowerLogInformationImpl(new PeerInfo("follower1", null, VotingState.VOTING), 9, context);
35 assertFalse("Follower should be termed inactive before stopwatch starts",
36 followerLogInformation.isFollowerActive());
38 followerLogInformation.markFollowerActive();
39 if (sleepWithElaspsedTimeReturned(200) > 200) {
42 assertTrue("Follower should be active", followerLogInformation.isFollowerActive());
44 if (sleepWithElaspsedTimeReturned(400) > 400) {
47 assertFalse("Follower should be inactive after time lapsed",
48 followerLogInformation.isFollowerActive());
50 followerLogInformation.markFollowerActive();
51 assertTrue("Follower should be active from inactive",
52 followerLogInformation.isFollowerActive());
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);
62 return stopwatch.elapsed(TimeUnit.MILLISECONDS);
66 public void testOkToReplicate() {
67 MockRaftActorContext context = new MockRaftActorContext();
68 context.setCommitIndex(0);
69 FollowerLogInformation followerLogInformation =
70 new FollowerLogInformationImpl(new PeerInfo("follower1", null, VotingState.VOTING), 10, context);
72 assertTrue(followerLogInformation.okToReplicate());
73 assertFalse(followerLogInformation.okToReplicate());
75 // wait for 150 milliseconds and it should work again
76 Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
77 assertTrue(followerLogInformation.okToReplicate());
79 //increment next index and try immediately and it should work again
80 followerLogInformation.incrNextIndex();
81 assertTrue(followerLogInformation.okToReplicate());
85 public void testVotingNotInitializedState() {
86 final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.VOTING_NOT_INITIALIZED);
87 MockRaftActorContext context = new MockRaftActorContext();
88 context.setCommitIndex(0);
89 FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl(peerInfo, -1, context);
91 assertFalse(followerLogInformation.okToReplicate());
93 followerLogInformation.markFollowerActive();
94 assertFalse(followerLogInformation.isFollowerActive());
96 peerInfo.setVotingState(VotingState.VOTING);
97 assertTrue(followerLogInformation.okToReplicate());
99 followerLogInformation.markFollowerActive();
100 assertTrue(followerLogInformation.isFollowerActive());
104 public void testNonVotingState() {
105 final PeerInfo peerInfo = new PeerInfo("follower1", null, VotingState.NON_VOTING);
106 MockRaftActorContext context = new MockRaftActorContext();
107 context.setCommitIndex(0);
108 FollowerLogInformation followerLogInformation = new FollowerLogInformationImpl(peerInfo, -1, context);
110 assertTrue(followerLogInformation.okToReplicate());
112 followerLogInformation.markFollowerActive();
113 assertTrue(followerLogInformation.isFollowerActive());