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