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