Sfoglia il codice sorgente

Make ChildEventBusManager.disconnect async.

This allows other async events a chance to be popagated up to
the parent, rather than the bus abruptly being cut off with
a bunch of pending events.

Change-Id: I2a60826174d33240c0a1016b41ef881efdd97fed
Reviewed-on: http://gerrit.dmdirc.com/3818
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Chris Smith 9 anni fa
parent
commit
d5b9bebef5

+ 19
- 4
src/com/dmdirc/util/ChildEventBusManager.java Vedi File

@@ -31,7 +31,8 @@ import net.engio.mbassy.listener.Handler;
31 31
 import static com.google.common.base.Preconditions.checkNotNull;
32 32
 
33 33
 /**
34
- * Utility for creating and managing instances of {@link com.dmdirc.DMDircMBassador} that are slaved to a parent bus.
34
+ * Utility for creating and managing instances of {@link DMDircMBassador} that are slaved to a
35
+ * parent bus.
35 36
  * <p>
36 37
  * Any events sent on the child bus will be propagated up to the parent bus.
37 38
  */
@@ -59,10 +60,11 @@ public class ChildEventBusManager {
59 60
     /**
60 61
      * Disconnects the child event bus from the parent.
61 62
      * <p>
62
-     * After this method is called, no further events will be passed to the parent.
63
+     * The child will be disconnected asynchronously, to allow any pending async events to be
64
+     * dispatched. After the bus is disconnected, no future events will be passed to the parent.
63 65
      */
64 66
     public void disconnect() {
65
-        child.unsubscribe(propagator);
67
+        child.publishAsync(new ChildEventBusDisconnectingEvent());
66 68
     }
67 69
 
68 70
     /**
@@ -78,9 +80,22 @@ public class ChildEventBusManager {
78 80
 
79 81
         @Handler
80 82
         public void handleEvent(final DMDircEvent event) {
81
-            parent.publish(event);
83
+            if (!(event instanceof ChildEventBusDisconnectingEvent)) {
84
+                // Don't propagate our private event
85
+                parent.publish(event);
86
+            }
82 87
         }
83 88
 
89
+        // Allow all other handlers on the child bus to process this first
90
+        @Handler(priority = Integer.MIN_VALUE)
91
+        public void handleDisconnect(final ChildEventBusDisconnectingEvent event) {
92
+            child.unsubscribe(propagator);
93
+        }
94
+
95
+    }
96
+
97
+    private static class ChildEventBusDisconnectingEvent extends DMDircEvent {
98
+
84 99
     }
85 100
 
86 101
 }

+ 8
- 3
test/com/dmdirc/util/ChildEventBusManagerTest.java Vedi File

@@ -35,13 +35,12 @@ import static org.junit.Assert.assertSame;
35 35
 
36 36
 public class ChildEventBusManagerTest {
37 37
 
38
-    private DMDircMBassador parent;
39 38
     private ChildEventBusManager manager;
40 39
     private StubHandler handler;
41 40
 
42 41
     @Before
43 42
     public void setup() {
44
-        parent = new DMDircMBassador();
43
+        final DMDircMBassador parent = new DMDircMBassador();
45 44
         manager = new ChildEventBusManager(parent);
46 45
         handler = new StubHandler();
47 46
         parent.subscribe(handler);
@@ -54,9 +53,15 @@ public class ChildEventBusManagerTest {
54 53
     }
55 54
 
56 55
     @Test
57
-    public void testDoesNotPropagateAfterDisconnect() {
56
+    public void testDoesNotPropagateAfterDisconnect() throws InterruptedException {
58 57
         manager.connect();
59 58
         manager.disconnect();
59
+
60
+        // Wait for the asynchronous disconnect to go through. This is a bit lame.
61
+        do {
62
+            Thread.sleep(100);
63
+        } while (manager.getChildBus().hasPendingMessages());
64
+
60 65
         manager.getChildBus().publish(new StubEvent());
61 66
         assertNull(handler.received);
62 67
     }

Loading…
Annulla
Salva