blob: 1f57266495d8df10fed0f48a7b8f02345bd072a7 [file] [log] [blame]
tom931af4e2014-09-13 12:00:57 -07001package org.onlab.onos.event;
2
alshabib030111e2014-09-15 15:56:42 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.onlab.junit.TestTools.delay;
7import static org.onlab.onos.event.TestEvent.Type.FOO;
tom931af4e2014-09-13 12:00:57 -07008
9import java.util.List;
10import java.util.Timer;
11
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070012import org.junit.Ignore;
alshabib030111e2014-09-15 15:56:42 -070013import org.junit.Test;
tom931af4e2014-09-13 12:00:57 -070014
15/**
16 * Tests the operation of the accumulator.
17 */
18public class AbstractEventAccumulatorTest {
19
20 private final Timer timer = new Timer();
21
22 @Test
23 public void basics() throws Exception {
24 TestAccumulator accumulator = new TestAccumulator();
25 assertEquals("incorrect timer", timer, accumulator.timer());
26 assertEquals("incorrect max events", 5, accumulator.maxEvents());
27 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
28 assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis());
29 }
30
31 @Test
32 public void eventTrigger() {
33 TestAccumulator accumulator = new TestAccumulator();
34 accumulator.add(new TestEvent(FOO, "a"));
35 accumulator.add(new TestEvent(FOO, "b"));
36 accumulator.add(new TestEvent(FOO, "c"));
37 accumulator.add(new TestEvent(FOO, "d"));
38 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
39 accumulator.add(new TestEvent(FOO, "e"));
40 delay(10);
41 assertFalse("should have fired", accumulator.batch.isEmpty());
42 assertEquals("incorrect batch", "abcde", accumulator.batch);
43 }
44
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070045 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070046 @Test
47 public void timeTrigger() {
48 TestAccumulator accumulator = new TestAccumulator();
49 accumulator.add(new TestEvent(FOO, "a"));
tom093340b2014-10-10 00:15:36 -070050 delay(30);
tom931af4e2014-09-13 12:00:57 -070051 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
52 accumulator.add(new TestEvent(FOO, "b"));
tom093340b2014-10-10 00:15:36 -070053 delay(30);
tom931af4e2014-09-13 12:00:57 -070054 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
55 accumulator.add(new TestEvent(FOO, "c"));
tom093340b2014-10-10 00:15:36 -070056 delay(30);
57 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
58 accumulator.add(new TestEvent(FOO, "d"));
59 delay(30);
tom931af4e2014-09-13 12:00:57 -070060 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070061 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070062 }
63
64 @Test
65 public void idleTrigger() {
66 TestAccumulator accumulator = new TestAccumulator();
67 accumulator.add(new TestEvent(FOO, "a"));
68 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
69 accumulator.add(new TestEvent(FOO, "b"));
70 delay(80);
71 assertFalse("should have fired", accumulator.batch.isEmpty());
72 assertEquals("incorrect batch", "ab", accumulator.batch);
73 }
74
75 private class TestAccumulator extends AbstractEventAccumulator {
76
77 String batch = "";
78
79 protected TestAccumulator() {
80 super(timer, 5, 100, 50);
81 }
82
83 @Override
84 public void processEvents(List<Event> events) {
85 for (Event event : events) {
86 batch += event.subject();
87 }
88 }
89 }
90}