blob: 55ed7bff48bf5953aab6a630b0233d0404efb6fc [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
tom931af4e2014-09-13 12:00:57 -070016package org.onlab.onos.event;
17
alshabib030111e2014-09-15 15:56:42 -070018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.onlab.junit.TestTools.delay;
22import static org.onlab.onos.event.TestEvent.Type.FOO;
tom931af4e2014-09-13 12:00:57 -070023
24import java.util.List;
25import java.util.Timer;
26
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070027import org.junit.Ignore;
alshabib030111e2014-09-15 15:56:42 -070028import org.junit.Test;
tom931af4e2014-09-13 12:00:57 -070029
30/**
31 * Tests the operation of the accumulator.
32 */
33public class AbstractEventAccumulatorTest {
34
35 private final Timer timer = new Timer();
36
37 @Test
38 public void basics() throws Exception {
39 TestAccumulator accumulator = new TestAccumulator();
40 assertEquals("incorrect timer", timer, accumulator.timer());
41 assertEquals("incorrect max events", 5, accumulator.maxEvents());
42 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
43 assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis());
44 }
45
46 @Test
47 public void eventTrigger() {
48 TestAccumulator accumulator = new TestAccumulator();
49 accumulator.add(new TestEvent(FOO, "a"));
50 accumulator.add(new TestEvent(FOO, "b"));
51 accumulator.add(new TestEvent(FOO, "c"));
52 accumulator.add(new TestEvent(FOO, "d"));
53 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
54 accumulator.add(new TestEvent(FOO, "e"));
55 delay(10);
56 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();
64 accumulator.add(new TestEvent(FOO, "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());
67 accumulator.add(new TestEvent(FOO, "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());
70 accumulator.add(new TestEvent(FOO, "c"));
tom093340b2014-10-10 00:15:36 -070071 delay(30);
72 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
73 accumulator.add(new TestEvent(FOO, "d"));
74 delay(30);
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
79 @Test
80 public void idleTrigger() {
81 TestAccumulator accumulator = new TestAccumulator();
82 accumulator.add(new TestEvent(FOO, "a"));
83 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
84 accumulator.add(new TestEvent(FOO, "b"));
85 delay(80);
86 assertFalse("should have fired", accumulator.batch.isEmpty());
87 assertEquals("incorrect batch", "ab", accumulator.batch);
88 }
89
90 private class TestAccumulator extends AbstractEventAccumulator {
91
92 String batch = "";
93
94 protected TestAccumulator() {
95 super(timer, 5, 100, 50);
96 }
97
98 @Override
99 public void processEvents(List<Event> events) {
100 for (Event event : events) {
101 batch += event.subject();
102 }
103 }
104 }
105}