blob: 421fa7a445a5eb32592f69680a2c1fd11f3b1429 [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;
23
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080024import static org.junit.Assert.*;
alshabib030111e2014-09-15 15:56:42 -070025import static org.onlab.junit.TestTools.delay;
tom931af4e2014-09-13 12:00:57 -070026
tom931af4e2014-09-13 12:00:57 -070027/**
28 * Tests the operation of the accumulator.
29 */
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080030public class AbstractAccumulatorTest {
tom931af4e2014-09-13 12:00:57 -070031
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 Vachuskaecb63c52015-02-19 10:03:48 -080038 assertEquals("incorrect max events", 5, accumulator.maxItems());
tom931af4e2014-09-13 12:00:57 -070039 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
Thomas Vachuska75af68a2015-02-22 12:13:52 -080040 assertEquals("incorrect idle ms", 70, accumulator.maxIdleMillis());
tom931af4e2014-09-13 12:00:57 -070041 }
42
Jonathan Hart49f09202015-07-15 15:15:06 -070043 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070044 @Test
45 public void eventTrigger() {
46 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080047 accumulator.add(new TestItem("a"));
48 accumulator.add(new TestItem("b"));
49 accumulator.add(new TestItem("c"));
50 accumulator.add(new TestItem("d"));
tom931af4e2014-09-13 12:00:57 -070051 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080052 accumulator.add(new TestItem("e"));
Jonathan Hartecaa8a82015-02-04 09:44:08 -080053 delay(20);
tom931af4e2014-09-13 12:00:57 -070054 assertFalse("should have fired", accumulator.batch.isEmpty());
55 assertEquals("incorrect batch", "abcde", accumulator.batch);
56 }
57
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070058 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070059 @Test
60 public void timeTrigger() {
61 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080062 accumulator.add(new TestItem("a"));
tom093340b2014-10-10 00:15:36 -070063 delay(30);
tom931af4e2014-09-13 12:00:57 -070064 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080065 accumulator.add(new TestItem("b"));
tom093340b2014-10-10 00:15:36 -070066 delay(30);
tom931af4e2014-09-13 12:00:57 -070067 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080068 accumulator.add(new TestItem("c"));
tom093340b2014-10-10 00:15:36 -070069 delay(30);
70 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080071 accumulator.add(new TestItem("d"));
Thomas Vachuska75af68a2015-02-22 12:13:52 -080072 delay(60);
tom931af4e2014-09-13 12:00:57 -070073 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070074 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070075 }
76
Jonathan Hart49f09202015-07-15 15:15:06 -070077 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070078 @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"));
tom931af4e2014-09-13 12:00:57 -070084 delay(80);
85 assertFalse("should have fired", accumulator.batch.isEmpty());
86 assertEquals("incorrect batch", "ab", accumulator.batch);
87 }
88
Jonathan Hart49f09202015-07-15 15:15:06 -070089 @Ignore("FIXME: timing sensitive test failing randomly.")
Thomas Vachuska75af68a2015-02-22 12:13:52 -080090 @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 Hart49f09202015-07-15 15:15:06 -0700105 @Ignore("FIXME: timing sensitive test failing randomly.")
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800106 @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
alshabibea74cc92015-07-07 10:38:21 -0700120 @Ignore("FIXME: timing sensitive test failing randomly.")
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800121 @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 Vachuskaecb63c52015-02-19 10:03:48 -0800140 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> {
tom931af4e2014-09-13 12:00:57 -0700149
150 String batch = "";
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800151 boolean ready = true;
tom931af4e2014-09-13 12:00:57 -0700152
153 protected TestAccumulator() {
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800154 super(timer, 5, 100, 70);
tom931af4e2014-09-13 12:00:57 -0700155 }
156
157 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800158 public void processItems(List<TestItem> items) {
159 for (TestItem item : items) {
160 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700161 }
162 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800163
164 @Override
165 public boolean isReady() {
166 return ready;
167 }
tom931af4e2014-09-13 12:00:57 -0700168 }
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800169
tom931af4e2014-09-13 12:00:57 -0700170}