Merge "Startup arch - remove artifactId prefix from dir names."
[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 com.google.common.base.Stopwatch;
11 import com.google.common.util.concurrent.Uninterruptibles;
12 import org.junit.Test;
13 import scala.concurrent.duration.FiniteDuration;
14
15 import java.util.concurrent.TimeUnit;
16
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19
20 public class FollowerLogInformationImplTest {
21
22     @Test
23     public void testIsFollowerActive() {
24
25         FiniteDuration timeoutDuration =
26             new FiniteDuration(500, TimeUnit.MILLISECONDS);
27
28         FollowerLogInformation followerLogInformation =
29             new FollowerLogInformationImpl(
30                 "follower1", 10, 9, timeoutDuration);
31
32
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 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 }