Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 16 | package org.onlab.util; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame] | 18 | import org.junit.Test; |
| 19 | |
| 20 | import java.util.List; |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 21 | import java.util.stream.IntStream; |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame] | 22 | |
Aaron Kruglikov | 4ce0b04 | 2015-10-07 14:04:17 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 26 | import static org.onlab.junit.TestTools.assertAfter; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 27 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Tests the operation of the accumulator. |
| 30 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 31 | public class AbstractAccumulatorTest { |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 32 | |
Aaron Kruglikov | 4ce0b04 | 2015-10-07 14:04:17 -0700 | [diff] [blame] | 33 | |
Aaron Kruglikov | f27dba6 | 2015-10-27 15:06:29 -0700 | [diff] [blame] | 34 | private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer(true); |
Aaron Kruglikov | 4ce0b04 | 2015-10-07 14:04:17 -0700 | [diff] [blame] | 35 | |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 36 | private static final int LONG_REAL_TIME_DELAY = 30; |
| 37 | private static final int SHORT_REAL_TIME_DELAY = 5; |
| 38 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 39 | |
| 40 | @Test |
| 41 | public void basics() throws Exception { |
| 42 | TestAccumulator accumulator = new TestAccumulator(); |
| 43 | assertEquals("incorrect timer", timer, accumulator.timer()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 44 | assertEquals("incorrect max events", 5, accumulator.maxItems()); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 45 | assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis()); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 46 | assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis()); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void eventTrigger() { |
| 51 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 52 | accumulator.add(new TestItem("a")); |
| 53 | accumulator.add(new TestItem("b")); |
| 54 | accumulator.add(new TestItem("c")); |
| 55 | accumulator.add(new TestItem("d")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 56 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 57 | accumulator.add(new TestItem("e")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 58 | timer.advanceTimeMillis(20, LONG_REAL_TIME_DELAY); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 59 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 60 | assertEquals("incorrect batch", "abcde", accumulator.batch); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void timeTrigger() { |
| 65 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 66 | accumulator.add(new TestItem("a")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 67 | timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 68 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 69 | accumulator.add(new TestItem("b")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 70 | timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 71 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 72 | accumulator.add(new TestItem("c")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 73 | timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 74 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 75 | accumulator.add(new TestItem("d")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 76 | timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 77 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 78 | assertEquals("incorrect batch", "abcd", accumulator.batch); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void idleTrigger() { |
| 83 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 84 | accumulator.add(new TestItem("a")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 85 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 86 | accumulator.add(new TestItem("b")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 87 | timer.advanceTimeMillis(70, LONG_REAL_TIME_DELAY); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 88 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 89 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 90 | } |
| 91 | |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 92 | @Test |
| 93 | public void readyIdleTrigger() { |
| 94 | TestAccumulator accumulator = new TestAccumulator(); |
| 95 | accumulator.ready = false; |
| 96 | accumulator.add(new TestItem("a")); |
| 97 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 98 | accumulator.add(new TestItem("b")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 99 | timer.advanceTimeMillis(80, SHORT_REAL_TIME_DELAY); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 100 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 101 | accumulator.ready = true; |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 102 | timer.advanceTimeMillis(80, LONG_REAL_TIME_DELAY); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 103 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 104 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void readyLongTrigger() { |
| 109 | TestAccumulator accumulator = new TestAccumulator(); |
| 110 | accumulator.ready = false; |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 111 | timer.advanceTimeMillis(120, SHORT_REAL_TIME_DELAY); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 112 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 113 | accumulator.add(new TestItem("a")); |
| 114 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 115 | accumulator.ready = true; |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 116 | timer.advanceTimeMillis(120, LONG_REAL_TIME_DELAY); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 117 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 118 | assertEquals("incorrect batch", "a", accumulator.batch); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void readyMaxTrigger() { |
| 123 | TestAccumulator accumulator = new TestAccumulator(); |
| 124 | accumulator.ready = false; |
| 125 | accumulator.add(new TestItem("a")); |
| 126 | accumulator.add(new TestItem("b")); |
| 127 | accumulator.add(new TestItem("c")); |
| 128 | accumulator.add(new TestItem("d")); |
| 129 | accumulator.add(new TestItem("e")); |
| 130 | accumulator.add(new TestItem("f")); |
| 131 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 132 | accumulator.ready = true; |
| 133 | accumulator.add(new TestItem("g")); |
Aaron Kruglikov | b0ca8cf | 2015-10-27 10:33:41 -0700 | [diff] [blame] | 134 | timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 135 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 136 | assertEquals("incorrect batch", "abcdefg", accumulator.batch); |
| 137 | } |
| 138 | |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 139 | @Test |
| 140 | public void stormTest() { |
| 141 | TestAccumulator accumulator = new TestAccumulator(); |
| 142 | IntStream.range(0, 1000).forEach(i -> accumulator.add(new TestItem("#" + i))); |
Aaron Kruglikov | 4ce0b04 | 2015-10-07 14:04:17 -0700 | [diff] [blame] | 143 | timer.advanceTimeMillis(1); |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 144 | assertAfter(100, () -> assertEquals("wrong item count", 1000, accumulator.itemCount)); |
| 145 | assertEquals("wrong batch count", 200, accumulator.batchCount); |
| 146 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 147 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 148 | private class TestItem { |
| 149 | private final String s; |
| 150 | |
| 151 | public TestItem(String s) { |
| 152 | this.s = s; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private class TestAccumulator extends AbstractAccumulator<TestItem> { |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 157 | |
| 158 | String batch = ""; |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 159 | boolean ready = true; |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 160 | int batchCount = 0; |
| 161 | int itemCount = 0; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 162 | |
| 163 | protected TestAccumulator() { |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 164 | super(timer, 5, 100, 70); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 168 | public void processItems(List<TestItem> items) { |
Thomas Vachuska | a82341c | 2015-08-25 17:36:59 -0700 | [diff] [blame] | 169 | batchCount++; |
| 170 | itemCount += items.size(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 171 | for (TestItem item : items) { |
| 172 | batch += item.s; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 175 | |
| 176 | @Override |
| 177 | public boolean isReady() { |
| 178 | return ready; |
| 179 | } |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 180 | } |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 181 | } |