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