BUG-4083: Li: knob to disable statistics collecting
[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     @Test
31     public void testGetAverageTimeBetweenMarks() throws Exception {
32         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
33         timeCounter.markStart();
34         Assert.assertEquals(0, timeCounter.getAverageTimeBetweenMarks());
35
36         zzz(2L);
37         timeCounter.addTimeMark();
38         Assert.assertEquals(2, timeCounter.getAverageTimeBetweenMarks());
39
40         zzz(2L);
41         timeCounter.addTimeMark();
42         Assert.assertEquals(2, timeCounter.getAverageTimeBetweenMarks());
43
44         zzz(5L);
45         timeCounter.addTimeMark();
46         Assert.assertEquals(3, timeCounter.getAverageTimeBetweenMarks());
47     }
48
49     private void zzz(long length) {
50         try {
51             Thread.sleep(length);
52         } catch (InterruptedException e) {
53             LOG.error("processing sleep interrupted", e);
54         }
55     }
56 }