@Inject public AppEventTestImpl(AppContext ctx, AppView view, @Named(AppEventBus.NAME) EventBus eventBus, @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus) { super(ctx, view); this.eventBus = eventBus; this.admincentralEventBus = admincentralEventBus; // Register handler = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler); }
@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 testHandlerAddedWhileDispatchingIsNotCalled() { // GIVEN final SimpleEventBus eventBus = new SimpleEventBus(); final InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler handler1 = new InvocationCountingTestEventHandler() { @Override public void handleEvent(TestEvent event) { super.handleEvent(event); eventBus.addHandler(TestEvent.class, handler2); } }; eventBus.addHandler(TestEvent.class, handler1); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(0, handler2.getInvocationCount()); }
@Test public void testHandlersAddedWhileDispatchedAreNotInvoked() { // GIVEN final EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); final InvocationCountingTestEventHandler secondHandler = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler firstHandler = new InvocationCountingTestEventHandler() { @Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.add(secondHandler); } }; collection.add(firstHandler); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, firstHandler.getInvocationCount()); assertEquals(0, secondHandler.getInvocationCount()); }
@Test public void testHandlersRemovedWhileDispatchedAreStillInvoked() { // GIVEN final EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); final InvocationCountingTestEventHandler secondHandler = new InvocationCountingTestEventHandler(); InvocationCountingTestEventHandler firstHandler = new InvocationCountingTestEventHandler() { @Override public synchronized void handleEvent(TestEvent event) { super.handleEvent(event); collection.remove(secondHandler); } }; collection.add(firstHandler); collection.add(secondHandler); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, collection.size()); assertEquals(1, firstHandler.getInvocationCount()); assertEquals(1, secondHandler.getInvocationCount()); } }
@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()); }
InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); handlers.add(handler); eventBus.addHandler(TestEvent.class, handler);
@Test public void testMultipleRegistrationOfTheSameHandlerWillNotResultInMultipleInvocations() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); InvocationCountingTestEventHandler handler = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); eventBus.addHandler(TestEvent.class, handler); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler.getInvocationCount()); }
@Test public void testHandlerRemovedWhileDispatchingIsCalled() { // GIVEN SimpleEventBus eventBus = new SimpleEventBus(); RemoveEventHandler handler1 = new RemoveEventHandler(); InvocationCountingTestEventHandler handler2 = new InvocationCountingTestEventHandler(); eventBus.addHandler(TestEvent.class, handler1); handler1.handlerRegistration = eventBus.addHandler(TestEvent.class, handler2); // WHEN eventBus.fireEvent(new TestEvent()); // THEN assertEquals(1, handler1.getInvocationCount()); assertEquals(1, handler2.getInvocationCount()); }
@Test public void testCanRemoveHandler() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); collection.add(eventHandler); collection.dispatch(new TestEvent()); assertEquals(1, eventHandler.getInvocationCount()); collection.remove(eventHandler); // WHEN collection.dispatch(new TestEvent()); // THEN (still 1) assertEquals(1, eventHandler.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()); }
@Test public void testCanDispatchEvent() { // GIVEN EventHandlerCollection<TestEventHandler> collection = new EventHandlerCollection<TestEventHandler>(); InvocationCountingTestEventHandler eventHandler = new InvocationCountingTestEventHandler(); assertTrue(collection.isEmpty()); assertEquals(0, collection.size()); collection.add(eventHandler); assertFalse(collection.isEmpty()); assertEquals(1, collection.size()); // WHEN collection.dispatch(new TestEvent()); // THEN assertEquals(1, eventHandler.getInvocationCount()); }