blob: d30e1b59c70e757c9b981490e4ca6e6acffef931 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
toma7083182014-09-25 21:38:03 -070016package org.onlab.util;
17
18import org.junit.Test;
19
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertTrue;
22import static org.onlab.junit.TestTools.delay;
23
24/**
25 * Tests of the Counter utility.
26 */
27public class CounterTest {
28
29 @Test
30 public void basics() {
31 Counter tt = new Counter();
32 assertEquals("incorrect number of bytes", 0L, tt.total());
33 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
34 tt.add(1234567890L);
35 assertEquals("incorrect number of bytes", 1234567890L, tt.total());
36 assertTrue("incorrect throughput", 1234567890.0 < tt.throughput());
37 delay(1500);
38 tt.add(1L);
39 assertEquals("incorrect number of bytes", 1234567891L, tt.total());
40 assertTrue("incorrect throughput", 1234567891.0 > tt.throughput());
41 tt.reset();
42 assertEquals("incorrect number of bytes", 0L, tt.total());
43 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
44 }
45
46 @Test
47 public void freeze() {
48 Counter tt = new Counter();
49 tt.add(123L);
50 assertEquals("incorrect number of bytes", 123L, tt.total());
51 delay(1000);
52 tt.freeze();
53 tt.add(123L);
54 assertEquals("incorrect number of bytes", 123L, tt.total());
55
56 double d = tt.duration();
57 double t = tt.throughput();
58 assertEquals("incorrect duration", d, tt.duration(), 0.0001);
59 assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
60 assertEquals("incorrect number of bytes", 123L, tt.total());
61 }
62
63 @Test
64 public void reset() {
65 Counter tt = new Counter();
66 tt.add(123L);
67 assertEquals("incorrect number of bytes", 123L, tt.total());
68
69 double d = tt.duration();
70 double t = tt.throughput();
71 assertEquals("incorrect duration", d, tt.duration(), 0.0001);
72 assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
73 assertEquals("incorrect number of bytes", 123L, tt.total());
74
75 tt.reset();
76 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
77 assertEquals("incorrect number of bytes", 0, tt.total());
78 }
79
80 @Test
81 public void syntheticTracker() {
82 Counter tt = new Counter(5000, 1000, 6000);
83 assertEquals("incorrect duration", 1, tt.duration(), 0.1);
84 assertEquals("incorrect throughput", 1000, tt.throughput(), 1.0);
85 }
86}