Add missing license headers
[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() throws Exception {
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      *
36      * awaited average time:
37      * - tm1 = 2/1 = 2 ms
38      * - tm2 = 4/2 = 2 ms
39      * - tm3 = 9/3 = 3 ms
40      *
41      * But this times are only theoretical if whole test is executed without latency and atomically. Therefore awaited
42      * average times can't be compared to exact values of awaited average time (therefore == was replaced with >=)
43      * @throws Exception
44      */
45     @Test
46     public void testGetAverageTimeBetweenMarks() throws Exception {
47         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
48         timeCounter.markStart();
49         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
50
51         zzz(2L);
52         timeCounter.addTimeMark();
53         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 2);
54
55         zzz(2L);
56         timeCounter.addTimeMark();
57         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 2);
58
59         zzz(5L);
60         timeCounter.addTimeMark();
61         Assert.assertTrue(timeCounter.getAverageTimeBetweenMarks() >= 3);
62     }
63
64     private void zzz(long length) {
65         try {
66             Thread.sleep(length);
67         } catch (InterruptedException e) {
68             LOG.error("processing sleep interrupted", e);
69         }
70     }
71 }