blob: 375748193071260338dfe026421c0406f6eb6456 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.event.impl;
tom8ccc0bf2014-09-10 15:34:13 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.event.AbstractEvent;
22import org.onosproject.event.EventSink;
tom8ccc0bf2014-09-10 15:34:13 -070023
24import java.util.ArrayList;
25import java.util.List;
26import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
29import static org.junit.Assert.assertEquals;
30
31/**
32 * Test of the even dispatcher mechanism.
33 */
tom202175a2014-09-19 19:00:11 -070034public class CoreEventDispatcherTest {
tom8ccc0bf2014-09-10 15:34:13 -070035
tom202175a2014-09-19 19:00:11 -070036 private final CoreEventDispatcher dispatcher = new CoreEventDispatcher();
tom8ccc0bf2014-09-10 15:34:13 -070037 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
Sho SHIMIZU006e4862015-09-09 14:39:28 -070087 private enum Type { FOO }
tom8ccc0bf2014-09-10 15:34:13 -070088
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}