blob: 1538e16bc7e9990c9e5233b55cb654f50243e5a6 [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;
Ray Milkeyce48f962016-10-20 15:15:49 -070030import static org.junit.Assert.assertTrue;
tom8ccc0bf2014-09-10 15:34:13 -070031
32/**
Jonathan Hart943893f2016-04-08 13:38:54 -070033 * Test of the event dispatcher mechanism.
tom8ccc0bf2014-09-10 15:34:13 -070034 */
tom202175a2014-09-19 19:00:11 -070035public class CoreEventDispatcherTest {
tom8ccc0bf2014-09-10 15:34:13 -070036
tom202175a2014-09-19 19:00:11 -070037 private final CoreEventDispatcher dispatcher = new CoreEventDispatcher();
tom8ccc0bf2014-09-10 15:34:13 -070038 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 Milkeyce48f962016-10-20 15:15:49 -070080 @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
tom8ccc0bf2014-09-10 15:34:13 -070091 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 SHIMIZU006e4862015-09-09 14:39:28 -070099 private enum Type { FOO }
tom8ccc0bf2014-09-10 15:34:13 -0700100
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 Milkeyce48f962016-10-20 15:15:49 -0700144 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
tom8ccc0bf2014-09-10 15:34:13 -0700165}