blob: 4b7c95416cc7ef9778f9b49a6f2ff8a369c9158f [file] [log] [blame]
toma7083182014-09-25 21:38:03 -07001package org.onlab.util;
2
3import org.junit.Test;
4
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertTrue;
7import static org.onlab.junit.TestTools.delay;
8
9/**
10 * Tests of the Counter utility.
11 */
12public class CounterTest {
13
14 @Test
15 public void basics() {
16 Counter tt = new Counter();
17 assertEquals("incorrect number of bytes", 0L, tt.total());
18 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
19 tt.add(1234567890L);
20 assertEquals("incorrect number of bytes", 1234567890L, tt.total());
21 assertTrue("incorrect throughput", 1234567890.0 < tt.throughput());
22 delay(1500);
23 tt.add(1L);
24 assertEquals("incorrect number of bytes", 1234567891L, tt.total());
25 assertTrue("incorrect throughput", 1234567891.0 > tt.throughput());
26 tt.reset();
27 assertEquals("incorrect number of bytes", 0L, tt.total());
28 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
29 }
30
31 @Test
32 public void freeze() {
33 Counter tt = new Counter();
34 tt.add(123L);
35 assertEquals("incorrect number of bytes", 123L, tt.total());
36 delay(1000);
37 tt.freeze();
38 tt.add(123L);
39 assertEquals("incorrect number of bytes", 123L, tt.total());
40
41 double d = tt.duration();
42 double t = tt.throughput();
43 assertEquals("incorrect duration", d, tt.duration(), 0.0001);
44 assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
45 assertEquals("incorrect number of bytes", 123L, tt.total());
46 }
47
48 @Test
49 public void reset() {
50 Counter tt = new Counter();
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 tt.reset();
61 assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
62 assertEquals("incorrect number of bytes", 0, tt.total());
63 }
64
65 @Test
66 public void syntheticTracker() {
67 Counter tt = new Counter(5000, 1000, 6000);
68 assertEquals("incorrect duration", 1, tt.duration(), 0.1);
69 assertEquals("incorrect throughput", 1000, tt.throughput(), 1.0);
70 }
71}