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 | |
Yuta HIGUCHI | 38935e5 | 2014-10-10 13:27:46 -0700 | [diff] [blame^] | 12 | import org.junit.Ignore; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 13 | import org.junit.Test; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Tests the operation of the accumulator. |
| 17 | */ |
| 18 | public 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 HIGUCHI | 38935e5 | 2014-10-10 13:27:46 -0700 | [diff] [blame^] | 45 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 46 | @Test |
| 47 | public void timeTrigger() { |
| 48 | TestAccumulator accumulator = new TestAccumulator(); |
| 49 | accumulator.add(new TestEvent(FOO, "a")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 50 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 51 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 52 | accumulator.add(new TestEvent(FOO, "b")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 53 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 54 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 55 | accumulator.add(new TestEvent(FOO, "c")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 56 | delay(30); |
| 57 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 58 | accumulator.add(new TestEvent(FOO, "d")); |
| 59 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 60 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 61 | assertEquals("incorrect batch", "abcd", accumulator.batch); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 62 | } |
| 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 | } |