blob: 02f0deb184f101e5f1f0c83c84d52abe36770902 [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.Ignore;
19import org.junit.Test;
20
21import java.util.List;
22import java.util.Timer;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -070023import java.util.stream.IntStream;
Jonathan Hartecaa8a82015-02-04 09:44:08 -080024
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080025import static org.junit.Assert.*;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -070026import static org.onlab.junit.TestTools.assertAfter;
alshabib030111e2014-09-15 15:56:42 -070027import static org.onlab.junit.TestTools.delay;
tom931af4e2014-09-13 12:00:57 -070028
tom931af4e2014-09-13 12:00:57 -070029/**
30 * Tests the operation of the accumulator.
31 */
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080032public class AbstractAccumulatorTest {
tom931af4e2014-09-13 12:00:57 -070033
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 Vachuskaecb63c52015-02-19 10:03:48 -080040 assertEquals("incorrect max events", 5, accumulator.maxItems());
tom931af4e2014-09-13 12:00:57 -070041 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
Thomas Vachuska75af68a2015-02-22 12:13:52 -080042 assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis());
tom931af4e2014-09-13 12:00:57 -070043 }
44
Jonathan Hart49f09202015-07-15 15:15:06 -070045 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070046 @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"));
Jonathan Hartecaa8a82015-02-04 09:44:08 -080055 delay(20);
tom931af4e2014-09-13 12:00:57 -070056 assertFalse("should have fired", accumulator.batch.isEmpty());
57 assertEquals("incorrect batch", "abcde", accumulator.batch);
58 }
59
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070060 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070061 @Test
62 public void timeTrigger() {
63 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080064 accumulator.add(new TestItem("a"));
tom093340b2014-10-10 00:15:36 -070065 delay(30);
tom931af4e2014-09-13 12:00:57 -070066 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080067 accumulator.add(new TestItem("b"));
tom093340b2014-10-10 00:15:36 -070068 delay(30);
tom931af4e2014-09-13 12:00:57 -070069 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080070 accumulator.add(new TestItem("c"));
tom093340b2014-10-10 00:15:36 -070071 delay(30);
72 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080073 accumulator.add(new TestItem("d"));
Thomas Vachuska75af68a2015-02-22 12:13:52 -080074 delay(60);
tom931af4e2014-09-13 12:00:57 -070075 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070076 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070077 }
78
Jonathan Hart49f09202015-07-15 15:15:06 -070079 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070080 @Test
81 public void idleTrigger() {
82 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080083 accumulator.add(new TestItem("a"));
tom931af4e2014-09-13 12:00:57 -070084 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080085 accumulator.add(new TestItem("b"));
tom931af4e2014-09-13 12:00:57 -070086 delay(80);
87 assertFalse("should have fired", accumulator.batch.isEmpty());
88 assertEquals("incorrect batch", "ab", accumulator.batch);
89 }
90
Jonathan Hart49f09202015-07-15 15:15:06 -070091 @Ignore("FIXME: timing sensitive test failing randomly.")
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"));
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 Hart49f09202015-07-15 15:15:06 -0700107 @Ignore("FIXME: timing sensitive test failing randomly.")
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800108 @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
alshabibea74cc92015-07-07 10:38:21 -0700122 @Ignore("FIXME: timing sensitive test failing randomly.")
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800123 @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 Vachuskaa82341c2015-08-25 17:36:59 -0700141 @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 Vachuska75af68a2015-02-22 12:13:52 -0800149
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800150 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> {
tom931af4e2014-09-13 12:00:57 -0700159
160 String batch = "";
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800161 boolean ready = true;
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700162 int batchCount = 0;
163 int itemCount = 0;
tom931af4e2014-09-13 12:00:57 -0700164
165 protected TestAccumulator() {
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800166 super(timer, 5, 100, 70);
tom931af4e2014-09-13 12:00:57 -0700167 }
168
169 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800170 public void processItems(List<TestItem> items) {
Thomas Vachuskaa82341c2015-08-25 17:36:59 -0700171 batchCount++;
172 itemCount += items.size();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800173 for (TestItem item : items) {
174 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700175 }
176 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800177
178 @Override
179 public boolean isReady() {
180 return ready;
181 }
tom931af4e2014-09-13 12:00:57 -0700182 }
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800183
tom931af4e2014-09-13 12:00:57 -0700184}