blob: 124a571bb7f9ea83f6e872957438a33add37ba0a [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
Sonal Muthal6cc2e0b2017-06-26 15:26:19 -070020import com.google.common.testing.EqualsTester;
21
toma7083182014-09-25 21:38:03 -070022import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertTrue;
24import static org.onlab.junit.TestTools.delay;
25
26/**
27 * Tests of the Counter utility.
28 */
29public class CounterTest {
30
31 @Test
32 public void basics() {
33 Counter tt = new Counter();
34 assertEquals("incorrect number of bytes", 0L, tt.total());
35 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
36 tt.add(1234567890L);
37 assertEquals("incorrect number of bytes", 1234567890L, tt.total());
38 assertTrue("incorrect throughput", 1234567890.0 < tt.throughput());
39 delay(1500);
40 tt.add(1L);
41 assertEquals("incorrect number of bytes", 1234567891L, tt.total());
42 assertTrue("incorrect throughput", 1234567891.0 > tt.throughput());
43 tt.reset();
44 assertEquals("incorrect number of bytes", 0L, tt.total());
45 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
46 }
47
48 @Test
49 public void freeze() {
HIGUCHI Yutaf96f7cf2016-05-21 12:32:31 -070050 long now = System.currentTimeMillis();
51 Counter tt = new Counter(now, 123L, now + 1000);
toma7083182014-09-25 21:38:03 -070052 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 }
Sonal Muthal6cc2e0b2017-06-26 15:26:19 -070086
87 @Test
88 public void equals() {
89 long start = 100L;
90 long total = 300L;
91 long end = 200L;
92 Counter tt = new Counter(start, total, end);
93 Counter same = new Counter(start, total, end);
94 Counter diff = new Counter(300L, 700L, 400L);
95 new EqualsTester()
96 .addEqualityGroup(tt, same)
97 .addEqualityGroup(400)
98 .addEqualityGroup("")
99 .addEqualityGroup(diff);
100
101 }
102
103 @Test
104 public void toStringTest() {
105 Counter tt = new Counter(100L, 300L, 200L);
106 assertEquals("Counter{total=300, start=100, end=200}", tt.toString());
107 Counter another = new Counter(200L, 500L, 300L);
108 assertEquals("Counter{total=500, start=200, end=300}", another.toString());
109 }
toma7083182014-09-25 21:38:03 -0700110}