Browse Source

Merge pull request #17327 from s1monw/harden_test

Improve test to not rely on thread slowness

We have to swap the second latch before we count it down otherwise
threads might be faster than the test. This has happend on a recent
CI failure: https://elasticsearch-ci.elastic.co/job/elastic+elasticsearch+master+multijob-os-compatibility/os=ubuntu/121/console

This commit also adds a synchronized on the close method since it's
canceling and modifying a member varialbe that is assigned under lock.
Simon Willnauer 9 years ago
parent
commit
d0413f0f0e

+ 1 - 1
core/src/main/java/org/elasticsearch/index/IndexService.java

@@ -780,7 +780,7 @@ public final class IndexService extends AbstractIndexComponent implements IndexC
         }
         }
 
 
         @Override
         @Override
-        public void close() {
+        public synchronized void close() {
             if (closed.compareAndSet(false, true)) {
             if (closed.compareAndSet(false, true)) {
                 FutureUtils.cancel(scheduledFuture);
                 FutureUtils.cancel(scheduledFuture);
                 scheduledFuture = null;
                 scheduledFuture = null;

+ 4 - 3
core/src/test/java/org/elasticsearch/index/IndexServiceTests.java

@@ -187,12 +187,13 @@ public class IndexServiceTests extends ESSingleNodeTestCase {
                 return ThreadPool.Names.GENERIC;
                 return ThreadPool.Names.GENERIC;
             }
             }
         };
         };
+
         latch.get().await();
         latch.get().await();
         latch.set(new CountDownLatch(1));
         latch.set(new CountDownLatch(1));
         assertEquals(1, count.get());
         assertEquals(1, count.get());
-        latch2.get().countDown();
-        latch2.set(new CountDownLatch(1));
-
+        // here we need to swap first before we let it go otherwise threads might be very fast and run that task twice due to
+        // random exception and the schedule interval is 1ms
+        latch2.getAndSet(new CountDownLatch(1)).countDown();
         latch.get().await();
         latch.get().await();
         assertEquals(2, count.get());
         assertEquals(2, count.get());
         task.close();
         task.close();