From e9759b2b331fbeda25b3a54748cf616bd0421afb Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Tue, 20 Aug 2019 09:13:46 -0400 Subject: [PATCH] Wait for background refresh in testAutomaticRefresh (#45661) If the background refresh is running, but not finished yet then the document might not be visible to the next search. Thus, if scheduledRefresh returns false, we need to wait until the background refresh is done. Closes #45571 --- .../elasticsearch/index/shard/IndexShardIT.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java b/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java index 690ec48bcc6..58430198642 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java +++ b/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java @@ -710,10 +710,16 @@ public class IndexShardIT extends ESSingleNodeTestCase { // with ZERO we are guaranteed to see the doc since we will wait for a refresh in the background assertFalse(hasRefreshed); assertTrue(shard.isSearchIdle()); - } else if (randomTimeValue == null){ - // with null we are guaranteed to see the doc since do execute the refresh. - // we can't assert on hasRefreshed since it might have been refreshed in the background on the shard concurrently - assertFalse(shard.isSearchIdle()); + } else { + if (randomTimeValue == null) { + assertFalse(shard.isSearchIdle()); + } + // we can't assert on hasRefreshed since it might have been refreshed in the background on the shard concurrently. + // and if the background refresh wins the refresh race (both call maybeRefresh), the document might not be visible + // until the background refresh is done. + if (hasRefreshed == false) { + ensureNoPendingScheduledRefresh(indexService.getThreadPool()); + } } CountDownLatch started = new CountDownLatch(1); Thread t = new Thread(() -> {