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.
This commit is contained in:
Simon Willnauer 2016-03-24 16:51:48 +01:00
commit d0413f0f0e
2 changed files with 5 additions and 4 deletions

View File

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

View File

@ -187,12 +187,13 @@ public class IndexServiceTests extends ESSingleNodeTestCase {
return ThreadPool.Names.GENERIC;
}
};
latch.get().await();
latch.set(new CountDownLatch(1));
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();
assertEquals(2, count.get());
task.close();