blob: 87b3fc0b55f154527c1166c9adb50b6785837107 [file] [log] [blame]
yoonseonfe721972017-01-10 17:18:49 -08001/*
2 * Copyright 2017-present 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 */
16
17package org.onosproject.incubator.net.virtual.event;
18
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.event.AbstractEvent;
23import org.onosproject.event.Event;
24import org.onosproject.event.EventDeliveryService;
25import org.onosproject.event.EventListener;
26import org.onosproject.event.EventSink;
27import org.onosproject.incubator.net.virtual.NetworkId;
28
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Set;
32import java.util.concurrent.CountDownLatch;
33import java.util.concurrent.TimeUnit;
34
35import static org.junit.Assert.*;
36
37/**
38 * Test of the virtual event dispatcher mechanism.
39 */
40public class AbstractVirtualListenerManagerTest {
41
42 TestEventDispatcher dispatcher = new TestEventDispatcher();
43 VirtualListenerRegistryManager listenerRegistryManager =
44 VirtualListenerRegistryManager.getInstance();
45
46 PrickleManager prickleManager;
47 PrickleListener prickleListener;
48
49 GooManager gooManager;
50 GooListener gooListener;
51
52 BarManager barManager;
53 BarListener barListener;
54
55 @Before
56 public void setUp() {
57 dispatcher.addSink(VirtualEvent.class, listenerRegistryManager);
58
59 prickleListener = new PrickleListener();
60 prickleManager = new PrickleManager(NetworkId.networkId(1));
61 prickleManager.eventDispatcher = dispatcher;
62 prickleManager.addListener(prickleListener);
63
64 gooListener = new GooListener();
65 gooManager = new GooManager(NetworkId.networkId(1));
66 gooManager.eventDispatcher = dispatcher;
67 gooManager.addListener(gooListener);
68
69 barListener = new BarListener();
70 barManager = new BarManager(NetworkId.networkId(2));
71 barManager.eventDispatcher = dispatcher;
72 barManager.addListener(barListener);
73 }
74
75 @After
76 public void tearDown() {
77 dispatcher.removeSink(VirtualEvent.class);
78
79 prickleListener.events.clear();
80 gooListener.events.clear();
81 barListener.events.clear();
82
83 prickleListener.latch = null;
84 gooListener.latch = null;
85 barListener.latch = null;
86 }
87
88 @Test
89 public void postPrickle() throws InterruptedException {
90 prickleListener.latch = new CountDownLatch(1);
91 prickleManager.post(new Prickle("prickle"));
92 prickleListener.latch.await(100, TimeUnit.MILLISECONDS);
93
94 validate(prickleListener, "prickle");
95 validate(gooListener);
96 validate(barListener);
97 }
98
99 @Test
100 public void postGoo() throws InterruptedException {
101 gooListener.latch = new CountDownLatch(1);
102 gooManager.post(new Goo("goo"));
103 gooListener.latch.await(100, TimeUnit.MILLISECONDS);
104
105 validate(prickleListener);
106 validate(gooListener, "goo");
107 validate(barListener);
108 }
109
110 @Test
111 public void postBar() throws InterruptedException {
112 barListener.latch = new CountDownLatch(1);
113 barManager.post(new Bar("bar"));
114 barListener.latch.await(100, TimeUnit.MILLISECONDS);
115
116 validate(prickleListener);
117 validate(gooListener);
118 validate(barListener, "bar");
119 }
120
121 @Test
122 public void postEventWithNoListener() throws Exception {
123 dispatcher.post(new Thing("boom"));
124
125 validate(prickleListener);
126 validate(gooListener);
127 validate(barListener);
128 }
129
130 private void validate(TestListener listener, String... strings) {
131 int i = 0;
132 assertEquals("incorrect event count", strings.length, listener.events.size());
133 for (String string : strings) {
134 Event event = (Event) listener.events.get(i++);
135 assertEquals("incorrect event", string, event.subject());
136 }
137 }
138
139 private enum Type { FOO }
140
141 private static class Thing extends AbstractEvent<Type, String> {
142 protected Thing(String subject) {
143 super(Type.FOO, subject);
144 }
145 }
146
147 private static class Prickle extends Thing {
148 protected Prickle(String subject) {
149 super(subject);
150 }
151 }
152
153 private static class Goo extends Thing {
154 protected Goo(String subject) {
155 super(subject);
156 }
157 }
158
159 private static class Bar extends Thing {
160 protected Bar(String subject) {
161 super(subject);
162 }
163 }
164
165 private class TestListener<E extends Event> implements EventListener<E> {
166 List<E> events = new ArrayList<>();
167 CountDownLatch latch;
168
169 @Override
170 public void event(E event) {
171 System.out.println(this.getClass().toString());
172 events.add(event);
173 latch.countDown();
174 }
175 }
176
177 private class PrickleListener extends TestListener<Prickle> {
178 }
179
180 private class GooListener extends TestListener<Goo> {
181 }
182
183 private class BarListener extends TestListener<Bar> {
184 }
185
186 private class PrickleManager extends AbstractVirtualListenerManager<Prickle, PrickleListener> {
187 public PrickleManager(NetworkId networkId) {
188 super(networkId);
189 }
190 }
191
192 private class GooManager extends AbstractVirtualListenerManager<Goo, GooListener> {
193 public GooManager(NetworkId networkId) {
194 super(networkId);
195 }
196 }
197
198 private class BarManager extends AbstractVirtualListenerManager<Bar, BarListener> {
199 public BarManager(NetworkId networkId) {
200 super(networkId);
201 }
202 }
203
204
205 private class TestEventDispatcher implements EventDeliveryService {
206 private EventSink sink;
207
208 @Override
209 public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) {
210 this.sink = sink;
211 }
212
213 @Override
214 public <E extends Event> void removeSink(Class<E> eventClass) {
215 this.sink = null;
216 }
217
218 @Override
219 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
220 return null;
221 }
222
223 @Override
224 public Set<Class<? extends Event>> getSinks() {
225 return null;
226 }
227
228 @Override
229 public void setDispatchTimeLimit(long millis) {
230
231 }
232
233 @Override
234 public long getDispatchTimeLimit() {
235 return 0;
236 }
237
238 @Override
239 public void post(Event event) {
240 if (event instanceof VirtualEvent) {
241 sink.process(event);
242 }
243 }
244 }
245}