feat(event): Add unregisterAll method to EventService and EventHandlerRegistry
This commit is contained in:
@@ -23,6 +23,12 @@ public class EventHandlerRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void unregisterAll(final String consumerId) {
|
||||||
|
for (final var consumers : this.handlers.values()) {
|
||||||
|
consumers.removeIf(ec -> ec.id().equals(consumerId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<EventConsumer> getHandlers(final Class<? extends Event> eventClass) {
|
List<EventConsumer> getHandlers(final Class<? extends Event> eventClass) {
|
||||||
return this.handlers.get(eventClass);
|
return this.handlers.get(eventClass);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ public interface EventService {
|
|||||||
|
|
||||||
void unregister(Class<? extends Event> eventClass, String consumerId);
|
void unregister(Class<? extends Event> eventClass, String consumerId);
|
||||||
|
|
||||||
|
void unregisterAll(String consumerId);
|
||||||
|
|
||||||
void fire(Event event);
|
void fire(Event event);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ public class EventServiceImpl implements EventService {
|
|||||||
this.registry.unregister(eventClass, consumerId);
|
this.registry.unregister(eventClass, consumerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unregisterAll(final String consumerId) {
|
||||||
|
this.registry.unregisterAll(consumerId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fire(final Event event) {
|
public void fire(final Event event) {
|
||||||
this.executor.submit(event);
|
this.executor.submit(event);
|
||||||
|
|||||||
@@ -107,6 +107,21 @@ class EventServiceTest {
|
|||||||
.untilAsserted(() -> assertThat(this.receivedEvents).isEmpty());
|
.untilAsserted(() -> assertThat(this.receivedEvents).isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUnregisterAll() throws InterruptedException {
|
||||||
|
this.eventService.register(TestEvent.class, "consumer1", event -> this.receivedEvents.add(event));
|
||||||
|
this.eventService.register(OtherTestEvent.class, "consumer1", event -> this.receivedEvents.add(event));
|
||||||
|
|
||||||
|
this.eventService.unregisterAll("consumer1");
|
||||||
|
|
||||||
|
this.eventService.fire(new TestEvent("test"));
|
||||||
|
this.eventService.fire(new OtherTestEvent("other"));
|
||||||
|
|
||||||
|
await().pollDelay(Duration.ofMillis(50))
|
||||||
|
.atMost(Duration.ofMillis(200))
|
||||||
|
.untilAsserted(() -> assertThat(this.receivedEvents).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConsumerReregistration() throws InterruptedException {
|
void testConsumerReregistration() throws InterruptedException {
|
||||||
final var firstLatch = new CountDownLatch(1);
|
final var firstLatch = new CountDownLatch(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user