Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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.impl; |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.event.AbstractEvent; |
| 22 | import org.onosproject.event.EventSink; |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.concurrent.CountDownLatch; |
| 27 | import java.util.concurrent.TimeUnit; |
| 28 | |
| 29 | import static org.junit.Assert.assertEquals; |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 30 | import static org.junit.Assert.assertTrue; |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 33 | * Test of the event dispatcher mechanism. |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 34 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 35 | public class CoreEventDispatcherTest { |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 36 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 37 | private final CoreEventDispatcher dispatcher = new CoreEventDispatcher(); |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 38 | private final PrickleSink prickleSink = new PrickleSink(); |
| 39 | private final GooSink gooSink = new GooSink(); |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() { |
| 43 | dispatcher.activate(); |
| 44 | dispatcher.addSink(Prickle.class, prickleSink); |
| 45 | dispatcher.addSink(Goo.class, gooSink); |
| 46 | } |
| 47 | |
| 48 | @After |
| 49 | public void tearDown() { |
| 50 | dispatcher.removeSink(Goo.class); |
| 51 | dispatcher.removeSink(Prickle.class); |
| 52 | dispatcher.deactivate(); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void post() throws Exception { |
| 57 | prickleSink.latch = new CountDownLatch(1); |
| 58 | dispatcher.post(new Prickle("yo")); |
| 59 | prickleSink.latch.await(100, TimeUnit.MILLISECONDS); |
| 60 | validate(prickleSink, "yo"); |
| 61 | validate(gooSink); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void postEventWithBadSink() throws Exception { |
| 66 | gooSink.latch = new CountDownLatch(1); |
| 67 | dispatcher.post(new Goo("boom")); |
| 68 | gooSink.latch.await(100, TimeUnit.MILLISECONDS); |
| 69 | validate(gooSink, "boom"); |
| 70 | validate(prickleSink); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void postEventWithNoSink() throws Exception { |
| 75 | dispatcher.post(new Thing("boom")); |
| 76 | validate(gooSink); |
| 77 | validate(prickleSink); |
| 78 | } |
| 79 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 80 | @Test |
| 81 | public void postEventSinkTakesTooLong() throws Exception { |
| 82 | SinkProcessTakesTooLong takesTooLong = new SinkProcessTakesTooLong(); |
| 83 | dispatcher.setDispatchTimeLimit(250); |
| 84 | dispatcher.addSink(TooLongEvent.class, takesTooLong); |
| 85 | takesTooLong.latch = new CountDownLatch(1); |
| 86 | dispatcher.post(new TooLongEvent("XYZZY")); |
| 87 | takesTooLong.latch.await(1000, TimeUnit.MILLISECONDS); |
| 88 | assertTrue(takesTooLong.interrupted); |
| 89 | } |
| 90 | |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 91 | private void validate(Sink sink, String... strings) { |
| 92 | int i = 0; |
| 93 | assertEquals("incorrect event count", strings.length, sink.subjects.size()); |
| 94 | for (String string : strings) { |
| 95 | assertEquals("incorrect event", string, sink.subjects.get(i++)); |
| 96 | } |
| 97 | } |
| 98 | |
Sho SHIMIZU | 006e486 | 2015-09-09 14:39:28 -0700 | [diff] [blame] | 99 | private enum Type { FOO } |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 100 | |
| 101 | private static class Thing extends AbstractEvent<Type, String> { |
| 102 | protected Thing(String subject) { |
| 103 | super(Type.FOO, subject); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private static class Prickle extends Thing { |
| 108 | protected Prickle(String subject) { |
| 109 | super(subject); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private static class Goo extends Thing { |
| 114 | protected Goo(String subject) { |
| 115 | super(subject); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private static class Sink { |
| 120 | final List<String> subjects = new ArrayList<>(); |
| 121 | CountDownLatch latch; |
| 122 | |
| 123 | protected void process(String subject) { |
| 124 | subjects.add(subject); |
| 125 | latch.countDown(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private static class PrickleSink extends Sink implements EventSink<Prickle> { |
| 130 | @Override |
| 131 | public void process(Prickle event) { |
| 132 | process(event.subject()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private static class GooSink extends Sink implements EventSink<Goo> { |
| 137 | @Override |
| 138 | public void process(Goo event) { |
| 139 | process(event.subject()); |
| 140 | throw new IllegalStateException("BOOM!"); |
| 141 | } |
| 142 | } |
| 143 | |
Ray Milkey | ce48f96 | 2016-10-20 15:15:49 -0700 | [diff] [blame] | 144 | private static class TooLongEvent extends AbstractEvent<Type, String> { |
| 145 | protected TooLongEvent(String subject) { |
| 146 | super(Type.FOO, subject); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private static class SinkProcessTakesTooLong |
| 151 | implements EventSink<TooLongEvent> { |
| 152 | boolean interrupted = false; |
| 153 | CountDownLatch latch; |
| 154 | |
| 155 | public void process(TooLongEvent event) { |
| 156 | try { |
| 157 | Thread.sleep(5 * 1000); |
| 158 | } catch (InterruptedException ie) { |
| 159 | interrupted = true; |
| 160 | } |
| 161 | latch.countDown(); |
| 162 | } |
| 163 | } |
| 164 | |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 165 | } |