feat(event): Add unregisterAll method to EventService and EventHandlerRegistry

This commit is contained in:
extio-de
2025-12-26 15:57:57 +01:00
parent a9ddcb173d
commit 64adf9c9f9
4 changed files with 28 additions and 0 deletions

View File

@@ -22,6 +22,12 @@ public class EventHandlerRegistry {
consumers.removeIf(ec -> ec.id().equals(consumerId));
}
}
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) {
return this.handlers.get(eventClass);

View File

@@ -7,6 +7,8 @@ public interface EventService {
<T extends Event> void register(Class<T> eventClass, String consumerId, Consumer<T> consumer);
void unregister(Class<? extends Event> eventClass, String consumerId);
void unregisterAll(String consumerId);
void fire(Event event);

View File

@@ -22,6 +22,11 @@ public class EventServiceImpl implements EventService {
this.registry.unregister(eventClass, consumerId);
}
@Override
public void unregisterAll(final String consumerId) {
this.registry.unregisterAll(consumerId);
}
@Override
public void fire(final Event event) {
this.executor.submit(event);

View File

@@ -107,6 +107,21 @@ class EventServiceTest {
.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
void testConsumerReregistration() throws InterruptedException {
final var firstLatch = new CountDownLatch(1);