Handler Caching #37

Merged
kske merged 5 commits from f/handler-caching into develop 2022-01-18 17:11:38 +01:00
3 changed files with 29 additions and 16 deletions
Showing only changes of commit ee9d08b2b8 - Show all commits

View File

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

View File

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

Why?

Why?
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)
static void onSimpleEventSecond() {
++hits;
assertEquals(3, hits);
static void onSimpleEventSecond(SimpleEvent event) {
event.increment();
assertEquals(2, event.getCounter());
}
}

View File

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