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