• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
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 
17 package com.google.common.eventbus;
18 
19 import com.google.common.collect.Lists;
20 import java.util.List;
21 import junit.framework.TestCase;
22 
23 /**
24  * Validate that {@link EventBus} behaves carefully when listeners publish
25  * their own events.
26  *
27  * @author Jesse Wilson
28  */
29 public class ReentrantEventsTest extends TestCase {
30 
31   static final String FIRST = "one";
32   static final Double SECOND = 2.0d;
33 
34   final EventBus bus = new EventBus();
35 
testNoReentrantEvents()36   public void testNoReentrantEvents() {
37     ReentrantEventsHater hater = new ReentrantEventsHater();
38     bus.register(hater);
39 
40     bus.post(FIRST);
41 
42     assertEquals("ReentrantEventHater expected 2 events",
43         Lists.<Object>newArrayList(FIRST, SECOND), hater.eventsReceived);
44   }
45 
46   public class ReentrantEventsHater {
47     boolean ready = true;
48     List<Object> eventsReceived = Lists.newArrayList();
49 
50     @Subscribe
listenForStrings(String event)51     public void listenForStrings(String event) {
52       eventsReceived.add(event);
53       ready = false;
54       try {
55         bus.post(SECOND);
56       } finally {
57         ready = true;
58       }
59     }
60 
61     @Subscribe
listenForDoubles(Double event)62     public void listenForDoubles(Double event) {
63       assertTrue("I received an event when I wasn't ready!", ready);
64       eventsReceived.add(event);
65     }
66   }
67 
testEventOrderingIsPredictable()68   public void testEventOrderingIsPredictable() {
69     EventProcessor processor = new EventProcessor();
70     bus.register(processor);
71 
72     EventRecorder recorder = new EventRecorder();
73     bus.register(recorder);
74 
75     bus.post(FIRST);
76 
77     assertEquals("EventRecorder expected events in order",
78         Lists.<Object>newArrayList(FIRST, SECOND), recorder.eventsReceived);
79   }
80 
81   public class EventProcessor {
listenForStrings(String event)82     @Subscribe public void listenForStrings(String event) {
83       bus.post(SECOND);
84     }
85   }
86 
87   public class EventRecorder {
88     List<Object> eventsReceived = Lists.newArrayList();
listenForEverything(Object event)89     @Subscribe public void listenForEverything(Object event) {
90       eventsReceived.add(event);
91     }
92   }
93 }
94