Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / TimeCounterTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Test for {@link TimeCounter}.
19  */
20 public class TimeCounterTest {
21
22     private static final Logger LOG = LoggerFactory.getLogger(TimeCounterTest.class);
23     private TimeCounter timeCounter;
24
25     @Before
26     public void setUp() {
27         timeCounter = new TimeCounter();
28     }
29
30     /**
31      * tm = time mark
32      * - tm1 at time 2 ms
33      * - tm2 at time 4 ms
34      * - tm3 at time 9 ms
35      * awaited average time:
36      * - tm1 = 2/1 = 2 ms
37      * - tm2 = 4/2 = 2 ms
38      * - tm3 = 9/3 = 3 ms
39      * But this times are only theoretical if whole test is executed without latency and atomically. Therefore awaited
40      * average times can't be compared to exact values of awaited average time (therefore == was replaced with >=)
41      * @throws Exception if interrupted
42      */
43     @Test
44     public void testGetAverageTimeBetweenMarks() {
45         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
46         timeCounter.markStart();
47         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
48
49         zzz(2L);
50         timeCounter.addTimeMark();
51         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 2);
52
53         zzz(2L);
54         timeCounter.addTimeMark();
55         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 2);
56
57         zzz(5L);
58         timeCounter.addTimeMark();
59         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 3);
60     }
61
62     private static void zzz(long length) {
63         try {
64             Thread.sleep(length);
65         } catch (InterruptedException e) {
66             LOG.error("processing sleep interrupted", e);
67         }
68     }
69 }