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