2626import org .mockito .ArgumentCaptor ;
2727
2828import java .io .IOException ;
29+ import java .io .UncheckedIOException ;
2930import java .util .List ;
3031import java .util .concurrent .BrokenBarrierException ;
3132import java .util .concurrent .CopyOnWriteArrayList ;
@@ -350,11 +351,21 @@ public void testConcurrency() throws BrokenBarrierException, InterruptedExceptio
350351 globalCheckpointListeners .globalCheckpointUpdated (globalCheckpoint .get ());
351352 final CyclicBarrier barrier = new CyclicBarrier (3 );
352353 final int numberOfIterations = randomIntBetween (1 , 1024 );
353-
354+ final AtomicBoolean closed = new AtomicBoolean ();
354355 final Thread updatingThread = new Thread (() -> {
355356 awaitQuietly (barrier );
356357 for (int i = 0 ; i < numberOfIterations ; i ++) {
357- globalCheckpointListeners .globalCheckpointUpdated (globalCheckpoint .incrementAndGet ());
358+ if (rarely () && closed .get () == false ) {
359+ closed .set (true );
360+ try {
361+ globalCheckpointListeners .close ();
362+ } catch (final IOException e ) {
363+ throw new UncheckedIOException (e );
364+ }
365+ }
366+ if (closed .get () == false ) {
367+ globalCheckpointListeners .globalCheckpointUpdated (globalCheckpoint .incrementAndGet ());
368+ }
358369 }
359370 awaitQuietly (barrier );
360371 });
@@ -381,7 +392,10 @@ public void testConcurrency() throws BrokenBarrierException, InterruptedExceptio
381392 barrier .await ();
382393 barrier .await ();
383394 // one last update to ensure all listeners are notified
384- globalCheckpointListeners .globalCheckpointUpdated (globalCheckpoint .incrementAndGet ());
395+ if (closed .get () == false ) {
396+ globalCheckpointListeners .globalCheckpointUpdated (globalCheckpoint .incrementAndGet ());
397+ }
398+ assertThat (globalCheckpointListeners .pendingListeners (), equalTo (0 ));
385399 executor .shutdown ();
386400 executor .awaitTermination (Long .MAX_VALUE , TimeUnit .SECONDS );
387401 for (final AtomicBoolean invocation : invocations ) {
0 commit comments