Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
Sonal Muthal | 6cc2e0b | 2017-06-26 15:26:19 -0700 | [diff] [blame] | 20 | import com.google.common.testing.EqualsTester; |
| 21 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.onlab.junit.TestTools.delay; |
| 25 | |
| 26 | /** |
| 27 | * Tests of the Counter utility. |
| 28 | */ |
| 29 | public 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 Yuta | f96f7cf | 2016-05-21 12:32:31 -0700 | [diff] [blame] | 50 | long now = System.currentTimeMillis(); |
| 51 | Counter tt = new Counter(now, 123L, now + 1000); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 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 | } |
Sonal Muthal | 6cc2e0b | 2017-06-26 15:26:19 -0700 | [diff] [blame] | 86 | |
| 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 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 110 | } |