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 | |
| 43 | @Test |
| 44 | public void eventTrigger() { |
| 45 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 46 | accumulator.add(new TestItem("a")); |
| 47 | accumulator.add(new TestItem("b")); |
| 48 | accumulator.add(new TestItem("c")); |
| 49 | accumulator.add(new TestItem("d")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 50 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 51 | accumulator.add(new TestItem("e")); |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame] | 52 | delay(20); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 53 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 54 | assertEquals("incorrect batch", "abcde", accumulator.batch); |
| 55 | } |
| 56 | |
Yuta HIGUCHI | 38935e5 | 2014-10-10 13:27:46 -0700 | [diff] [blame] | 57 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 58 | @Test |
| 59 | public void timeTrigger() { |
| 60 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 61 | accumulator.add(new TestItem("a")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 62 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 63 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 64 | accumulator.add(new TestItem("b")); |
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("c")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 68 | delay(30); |
| 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("d")); |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 71 | delay(60); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 72 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 73 | assertEquals("incorrect batch", "abcd", accumulator.batch); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void idleTrigger() { |
| 78 | TestAccumulator accumulator = new TestAccumulator(); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 79 | accumulator.add(new TestItem("a")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 80 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 81 | accumulator.add(new TestItem("b")); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 82 | delay(80); |
| 83 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 84 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 85 | } |
| 86 | |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 87 | @Test |
| 88 | public void readyIdleTrigger() { |
| 89 | TestAccumulator accumulator = new TestAccumulator(); |
| 90 | accumulator.ready = false; |
| 91 | accumulator.add(new TestItem("a")); |
| 92 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 93 | accumulator.add(new TestItem("b")); |
| 94 | delay(80); |
| 95 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 96 | accumulator.ready = true; |
| 97 | delay(80); |
| 98 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 99 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void readyLongTrigger() { |
| 104 | TestAccumulator accumulator = new TestAccumulator(); |
| 105 | accumulator.ready = false; |
| 106 | delay(120); |
| 107 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 108 | accumulator.add(new TestItem("a")); |
| 109 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 110 | accumulator.ready = true; |
| 111 | delay(80); |
| 112 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 113 | assertEquals("incorrect batch", "a", accumulator.batch); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void readyMaxTrigger() { |
| 118 | TestAccumulator accumulator = new TestAccumulator(); |
| 119 | accumulator.ready = false; |
| 120 | accumulator.add(new TestItem("a")); |
| 121 | accumulator.add(new TestItem("b")); |
| 122 | accumulator.add(new TestItem("c")); |
| 123 | accumulator.add(new TestItem("d")); |
| 124 | accumulator.add(new TestItem("e")); |
| 125 | accumulator.add(new TestItem("f")); |
| 126 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 127 | accumulator.ready = true; |
| 128 | accumulator.add(new TestItem("g")); |
| 129 | delay(5); |
| 130 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 131 | assertEquals("incorrect batch", "abcdefg", accumulator.batch); |
| 132 | } |
| 133 | |
| 134 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 135 | private class TestItem { |
| 136 | private final String s; |
| 137 | |
| 138 | public TestItem(String s) { |
| 139 | this.s = s; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private class TestAccumulator extends AbstractAccumulator<TestItem> { |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 144 | |
| 145 | String batch = ""; |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 146 | boolean ready = true; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 147 | |
| 148 | protected TestAccumulator() { |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 149 | super(timer, 5, 100, 70); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 153 | public void processItems(List<TestItem> items) { |
| 154 | for (TestItem item : items) { |
| 155 | batch += item.s; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Thomas Vachuska | 75af68a | 2015-02-22 12:13:52 -0800 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public boolean isReady() { |
| 161 | return ready; |
| 162 | } |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 163 | } |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 164 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 165 | } |