Add Test for Cancellation

This commit is contained in:
Leon Hofmeister 2020-11-23 23:42:17 +01:00
parent 9d1707de5b
commit 8aefb43823
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
2 changed files with 32 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import org.junit.jupiter.api.*;
class EventBusTest implements EventListener { class EventBusTest implements EventListener {
int hits; int hits;
static int canceledHits;
@BeforeEach @BeforeEach
public void registerListener() { public void registerListener() {
@ -25,11 +26,15 @@ class EventBusTest implements EventListener {
EventBus.getInstance().dispatch(new SimpleEvent()); EventBus.getInstance().dispatch(new SimpleEvent());
} }
@Event( @Test
eventType = SimpleEvent.class, void testCancellation() {
includeSubtypes = true, var test2 = new EventBusTest();
priority = 200 test2.registerListener();
) EventBus.getInstance().dispatch(new SimpleCancelEvent());
assertTrue(canceledHits == 1);
}
@Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200)
private void onSimpleEventFirst() { private void onSimpleEventFirst() {
++hits; ++hits;
assertTrue(hits == 1 || hits == 2); assertTrue(hits == 1 || hits == 2);
@ -46,4 +51,16 @@ class EventBusTest implements EventListener {
++hits; ++hits;
assertEquals(4, hits); assertEquals(4, hits);
} }
@Event(eventType = SimpleCancelEvent.class, priority = 500)
private void onSimpleCancelFirst() {
++canceledHits;
assertTrue(canceledHits == 1);
EventBus.getInstance().cancel();
}
@Event(eventType = SimpleCancelEvent.class, priority = 200)
private void onSimpleCancelSecond() {
fail();
}
} }

View File

@ -0,0 +1,9 @@
package dev.kske.eventbus;
/**
* A simple event for testing purposes that will get cancelled during propagation.
*
* @author Leon Hofmeister
* @since 0.1
*/
public class SimpleCancelEvent implements IEvent {}