blob: f13cacfea258516770c8fa666e97800129ebcfb4 [file] [log] [blame]
tom8ccc0bf2014-09-10 15:34:13 -07001package org.onlab.onos.event.impl;
2
3import org.junit.After;
4import org.junit.Before;
5import org.junit.Test;
6import org.onlab.onos.event.AbstractEvent;
7import org.onlab.onos.event.EventSink;
8
9import java.util.ArrayList;
10import java.util.List;
11import java.util.concurrent.CountDownLatch;
12import java.util.concurrent.TimeUnit;
13
14import static org.junit.Assert.assertEquals;
15
16/**
17 * Test of the even dispatcher mechanism.
18 */
tom202175a2014-09-19 19:00:11 -070019public class CoreEventDispatcherTest {
tom8ccc0bf2014-09-10 15:34:13 -070020
tom202175a2014-09-19 19:00:11 -070021 private final CoreEventDispatcher dispatcher = new CoreEventDispatcher();
tom8ccc0bf2014-09-10 15:34:13 -070022 private final PrickleSink prickleSink = new PrickleSink();
23 private final GooSink gooSink = new GooSink();
24
25 @Before
26 public void setUp() {
27 dispatcher.activate();
28 dispatcher.addSink(Prickle.class, prickleSink);
29 dispatcher.addSink(Goo.class, gooSink);
30 }
31
32 @After
33 public void tearDown() {
34 dispatcher.removeSink(Goo.class);
35 dispatcher.removeSink(Prickle.class);
36 dispatcher.deactivate();
37 }
38
39 @Test
40 public void post() throws Exception {
41 prickleSink.latch = new CountDownLatch(1);
42 dispatcher.post(new Prickle("yo"));
43 prickleSink.latch.await(100, TimeUnit.MILLISECONDS);
44 validate(prickleSink, "yo");
45 validate(gooSink);
46 }
47
48 @Test
49 public void postEventWithBadSink() throws Exception {
50 gooSink.latch = new CountDownLatch(1);
51 dispatcher.post(new Goo("boom"));
52 gooSink.latch.await(100, TimeUnit.MILLISECONDS);
53 validate(gooSink, "boom");
54 validate(prickleSink);
55 }
56
57 @Test
58 public void postEventWithNoSink() throws Exception {
59 dispatcher.post(new Thing("boom"));
60 validate(gooSink);
61 validate(prickleSink);
62 }
63
64 private void validate(Sink sink, String... strings) {
65 int i = 0;
66 assertEquals("incorrect event count", strings.length, sink.subjects.size());
67 for (String string : strings) {
68 assertEquals("incorrect event", string, sink.subjects.get(i++));
69 }
70 }
71
72 private enum Type { FOO };
73
74 private static class Thing extends AbstractEvent<Type, String> {
75 protected Thing(String subject) {
76 super(Type.FOO, subject);
77 }
78 }
79
80 private static class Prickle extends Thing {
81 protected Prickle(String subject) {
82 super(subject);
83 }
84 }
85
86 private static class Goo extends Thing {
87 protected Goo(String subject) {
88 super(subject);
89 }
90 }
91
92 private static class Sink {
93 final List<String> subjects = new ArrayList<>();
94 CountDownLatch latch;
95
96 protected void process(String subject) {
97 subjects.add(subject);
98 latch.countDown();
99 }
100 }
101
102 private static class PrickleSink extends Sink implements EventSink<Prickle> {
103 @Override
104 public void process(Prickle event) {
105 process(event.subject());
106 }
107 }
108
109 private static class GooSink extends Sink implements EventSink<Goo> {
110 @Override
111 public void process(Goo event) {
112 process(event.subject());
113 throw new IllegalStateException("BOOM!");
114 }
115 }
116
117}