blob: c15cc8a69d53446bd37f4d9a95b6a442466a5c4f [file] [log] [blame]
Jonathan Hart233a18a2015-03-02 17:24:58 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.util;
17
18import org.junit.After;
19import org.junit.Before;
Ray Milkey0811bdd2015-03-11 10:21:55 -070020import org.junit.Ignore;
Jonathan Hart233a18a2015-03-02 17:24:58 -080021import org.junit.Test;
22
23import static junit.framework.TestCase.fail;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertTrue;
26
27/**
28 * Unit tests for the sliding window counter.
29 */
Ray Milkey0811bdd2015-03-11 10:21:55 -070030
31@Ignore("Disable these for now because of intermittent load related failures on Jenkins runs.")
Jonathan Hart233a18a2015-03-02 17:24:58 -080032public 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}