tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.event; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import java.util.List; |
| 6 | import java.util.Timer; |
| 7 | |
| 8 | import static org.junit.Assert.*; |
| 9 | import static org.onlab.junit.TestTools.delay; |
| 10 | import static org.onlab.onos.event.TestEvent.Type.FOO; |
| 11 | |
| 12 | /** |
| 13 | * Tests the operation of the accumulator. |
| 14 | */ |
| 15 | public 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 | } |