Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.util; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
Ray Milkey | 0811bdd | 2015-03-11 10:21:55 -0700 | [diff] [blame] | 20 | import org.junit.Ignore; |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | |
| 23 | import static junit.framework.TestCase.fail; |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | |
| 27 | /** |
| 28 | * Unit tests for the sliding window counter. |
| 29 | */ |
Ray Milkey | 0811bdd | 2015-03-11 10:21:55 -0700 | [diff] [blame] | 30 | |
| 31 | @Ignore("Disable these for now because of intermittent load related failures on Jenkins runs.") |
Jonathan Hart | 233a18a | 2015-03-02 17:24:58 -0800 | [diff] [blame] | 32 | public class SlidingWindowCounterTest { |
| 33 | |
| 34 | private SlidingWindowCounter counter; |
| 35 | |
| 36 | @Before |
| 37 | public void setUp() { |
| 38 | counter = new SlidingWindowCounter(2); |
| 39 | } |
| 40 | |
| 41 | @After |
| 42 | public void tearDown() { |
| 43 | counter.destroy(); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testIncrementCount() { |
| 48 | assertEquals(0, counter.get(1)); |
| 49 | assertEquals(0, counter.get(2)); |
| 50 | counter.incrementCount(); |
| 51 | assertEquals(1, counter.get(1)); |
| 52 | assertEquals(1, counter.get(2)); |
| 53 | counter.incrementCount(2); |
| 54 | assertEquals(3, counter.get(2)); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testSlide() { |
| 59 | counter.incrementCount(); |
| 60 | counter.advanceHead(); |
| 61 | assertEquals(0, counter.get(1)); |
| 62 | assertEquals(1, counter.get(2)); |
| 63 | counter.incrementCount(2); |
| 64 | assertEquals(2, counter.get(1)); |
| 65 | assertEquals(3, counter.get(2)); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testWrap() { |
| 70 | counter.incrementCount(); |
| 71 | counter.advanceHead(); |
| 72 | counter.incrementCount(2); |
| 73 | counter.advanceHead(); |
| 74 | assertEquals(0, counter.get(1)); |
| 75 | assertEquals(2, counter.get(2)); |
| 76 | counter.advanceHead(); |
| 77 | assertEquals(0, counter.get(1)); |
| 78 | assertEquals(0, counter.get(2)); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testCornerCases() { |
| 84 | try { |
| 85 | counter.get(3); |
| 86 | fail("Exception should have been thrown"); |
| 87 | } catch (IllegalArgumentException e) { |
| 88 | assertTrue(true); |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | new SlidingWindowCounter(0); |
| 93 | fail("Exception should have been thrown"); |
| 94 | } catch (IllegalArgumentException e) { |
| 95 | assertTrue(true); |
| 96 | } |
| 97 | |
| 98 | try { |
| 99 | new SlidingWindowCounter(-1); |
| 100 | fail("Exception should have been thrown"); |
| 101 | } catch (IllegalArgumentException e) { |
| 102 | assertTrue(true); |
| 103 | } |
| 104 | } |
| 105 | } |