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; |
| 23 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.*; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 25 | import static org.onlab.junit.TestTools.delay; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 26 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Tests the operation of the accumulator. |
| 29 | */ |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 30 | public class AbstractAccumulatorTest { |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 31 | |
| 32 | private final Timer timer = new Timer(); |
| 33 | |
| 34 | @Test |
| 35 | public void basics() throws Exception { |
| 36 | TestAccumulator accumulator = new TestAccumulator(); |
| 37 | assertEquals("incorrect timer", timer, accumulator.timer()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 38 | assertEquals("incorrect max events", 5, accumulator.maxItems()); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 39 | assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis()); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 40 | assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis()); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Jonathan Hart | 49f0920 | 2015-07-15 15:15:06 -0700 | [diff] [blame] | 43 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 44 | @Test |
| 45 | public void eventTrigger() { |
| 46 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 47 | accumulator.add(new TestItem("a")); |
| 48 | accumulator.add(new TestItem("b")); |
| 49 | accumulator.add(new TestItem("c")); |
| 50 | accumulator.add(new TestItem("d")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 51 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 52 | accumulator.add(new TestItem("e")); |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame] | 53 | delay(20); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 54 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 55 | assertEquals("incorrect batch", "abcde", accumulator.batch); |
| 56 | } |
| 57 | |
Yuta HIGUCHI | 38935e5 | 2014-10-10 13:27:46 -0700 | [diff] [blame] | 58 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 59 | @Test |
| 60 | public void timeTrigger() { |
| 61 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 62 | accumulator.add(new TestItem("a")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 63 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 64 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 65 | accumulator.add(new TestItem("b")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 66 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 67 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 68 | accumulator.add(new TestItem("c")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 69 | delay(30); |
| 70 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 71 | accumulator.add(new TestItem("d")); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 72 | delay(60); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 73 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 74 | assertEquals("incorrect batch", "abcd", accumulator.batch); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jonathan Hart | 49f0920 | 2015-07-15 15:15:06 -0700 | [diff] [blame] | 77 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 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")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 84 | delay(80); |
| 85 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 86 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 87 | } |
| 88 | |
Jonathan Hart | 49f0920 | 2015-07-15 15:15:06 -0700 | [diff] [blame] | 89 | @Ignore("FIXME: timing sensitive test failing randomly.") |
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")); |
| 97 | delay(80); |
| 98 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 99 | accumulator.ready = true; |
| 100 | delay(80); |
| 101 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 102 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 103 | } |
| 104 | |
Jonathan Hart | 49f0920 | 2015-07-15 15:15:06 -0700 | [diff] [blame] | 105 | @Ignore("FIXME: timing sensitive test failing randomly.") |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 106 | @Test |
| 107 | public void readyLongTrigger() { |
| 108 | TestAccumulator accumulator = new TestAccumulator(); |
| 109 | accumulator.ready = false; |
| 110 | delay(120); |
| 111 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 112 | accumulator.add(new TestItem("a")); |
| 113 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 114 | accumulator.ready = true; |
| 115 | delay(80); |
| 116 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 117 | assertEquals("incorrect batch", "a", accumulator.batch); |
| 118 | } |
| 119 | |
alshabib | ea74cc9 | 2015-07-07 10:38:21 -0700 | [diff] [blame] | 120 | @Ignore("FIXME: timing sensitive test failing randomly.") |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 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")); |
| 134 | delay(5); |
| 135 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 136 | assertEquals("incorrect batch", "abcdefg", accumulator.batch); |
| 137 | } |
| 138 | |
| 139 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 140 | private class TestItem { |
| 141 | private final String s; |
| 142 | |
| 143 | public TestItem(String s) { |
| 144 | this.s = s; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private class TestAccumulator extends AbstractAccumulator<TestItem> { |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 149 | |
| 150 | String batch = ""; |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 151 | boolean ready = true; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 152 | |
| 153 | protected TestAccumulator() { |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 154 | super(timer, 5, 100, 70); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 158 | public void processItems(List<TestItem> items) { |
| 159 | for (TestItem item : items) { |
| 160 | batch += item.s; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public boolean isReady() { |
| 166 | return ready; |
| 167 | } |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 168 | } |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 169 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 170 | } |