Add DeadEvent #9

Merged
kske merged 2 commits from f/dead-event into develop 2021-02-21 09:16:33 +01:00
4 changed files with 34 additions and 11 deletions
Showing only changes of commit b2fe3a9d6c - Show all commits

View File

@ -18,8 +18,13 @@ public final class DeadEvent {
this.event = event; this.event = event;
} }
@Override
public String toString() {
return String.format("DeadEvent[eventBus=%s, event=%s]", eventBus, event);
}
/** /**
* @return the event bus that originated this event * @return the event bus that dispatched this event
delvh marked this conversation as resolved Outdated
Outdated
Review
 *  that dispatched this event
```java * … that dispatched this event ```
Outdated
Review

I have ammended my original commit.

I have ammended my original commit.
* @since 1.1.0 * @since 1.1.0
*/ */
public EventBus getEventBus() { return eventBus; } public EventBus getEventBus() { return eventBus; }

View File

@ -84,7 +84,13 @@ public final class EventBus {
handlers.next().execute(event); handlers.next().execute(event);
} }
} else if (!(event instanceof DeadEvent)) { } else if (!(event instanceof DeadEvent)) {
kske marked this conversation as resolved
Review

this would be a little bit different then.

this would be a little bit different then.
Review

That's why I use the if statement. I could use a boolean flag instead, but the iterator logic should be more efficient and arguably more readable here.

That's why I use the if statement. I could use a boolean flag instead, but the iterator logic should be more efficient and arguably more readable here.
// Dispatch dead event
dispatch(new DeadEvent(this, event)); dispatch(new DeadEvent(this, event));
} else {
// Warn about the dead event not being handled
logger.log(Level.WARNING, "{0} not handled", event);
} }
// Reset dispatch state // Reset dispatch state

View File

@ -2,7 +2,7 @@ package dev.kske.eventbus.core;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.Test;
/** /**
* Tests the dispatching of a dead event if an event could not be delivered. * Tests the dispatching of a dead event if an event could not be delivered.
@ -12,20 +12,32 @@ import org.junit.jupiter.api.*;
*/ */
class DeadTest { class DeadTest {
EventBus bus; EventBus bus = new EventBus();
String event = "This event has no handler"; String event = "This event has no handler";
boolean deadEventHandled; boolean deadEventHandled;
@BeforeEach /**
void registerListener() { * Tests dead event delivery.
bus = new EventBus(); *
bus.registerListener(this); * @since 1.1.0
} */
@Test @Test
void testDeadEvent() { void testDeadEvent() {
bus.registerListener(this);
bus.dispatch(event); bus.dispatch(event);
assertTrue(deadEventHandled); assertTrue(deadEventHandled);
bus.removeListener(this);
}
/**
* Tests how the event bus reacts to an unhandled dead event. This should not lead to an
* exception or endless recursion and instead be logged.
*
* @since 1.1.0
*/
@Test
void testUnhandledDeadEvent() {
bus.dispatch(event);
} }
@Event @Event

View File

@ -27,8 +27,8 @@ class DispatchTest {
} }
/** /**
* Tests {@link EventBus#dispatch(Object)} with multiple handler priorities, a subtype handler * Tests {@link EventBus#dispatch(Object)} with multiple handler priorities, a polymorphic
* and a static handler. * handler and a static handler.
* *
* @since 0.0.1 * @since 0.0.1
*/ */