Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
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 | |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.onlab.junit.TestTools.delay; |
| 23 | |
| 24 | /** |
| 25 | * Tests of the Counter utility. |
| 26 | */ |
| 27 | public 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() { |
| 48 | Counter tt = new Counter(); |
| 49 | tt.add(123L); |
| 50 | assertEquals("incorrect number of bytes", 123L, tt.total()); |
| 51 | delay(1000); |
| 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 | } |
| 86 | } |