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