Skip to content

Commit d806a0e

Browse files
committed
Fix race in global checkpoint listeners test
This race can occur if the latch from the listener notifies the test thread and the test thread races ahead before the scheduler thread has a chance to emit the log message. This commit fixes this test by not counting down the latch until after the log message we are going to assert on has been emitted.
1 parent 6dfe54c commit d806a0e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

server/src/test/java/org/elasticsearch/index/shard/GlobalCheckpointListenersTests.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@
4949

5050
import static org.elasticsearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
5151
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
52+
import static org.hamcrest.Matchers.any;
5253
import static org.hamcrest.Matchers.containsString;
5354
import static org.hamcrest.Matchers.equalTo;
5455
import static org.hamcrest.Matchers.hasToString;
5556
import static org.hamcrest.Matchers.instanceOf;
57+
import static org.mockito.Matchers.argThat;
58+
import static org.mockito.Mockito.doAnswer;
5659
import static org.mockito.Mockito.mock;
5760
import static org.mockito.Mockito.reset;
5861
import static org.mockito.Mockito.times;
@@ -561,19 +564,19 @@ public void testTimeoutNotificationUsesExecutor() throws InterruptedException {
561564
}
562565

563566
public void testFailingListenerAfterTimeout() throws InterruptedException {
567+
final CountDownLatch latch = new CountDownLatch(1);
564568
final Logger mockLogger = mock(Logger.class);
569+
doAnswer(invocationOnMock -> {
570+
latch.countDown();
571+
return null;
572+
}).when(mockLogger).warn(argThat(any(String.class)), argThat(any(RuntimeException.class)));
565573
final GlobalCheckpointListeners globalCheckpointListeners =
566574
new GlobalCheckpointListeners(shardId, Runnable::run, scheduler, mockLogger);
567-
final CountDownLatch latch = new CountDownLatch(1);
568575
final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
569576
globalCheckpointListeners.add(
570577
NO_OPS_PERFORMED,
571578
(g, e) -> {
572-
try {
573-
throw new RuntimeException("failure");
574-
} finally {
575-
latch.countDown();
576-
}
579+
throw new RuntimeException("failure");
577580
},
578581
timeout);
579582
latch.await();

0 commit comments

Comments
 (0)