Merge "BUG 2317 : StatisticsManager does not unregister from yang notifications on...
[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
11 import com.google.common.base.Stopwatch;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import org.junit.Test;
14 import scala.concurrent.duration.FiniteDuration;
15
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicLong;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 public class FollowerLogInformationImplTest {
23
24     @Test
25     public void testIsFollowerActive() {
26
27         FiniteDuration timeoutDuration =
28             new FiniteDuration(500, TimeUnit.MILLISECONDS);
29
30         FollowerLogInformation followerLogInformation =
31             new FollowerLogInformationImpl(
32                 "follower1", new AtomicLong(10), new AtomicLong(9), timeoutDuration);
33
34
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 long sleepWithElaspsedTimeReturned(long millis) {
60         Stopwatch stopwatch = new Stopwatch();
61         stopwatch.start();
62         Uninterruptibles.sleepUninterruptibly(millis, TimeUnit.MILLISECONDS);
63         stopwatch.stop();
64         return stopwatch.elapsed(TimeUnit.MILLISECONDS);
65     }
66 }