Test binding cache
All checks were successful
zdm/event-bus/pipeline/head This commit looks good

This commit is contained in:
Kai S. K. Engelbart 2022-01-18 13:44:33 +01:00
parent 5468bddb35
commit ee9d08b2b8
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 29 additions and 16 deletions

View File

@ -457,6 +457,7 @@ public final class EventBus {
public void clearListeners() { public void clearListeners() {
logger.log(Level.INFO, "Clearing event listeners"); logger.log(Level.INFO, "Clearing event listeners");
bindings.clear(); bindings.clear();
bindingCache.clear();
registeredListeners.clear(); registeredListeners.clear();
} }

View File

@ -15,7 +15,6 @@ import org.junit.jupiter.api.*;
class DispatchTest { class DispatchTest {
EventBus bus; EventBus bus;
static int hits;
/** /**
* Constructs an event bus and registers this test instance as an event listener. * Constructs an event bus and registers this test instance as an event listener.
@ -27,8 +26,8 @@ class DispatchTest {
bus = new EventBus(); bus = new EventBus();
bus.registerListener(this); bus.registerListener(this);
bus.registerListener(SimpleEvent.class, e -> { bus.registerListener(SimpleEvent.class, e -> {
++hits; e.increment();
assertEquals(4, hits); assertEquals(3, e.getCounter());
}); });
} }
@ -56,24 +55,37 @@ class DispatchTest {
assertEquals( assertEquals(
"Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n" "Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n"
+ "==========================================================================================\n" + "==========================================================================================\n"
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=true, priority=200, method=void dev.kske.eventbus.core.DispatchTest.onSimpleEventFirst(), useParameter=false]\n" + "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=true, priority=200, method=void dev.kske.eventbus.core.DispatchTest.onSimpleEventFirst(dev.kske.eventbus.core.SimpleEvent), useParameter=true]\n"
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=150, method=static void dev.kske.eventbus.core.DispatchTest.onSimpleEventSecond(), useParameter=false]\n" + "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=150, method=static void dev.kske.eventbus.core.DispatchTest.onSimpleEventSecond(dev.kske.eventbus.core.SimpleEvent), useParameter=true]\n"
+ "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n" + "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n"
+ "==========================================================================================", + "==========================================================================================",
executionOrder); executionOrder);
} }
@Event(SimpleEvent.class) /**
@Priority(200) * Tests whether the handlers bound to an event type are correct when retrieved from the binding
void onSimpleEventFirst() { * cache. On the second call of {@link EventBus#debugExecutionOrder(Class)} the cache is used.
++hits; *
assertTrue(hits == 1 || hits == 2); * @since 1.3.0
*/
@Test
void testBindingCache() {
String executionOrder = bus.debugExecutionOrder(SimpleEventSub.class);
System.out.println(executionOrder);
assertEquals(executionOrder, bus.debugExecutionOrder(SimpleEventSub.class));
} }
@Event(SimpleEvent.class) @Event
@Priority(200)
void onSimpleEventFirst(SimpleEvent event) {
event.increment();
assertTrue(event.getCounter() == 1 || event.getCounter() == 2);
}
@Event
@Polymorphic(false) @Polymorphic(false)
static void onSimpleEventSecond() { static void onSimpleEventSecond(SimpleEvent event) {
++hits; event.increment();
assertEquals(3, hits); assertEquals(2, event.getCounter());
} }
} }

View File

@ -14,7 +14,7 @@ class SimpleEvent {
@Override @Override
public String toString() { public String toString() {
return String.format("SimpleEvent[%d]", counter); return String.format("%s[%d]", getClass().getSimpleName(), counter);
} }
void increment() { void increment() {