@Override public void removeHandler() { synchronized (ResettableEventBus.this) { registration.removeHandler(); registrations.remove(registration); } } };
/** * Remove all handlers that have been added through this wrapper. */ public synchronized void reset() { for (HandlerRegistration registration : registrations) { registration.removeHandler(); } registrations.clear(); } }
@Override public void destroy() { registerHandler.removeHandler(); super.destroy(); endpoints.clear(); }
@Override public void destroy() { super.destroy(); registerHandler.removeHandler(); endpoints.clear(); log.info("RestJerseyDispatcherServlet disposed."); }
@Override public void handleEvent(TestEvent event) { super.handleEvent(event); handlerRegistration.removeHandler(); } }
@Override public void run() { try { startSignal.await(); for (int i = 0; i < numberOfIterations; i++) { List<InvocationCountingTestEventHandler> handlers = new ArrayList<InvocationCountingTestEventHandler>(); List<HandlerRegistration> registrations = new ArrayList<HandlerRegistration>(); // Add handlers for (int j = 0; j < numberOfHandlers; j++) { InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); handlers.add(handler); registrations.add(eventBus.addHandler(TestEvent.class, handler)); } // Fire event eventBus.fireEvent(new TestEvent()); // Remove handlers for (HandlerRegistration handler : registrations) { handler.removeHandler(); } // Make sure that every handler was invoked at least once for (InvocationCountingTestEventHandler handler : handlers) { Assert.assertTrue(handler.getInvocationCount() > 0); } } } catch (Throwable e) { threadEncounteredException.set(true); e.printStackTrace(); } } });
@Test public void testDoesNotDispatchToRemovedHandler() { // GIVEN ResettableEventBus eventBus = new ResettableEventBus(new SimpleEventBus()); InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); HandlerRegistration registration = eventBus.addHandler(TestEvent.class, handler); registration.removeHandler(); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(0, handler.getInvocationCount()); }
@Test public void testCanRemoveHandlerViaReturnedRegistrationObject() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); HandlerRegistration registration = collection.add(eventHandler); collection.dispatch(new TestEvent()); assertEquals(1, eventHandler.getInvocationCount()); registration.removeHandler(); // WHEN collection.dispatch(new TestEvent()); // THEN (still 1) assertEquals(1, eventHandler.getInvocationCount()); }