Browse Source

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.
Jason Tedor 7 years ago
parent
commit
d806a0e59d

+ 9 - 6
server/src/test/java/org/elasticsearch/index/shard/GlobalCheckpointListenersTests.java

@@ -49,10 +49,13 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import static org.elasticsearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
 import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
+import static org.hamcrest.Matchers.any;
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasToString;
 import static org.hamcrest.Matchers.instanceOf;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.times;
@@ -561,19 +564,19 @@ public class GlobalCheckpointListenersTests extends ESTestCase {
     }
 
     public void testFailingListenerAfterTimeout() throws InterruptedException {
+        final CountDownLatch latch = new CountDownLatch(1);
         final Logger mockLogger = mock(Logger.class);
+        doAnswer(invocationOnMock -> {
+            latch.countDown();
+            return null;
+        }).when(mockLogger).warn(argThat(any(String.class)), argThat(any(RuntimeException.class)));
         final GlobalCheckpointListeners globalCheckpointListeners =
                 new GlobalCheckpointListeners(shardId, Runnable::run, scheduler, mockLogger);
-        final CountDownLatch latch = new CountDownLatch(1);
         final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
         globalCheckpointListeners.add(
                 NO_OPS_PERFORMED,
                 (g, e) -> {
-                    try {
-                        throw new RuntimeException("failure");
-                    } finally {
-                        latch.countDown();
-                    }
+                    throw new RuntimeException("failure");
                 },
                 timeout);
         latch.await();