Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame^] | 18 | import org.junit.Ignore; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | import java.util.List; |
| 22 | import java.util.Timer; |
| 23 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertFalse; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.onlab.junit.TestTools.delay; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import static org.onosproject.event.TestEvent.Type.FOO; |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 29 | |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Tests the operation of the accumulator. |
| 32 | */ |
| 33 | public class AbstractEventAccumulatorTest { |
| 34 | |
| 35 | private final Timer timer = new Timer(); |
| 36 | |
| 37 | @Test |
| 38 | public void basics() throws Exception { |
| 39 | TestAccumulator accumulator = new TestAccumulator(); |
| 40 | assertEquals("incorrect timer", timer, accumulator.timer()); |
| 41 | assertEquals("incorrect max events", 5, accumulator.maxEvents()); |
| 42 | assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis()); |
| 43 | assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis()); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void eventTrigger() { |
| 48 | TestAccumulator accumulator = new TestAccumulator(); |
| 49 | accumulator.add(new TestEvent(FOO, "a")); |
| 50 | accumulator.add(new TestEvent(FOO, "b")); |
| 51 | accumulator.add(new TestEvent(FOO, "c")); |
| 52 | accumulator.add(new TestEvent(FOO, "d")); |
| 53 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 54 | accumulator.add(new TestEvent(FOO, "e")); |
Jonathan Hart | ecaa8a8 | 2015-02-04 09:44:08 -0800 | [diff] [blame^] | 55 | delay(20); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 56 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 57 | assertEquals("incorrect batch", "abcde", accumulator.batch); |
| 58 | } |
| 59 | |
Yuta HIGUCHI | 38935e5 | 2014-10-10 13:27:46 -0700 | [diff] [blame] | 60 | @Ignore("FIXME: timing sensitive test failing randomly.") |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 61 | @Test |
| 62 | public void timeTrigger() { |
| 63 | TestAccumulator accumulator = new TestAccumulator(); |
| 64 | accumulator.add(new TestEvent(FOO, "a")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 65 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 66 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 67 | accumulator.add(new TestEvent(FOO, "b")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 68 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 69 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 70 | accumulator.add(new TestEvent(FOO, "c")); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 71 | delay(30); |
| 72 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 73 | accumulator.add(new TestEvent(FOO, "d")); |
| 74 | delay(30); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 75 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 76 | assertEquals("incorrect batch", "abcd", accumulator.batch); |
tom | 931af4e | 2014-09-13 12:00:57 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void idleTrigger() { |
| 81 | TestAccumulator accumulator = new TestAccumulator(); |
| 82 | accumulator.add(new TestEvent(FOO, "a")); |
| 83 | assertTrue("should not have fired yet", accumulator.batch.isEmpty()); |
| 84 | accumulator.add(new TestEvent(FOO, "b")); |
| 85 | delay(80); |
| 86 | assertFalse("should have fired", accumulator.batch.isEmpty()); |
| 87 | assertEquals("incorrect batch", "ab", accumulator.batch); |
| 88 | } |
| 89 | |
| 90 | private class TestAccumulator extends AbstractEventAccumulator { |
| 91 | |
| 92 | String batch = ""; |
| 93 | |
| 94 | protected TestAccumulator() { |
| 95 | super(timer, 5, 100, 50); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void processEvents(List<Event> events) { |
| 100 | for (Event event : events) { |
| 101 | batch += event.subject(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |