Demonstrates ConcurrentModificationDetector: an ArrayList of event
listeners is iterated by fireEvent() while another thread calls register(),
causing ConcurrentModificationException.
EventBusService stores listeners in a plain ArrayList. fireEvent()
iterates the list with a for-each loop. register() adds to the same list.
When both operations run simultaneously, the ArrayList's modCount check fails
and ConcurrentModificationException is thrown mid-dispatch, leaving some
listeners uncalled.
- Remove
@DisabledfromtestEventBus_concurrent_detectsModification. - Run:
mvn testor./gradlew test - The test fails with a ConcurrentModificationDetector report showing that the listener list was modified while an iteration was in progress.
Fix: protect both register() and fireEvent() with synchronized(listeners)
blocks, or replace ArrayList with CopyOnWriteArrayList (appropriate here
because reads dominate writes).