tom

Added a unit test for the simple event dispatcher.

1 +package org.onlab.onos.event.impl;
2 +
3 +import org.junit.After;
4 +import org.junit.Before;
5 +import org.junit.Test;
6 +import org.onlab.onos.event.AbstractEvent;
7 +import org.onlab.onos.event.EventSink;
8 +
9 +import java.util.ArrayList;
10 +import java.util.List;
11 +import java.util.concurrent.CountDownLatch;
12 +import java.util.concurrent.TimeUnit;
13 +
14 +import static org.junit.Assert.assertEquals;
15 +
16 +/**
17 + * Test of the even dispatcher mechanism.
18 + */
19 +public class SimpleEventDispatcherTest {
20 +
21 + private final SimpleEventDispatcher dispatcher = new SimpleEventDispatcher();
22 + 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 +}