blob: 9e561bfe5c983ae5d667d221275a55d109978cb7 [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
alshabib030111e2014-09-15 15:56:42 -070012import org.junit.Test;
tom931af4e2014-09-13 12:00:57 -070013
14/**
15 * Tests the operation of the accumulator.
16 */
17public class AbstractEventAccumulatorTest {
18
19 private final Timer timer = new Timer();
20
21 @Test
22 public void basics() throws Exception {
23 TestAccumulator accumulator = new TestAccumulator();
24 assertEquals("incorrect timer", timer, accumulator.timer());
25 assertEquals("incorrect max events", 5, accumulator.maxEvents());
26 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
27 assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis());
28 }
29
30 @Test
31 public void eventTrigger() {
32 TestAccumulator accumulator = new TestAccumulator();
33 accumulator.add(new TestEvent(FOO, "a"));
34 accumulator.add(new TestEvent(FOO, "b"));
35 accumulator.add(new TestEvent(FOO, "c"));
36 accumulator.add(new TestEvent(FOO, "d"));
37 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
38 accumulator.add(new TestEvent(FOO, "e"));
39 delay(10);
40 assertFalse("should have fired", accumulator.batch.isEmpty());
41 assertEquals("incorrect batch", "abcde", accumulator.batch);
42 }
43
44 @Test
45 public void timeTrigger() {
46 TestAccumulator accumulator = new TestAccumulator();
47 accumulator.add(new TestEvent(FOO, "a"));
48 delay(40);
49 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
50 accumulator.add(new TestEvent(FOO, "b"));
51 delay(40);
52 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
53 accumulator.add(new TestEvent(FOO, "c"));
54 delay(40);
55 assertFalse("should have fired", accumulator.batch.isEmpty());
56 assertEquals("incorrect batch", "abc", accumulator.batch);
57 }
58
59 @Test
60 public void idleTrigger() {
61 TestAccumulator accumulator = new TestAccumulator();
62 accumulator.add(new TestEvent(FOO, "a"));
63 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
64 accumulator.add(new TestEvent(FOO, "b"));
65 delay(80);
66 assertFalse("should have fired", accumulator.batch.isEmpty());
67 assertEquals("incorrect batch", "ab", accumulator.batch);
68 }
69
70 private class TestAccumulator extends AbstractEventAccumulator {
71
72 String batch = "";
73
74 protected TestAccumulator() {
75 super(timer, 5, 100, 50);
76 }
77
78 @Override
79 public void processEvents(List<Event> events) {
80 for (Event event : events) {
81 batch += event.subject();
82 }
83 }
84 }
85}