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

@ -12,7 +12,8 @@ import org.junit.jupiter.api.*;
*/
class EventBusTest implements EventListener {
int hits;
int hits;
static int canceledHits;
@BeforeEach
public void registerListener() {
@ -25,11 +26,15 @@ class EventBusTest implements EventListener {
EventBus.getInstance().dispatch(new SimpleEvent());
}
@Event(
eventType = SimpleEvent.class,
includeSubtypes = true,
priority = 200
)
@Test
void testCancellation() {
var test2 = new EventBusTest();
test2.registerListener();
EventBus.getInstance().dispatch(new SimpleCancelEvent());
assertTrue(canceledHits == 1);
}
@Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200)
private void onSimpleEventFirst() {
++hits;
assertTrue(hits == 1 || hits == 2);
@ -46,4 +51,16 @@ class EventBusTest implements EventListener {
++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 {}