@Test
@SuppressWarnings("checkstyle:IllegalCatch")
public void testOnLeaderLocationChanged() {

    final LeaderLocationListener listener1 = mock(LeaderLocationListener.class);
    doThrow(new RuntimeException("Failed")).when(listener1).onLeaderLocationChanged(any());
    final LeaderLocationListener listener2 = mock(LeaderLocationListener.class);
    doNothing().when(listener2).onLeaderLocationChanged(any());
    final LeaderLocationListener listener3 = mock(LeaderLocationListener.class);
    doNothing().when(listener3).onLeaderLocationChanged(any());
    final LeaderLocationListenerRegistration<?> reg1 = shardAccess.registerLeaderLocationListener(listener1);
    final LeaderLocationListenerRegistration<?> reg2 = shardAccess.registerLeaderLocationListener(listener2);
    final LeaderLocationListenerRegistration<?> reg3 = shardAccess.registerLeaderLocationListener(listener3);
    // Error in listener1 should not affect dispatching change to other listeners
    shardAccess.onLeaderLocationChanged(LeaderLocation.LOCAL);
    verify(listener1).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    verify(listener2).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    verify(listener3).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    // Closed listeners shouldn't see new leader location changes
    reg1.close();
    reg2.close();
    shardAccess.onLeaderLocationChanged(LeaderLocation.REMOTE);
    verify(listener3).onLeaderLocationChanged(eq(LeaderLocation.REMOTE));
    verifyNoMoreInteractions(listener1);
    verifyNoMoreInteractions(listener2);
    // Closed shard access should not dispatch any new events
    shardAccess.close();
    shardAccess.onLeaderLocationChanged(LeaderLocation.UNKNOWN);
    verifyNoMoreInteractions(listener1);
    verifyNoMoreInteractions(listener2);
    verifyNoMoreInteractions(listener3);
    reg3.close();
}