blob: db7224ad492078221f6cb3d254c32c332a1994ee [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Thomas Vachuskaecb63c52015-02-19 10:03:48 -08002 * Copyright 2015 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
34 private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer();
35
tom931af4e2014-09-13 12:00:57 -070036
37 @Test
38 public void basics() throws Exception {
39 TestAccumulator accumulator = new TestAccumulator();
40 assertEquals("incorrect timer", timer, accumulator.timer());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080041 assertEquals("incorrect max events", 5, accumulator.maxItems());
tom931af4e2014-09-13 12:00:57 -070042 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
Thomas Vachuska75af68a2015-02-22 12:13:52 -080043 assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis());
tom931af4e2014-09-13 12:00:57 -070044 }
45
46 @Test
47 public void eventTrigger() {
48 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080049 accumulator.add(new TestItem("a"));
50 accumulator.add(new TestItem("b"));
51 accumulator.add(new TestItem("c"));
52 accumulator.add(new TestItem("d"));
tom931af4e2014-09-13 12:00:57 -070053 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080054 accumulator.add(new TestItem("e"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070055 timer.advanceTimeMillis(20, 10);
tom931af4e2014-09-13 12:00:57 -070056 assertFalse("should have fired", accumulator.batch.isEmpty());
57 assertEquals("incorrect batch", "abcde", accumulator.batch);
58 }
59
60 @Test
61 public void timeTrigger() {
62 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080063 accumulator.add(new TestItem("a"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070064 timer.advanceTimeMillis(30, 1);
tom931af4e2014-09-13 12:00:57 -070065 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080066 accumulator.add(new TestItem("b"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070067 timer.advanceTimeMillis(30, 1);
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("c"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070070 timer.advanceTimeMillis(30, 1);
tom093340b2014-10-10 00:15:36 -070071 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080072 accumulator.add(new TestItem("d"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070073 timer.advanceTimeMillis(10, 10);
tom931af4e2014-09-13 12:00:57 -070074 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070075 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070076 }
77
78 @Test
79 public void idleTrigger() {
80 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080081 accumulator.add(new TestItem("a"));
tom931af4e2014-09-13 12:00:57 -070082 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080083 accumulator.add(new TestItem("b"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070084 timer.advanceTimeMillis(70, 10);
tom931af4e2014-09-13 12:00:57 -070085 assertFalse("should have fired", accumulator.batch.isEmpty());
86 assertEquals("incorrect batch", "ab", accumulator.batch);
87 }
88
Thomas Vachuska75af68a2015-02-22 12:13:52 -080089 @Test
90 public void readyIdleTrigger() {
91 TestAccumulator accumulator = new TestAccumulator();
92 accumulator.ready = false;
93 accumulator.add(new TestItem("a"));
94 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
95 accumulator.add(new TestItem("b"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070096 timer.advanceTimeMillis(80, 1);
Thomas Vachuska75af68a2015-02-22 12:13:52 -080097 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
98 accumulator.ready = true;
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -070099 timer.advanceTimeMillis(80, 10);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800100 assertFalse("should have fired", accumulator.batch.isEmpty());
101 assertEquals("incorrect batch", "ab", accumulator.batch);
102 }
103
104 @Test
105 public void readyLongTrigger() {
106 TestAccumulator accumulator = new TestAccumulator();
107 accumulator.ready = false;
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -0700108 timer.advanceTimeMillis(120, 1);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800109 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
110 accumulator.add(new TestItem("a"));
111 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
112 accumulator.ready = true;
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -0700113 timer.advanceTimeMillis(120, 10);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800114 assertFalse("should have fired", accumulator.batch.isEmpty());
115 assertEquals("incorrect batch", "a", accumulator.batch);
116 }
117
118 @Test
119 public void readyMaxTrigger() {
120 TestAccumulator accumulator = new TestAccumulator();
121 accumulator.ready = false;
122 accumulator.add(new TestItem("a"));
123 accumulator.add(new TestItem("b"));
124 accumulator.add(new TestItem("c"));
125 accumulator.add(new TestItem("d"));
126 accumulator.add(new TestItem("e"));
127 accumulator.add(new TestItem("f"));
128 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
129 accumulator.ready = true;
130 accumulator.add(new TestItem("g"));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -0700131 timer.advanceTimeMillis(10, 10);
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800132 assertFalse("should have fired", accumulator.batch.isEmpty());
133 assertEquals("incorrect batch", "abcdefg", accumulator.batch);
134 }
135
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700136 @Test
137 public void stormTest() {
138 TestAccumulator accumulator = new TestAccumulator();
139 IntStream.range(0, 1000).forEach(i -> accumulator.add(new TestItem("#" + i)));
Aaron Kruglikov4ce0b042015-10-07 14:04:17 -0700140 timer.advanceTimeMillis(1);
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700141 assertAfter(100, () -> assertEquals("wrong item count", 1000, accumulator.itemCount));
142 assertEquals("wrong batch count", 200, accumulator.batchCount);
143 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800144
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800145 private class TestItem {
146 private final String s;
147
148 public TestItem(String s) {
149 this.s = s;
150 }
151 }
152
153 private class TestAccumulator extends AbstractAccumulator<TestItem> {
tom931af4e2014-09-13 12:00:57 -0700154
155 String batch = "";
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800156 boolean ready = true;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700157 int batchCount = 0;
158 int itemCount = 0;
tom931af4e2014-09-13 12:00:57 -0700159
160 protected TestAccumulator() {
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800161 super(timer, 5, 100, 70);
tom931af4e2014-09-13 12:00:57 -0700162 }
163
164 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800165 public void processItems(List<TestItem> items) {
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700166 batchCount++;
167 itemCount += items.size();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800168 for (TestItem item : items) {
169 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700170 }
171 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800172
173 @Override
174 public boolean isReady() {
175 return ready;
176 }
tom931af4e2014-09-13 12:00:57 -0700177 }
tom931af4e2014-09-13 12:00:57 -0700178}