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() {
logger.log(Level.INFO, "Clearing event listeners");
bindings.clear();
bindingCache.clear();
registeredListeners.clear();
}

View File

@ -15,7 +15,6 @@ import org.junit.jupiter.api.*;
class DispatchTest {
EventBus bus;
static int hits;
/**
* 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);
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() {