blob: adb0e958aae53a8edb1c46420ea36b9b4f9d7fe7 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 Vachuskaecb63c52015-02-19 10:03:48 -080016package org.onlab.util;
tom931af4e2014-09-13 12:00:57 -070017
Jonathan Hartecaa8a82015-02-04 09:44:08 -080018import org.junit.Test;
19
20import java.util.List;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -070021import java.util.stream.IntStream;
Jonathan Hartecaa8a82015-02-04 09:44:08 -080022
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070023import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertTrue;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -070026import static org.onlab.junit.TestTools.assertAfter;
tom931af4e2014-09-13 12:00:57 -070027
tom931af4e2014-09-13 12:00:57 -070028/**
29 * Tests the operation of the accumulator.
30 */
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080031public class AbstractAccumulatorTest {
tom931af4e2014-09-13 12:00:57 -070032
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070033
Aaron Kruglikovf27dba62015-10-27 15:06:29 -070034 private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer(true);
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070035
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070036 private static final int LONG_REAL_TIME_DELAY = 30;
37 private static final int SHORT_REAL_TIME_DELAY = 5;
38
tom931af4e2014-09-13 12:00:57 -070039
40 @Test
41 public void basics() throws Exception {
42 TestAccumulator accumulator = new TestAccumulator();
43 assertEquals("incorrect timer", timer, accumulator.timer());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080044 assertEquals("incorrect max events", 5, accumulator.maxItems());
tom931af4e2014-09-13 12:00:57 -070045 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
Thomas Vachuska75af68a2015-02-22 12:13:52 -080046 assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis());
tom931af4e2014-09-13 12:00:57 -070047 }
48
49 @Test
50 public void eventTrigger() {
51 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080052 accumulator.add(new TestItem("a"));
53 accumulator.add(new TestItem("b"));
54 accumulator.add(new TestItem("c"));
55 accumulator.add(new TestItem("d"));
tom931af4e2014-09-13 12:00:57 -070056 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080057 accumulator.add(new TestItem("e"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070058 timer.advanceTimeMillis(20, LONG_REAL_TIME_DELAY);
tom931af4e2014-09-13 12:00:57 -070059 assertFalse("should have fired", accumulator.batch.isEmpty());
60 assertEquals("incorrect batch", "abcde", accumulator.batch);
61 }
62
63 @Test
64 public void timeTrigger() {
65 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080066 accumulator.add(new TestItem("a"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070067 timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
tom931af4e2014-09-13 12:00:57 -070068 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080069 accumulator.add(new TestItem("b"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070070 timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
tom931af4e2014-09-13 12:00:57 -070071 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080072 accumulator.add(new TestItem("c"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070073 timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
tom093340b2014-10-10 00:15:36 -070074 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080075 accumulator.add(new TestItem("d"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070076 timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY);
tom931af4e2014-09-13 12:00:57 -070077 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070078 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070079 }
80
81 @Test
82 public void idleTrigger() {
83 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080084 accumulator.add(new TestItem("a"));
tom931af4e2014-09-13 12:00:57 -070085 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080086 accumulator.add(new TestItem("b"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070087 timer.advanceTimeMillis(70, LONG_REAL_TIME_DELAY);
tom931af4e2014-09-13 12:00:57 -070088 assertFalse("should have fired", accumulator.batch.isEmpty());
89 assertEquals("incorrect batch", "ab", accumulator.batch);
90 }
91
Thomas Vachuska75af68a2015-02-22 12:13:52 -080092 @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"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -070099 timer.advanceTimeMillis(80, SHORT_REAL_TIME_DELAY);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800100 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
101 accumulator.ready = true;
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -0700102 timer.advanceTimeMillis(80, LONG_REAL_TIME_DELAY);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800103 assertFalse("should have fired", accumulator.batch.isEmpty());
104 assertEquals("incorrect batch", "ab", accumulator.batch);
105 }
106
107 @Test
108 public void readyLongTrigger() {
109 TestAccumulator accumulator = new TestAccumulator();
110 accumulator.ready = false;
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -0700111 timer.advanceTimeMillis(120, SHORT_REAL_TIME_DELAY);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800112 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
113 accumulator.add(new TestItem("a"));
114 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
115 accumulator.ready = true;
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -0700116 timer.advanceTimeMillis(120, LONG_REAL_TIME_DELAY);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800117 assertFalse("should have fired", accumulator.batch.isEmpty());
118 assertEquals("incorrect batch", "a", accumulator.batch);
119 }
120
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"));
Aaron Kruglikovb0ca8cf2015-10-27 10:33:41 -0700134 timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800135 assertFalse("should have fired", accumulator.batch.isEmpty());
136 assertEquals("incorrect batch", "abcdefg", accumulator.batch);
137 }
138
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700139 @Test
140 public void stormTest() {
141 TestAccumulator accumulator = new TestAccumulator();
142 IntStream.range(0, 1000).forEach(i -> accumulator.add(new TestItem("#" + i)));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -0700143 timer.advanceTimeMillis(1);
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700144 assertAfter(100, () -> assertEquals("wrong item count", 1000, accumulator.itemCount));
145 assertEquals("wrong batch count", 200, accumulator.batchCount);
146 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800147
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800148 private class TestItem {
149 private final String s;
150
151 public TestItem(String s) {
152 this.s = s;
153 }
154 }
155
156 private class TestAccumulator extends AbstractAccumulator<TestItem> {
tom931af4e2014-09-13 12:00:57 -0700157
158 String batch = "";
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800159 boolean ready = true;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700160 int batchCount = 0;
161 int itemCount = 0;
tom931af4e2014-09-13 12:00:57 -0700162
163 protected TestAccumulator() {
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800164 super(timer, 5, 100, 70);
tom931af4e2014-09-13 12:00:57 -0700165 }
166
167 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800168 public void processItems(List<TestItem> items) {
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700169 batchCount++;
170 itemCount += items.size();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800171 for (TestItem item : items) {
172 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700173 }
174 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800175
176 @Override
177 public boolean isReady() {
178 return ready;
179 }
tom931af4e2014-09-13 12:00:57 -0700180 }
tom931af4e2014-09-13 12:00:57 -0700181}