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.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; |
| 30 | |
| 31 | /** |
| 32 | * Test of the even dispatcher mechanism. |
| 33 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 34 | public class CoreEventDispatcherTest { |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 35 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 36 | private final CoreEventDispatcher dispatcher = new CoreEventDispatcher(); |
tom | 8ccc0bf | 2014-09-10 15:34:13 -0700 | [diff] [blame] | 37 | private final PrickleSink prickleSink = new PrickleSink(); |
| 38 | private final GooSink gooSink = new GooSink(); |
| 39 | |
| 40 | @Before |
| 41 | public void setUp() { |
| 42 | dispatcher.activate(); |
| 43 | dispatcher.addSink(Prickle.class, prickleSink); |
| 44 | dispatcher.addSink(Goo.class, gooSink); |
| 45 | } |
| 46 | |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | dispatcher.removeSink(Goo.class); |
| 50 | dispatcher.removeSink(Prickle.class); |
| 51 | dispatcher.deactivate(); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void post() throws Exception { |
| 56 | prickleSink.latch = new CountDownLatch(1); |
| 57 | dispatcher.post(new Prickle("yo")); |
| 58 | prickleSink.latch.await(100, TimeUnit.MILLISECONDS); |
| 59 | validate(prickleSink, "yo"); |
| 60 | validate(gooSink); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void postEventWithBadSink() throws Exception { |
| 65 | gooSink.latch = new CountDownLatch(1); |
| 66 | dispatcher.post(new Goo("boom")); |
| 67 | gooSink.latch.await(100, TimeUnit.MILLISECONDS); |
| 68 | validate(gooSink, "boom"); |
| 69 | validate(prickleSink); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void postEventWithNoSink() throws Exception { |
| 74 | dispatcher.post(new Thing("boom")); |
| 75 | validate(gooSink); |
| 76 | validate(prickleSink); |
| 77 | } |
| 78 | |
| 79 | private void validate(Sink sink, String... strings) { |
| 80 | int i = 0; |
| 81 | assertEquals("incorrect event count", strings.length, sink.subjects.size()); |
| 82 | for (String string : strings) { |
| 83 | assertEquals("incorrect event", string, sink.subjects.get(i++)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private enum Type { FOO }; |
| 88 | |
| 89 | private static class Thing extends AbstractEvent<Type, String> { |
| 90 | protected Thing(String subject) { |
| 91 | super(Type.FOO, subject); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private static class Prickle extends Thing { |
| 96 | protected Prickle(String subject) { |
| 97 | super(subject); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private static class Goo extends Thing { |
| 102 | protected Goo(String subject) { |
| 103 | super(subject); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private static class Sink { |
| 108 | final List<String> subjects = new ArrayList<>(); |
| 109 | CountDownLatch latch; |
| 110 | |
| 111 | protected void process(String subject) { |
| 112 | subjects.add(subject); |
| 113 | latch.countDown(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private static class PrickleSink extends Sink implements EventSink<Prickle> { |
| 118 | @Override |
| 119 | public void process(Prickle event) { |
| 120 | process(event.subject()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private static class GooSink extends Sink implements EventSink<Goo> { |
| 125 | @Override |
| 126 | public void process(Goo event) { |
| 127 | process(event.subject()); |
| 128 | throw new IllegalStateException("BOOM!"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } |