blob: e3d87e20194057da6f802853613b795f84813246 [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
43 @Test
44 public void eventTrigger() {
45 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080046 accumulator.add(new TestItem("a"));
47 accumulator.add(new TestItem("b"));
48 accumulator.add(new TestItem("c"));
49 accumulator.add(new TestItem("d"));
tom931af4e2014-09-13 12:00:57 -070050 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080051 accumulator.add(new TestItem("e"));
Jonathan Hartecaa8a82015-02-04 09:44:08 -080052 delay(20);
tom931af4e2014-09-13 12:00:57 -070053 assertFalse("should have fired", accumulator.batch.isEmpty());
54 assertEquals("incorrect batch", "abcde", accumulator.batch);
55 }
56
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070057 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070058 @Test
59 public void timeTrigger() {
60 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080061 accumulator.add(new TestItem("a"));
tom093340b2014-10-10 00:15:36 -070062 delay(30);
tom931af4e2014-09-13 12:00:57 -070063 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080064 accumulator.add(new TestItem("b"));
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("c"));
tom093340b2014-10-10 00:15:36 -070068 delay(30);
69 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080070 accumulator.add(new TestItem("d"));
Thomas Vachuska75af68a2015-02-22 12:13:52 -080071 delay(60);
tom931af4e2014-09-13 12:00:57 -070072 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070073 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070074 }
75
76 @Test
77 public void idleTrigger() {
78 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080079 accumulator.add(new TestItem("a"));
tom931af4e2014-09-13 12:00:57 -070080 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080081 accumulator.add(new TestItem("b"));
tom931af4e2014-09-13 12:00:57 -070082 delay(80);
83 assertFalse("should have fired", accumulator.batch.isEmpty());
84 assertEquals("incorrect batch", "ab", accumulator.batch);
85 }
86
Thomas Vachuska75af68a2015-02-22 12:13:52 -080087 @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 Vachuskaecb63c52015-02-19 10:03:48 -0800135 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> {
tom931af4e2014-09-13 12:00:57 -0700144
145 String batch = "";
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800146 boolean ready = true;
tom931af4e2014-09-13 12:00:57 -0700147
148 protected TestAccumulator() {
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800149 super(timer, 5, 100, 70);
tom931af4e2014-09-13 12:00:57 -0700150 }
151
152 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800153 public void processItems(List<TestItem> items) {
154 for (TestItem item : items) {
155 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700156 }
157 }
Thomas Vachuska75af68a2015-02-22 12:13:52 -0800158
159 @Override
160 public boolean isReady() {
161 return ready;
162 }
tom931af4e2014-09-13 12:00:57 -0700163 }
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800164
tom931af4e2014-09-13 12:00:57 -0700165}