blob: cb085bbe42cba993b2470667b6f33b50dacd9c87 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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() {
HIGUCHI Yutaf96f7cf2016-05-21 12:32:31 -070048 long now = System.currentTimeMillis();
49 Counter tt = new Counter(now, 123L, now + 1000);
toma7083182014-09-25 21:38:03 -070050 tt.freeze();
51 tt.add(123L);
52 assertEquals("incorrect number of bytes", 123L, tt.total());
53
54 double d = tt.duration();
55 double t = tt.throughput();
56 assertEquals("incorrect duration", d, tt.duration(), 0.0001);
57 assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
58 assertEquals("incorrect number of bytes", 123L, tt.total());
59 }
60
61 @Test
62 public void reset() {
63 Counter tt = new Counter();
64 tt.add(123L);
65 assertEquals("incorrect number of bytes", 123L, tt.total());
66
67 double d = tt.duration();
68 double t = tt.throughput();
69 assertEquals("incorrect duration", d, tt.duration(), 0.0001);
70 assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
71 assertEquals("incorrect number of bytes", 123L, tt.total());
72
73 tt.reset();
74 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
75 assertEquals("incorrect number of bytes", 0, tt.total());
76 }
77
78 @Test
79 public void syntheticTracker() {
80 Counter tt = new Counter(5000, 1000, 6000);
81 assertEquals("incorrect duration", 1, tt.duration(), 0.1);
82 assertEquals("incorrect throughput", 1000, tt.throughput(), 1.0);
83 }
84}