@@ -291,4 +291,29 @@ public EventBus<RecursiveTestEvent> getDefaultBus() {
291291 new RecursiveTestEvent (false ).post ();
292292 Assertions .assertEquals (1 , hits .get (), "Listener should have been called once" );
293293 }
294+
295+ /**
296+ * Tests that {@link EventBus#hasListeners()} works as expected.
297+ */
298+ @ Test
299+ public void testHasListeners () {
300+ record TestEvent () implements RecordEvent {
301+ static final EventBus <TestEvent > BUS = EventBus .create (TestEvent .class );
302+ }
303+ record CancellableTestEvent () implements Cancellable , RecordEvent {
304+ static final CancellableEventBus <CancellableTestEvent > BUS = CancellableEventBus .create (CancellableTestEvent .class );
305+ }
306+
307+ Assertions .assertFalse (TestEvent .BUS .hasListeners (), "TestEvent.BUS should not have listeners yet" );
308+ var listener = TestEvent .BUS .addListener (event -> {});
309+ Assertions .assertTrue (TestEvent .BUS .hasListeners (), "TestEvent.BUS should have listeners" );
310+ TestEvent .BUS .removeListener (listener );
311+ Assertions .assertFalse (TestEvent .BUS .hasListeners (), "TestEvent.BUS should no longer have listeners" );
312+
313+ Assertions .assertFalse (CancellableTestEvent .BUS .hasListeners (), "CancellableTestEvent.BUS should not have listeners yet" );
314+ listener = CancellableTestEvent .BUS .addListener (event -> true );
315+ Assertions .assertTrue (CancellableTestEvent .BUS .hasListeners (), "CancellableTestEvent.BUS should have listeners" );
316+ CancellableTestEvent .BUS .removeListener (listener );
317+ Assertions .assertFalse (CancellableTestEvent .BUS .hasListeners (), "CancellableTestEvent.BUS should no longer have listeners" );
318+ }
294319}
0 commit comments