From dc6e3e2eb293f41b48fa04978a68de93a3093041 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 a49d82f95ea05..1d7969dbfde9b 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java +++ b/server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java @@ -709,10 +709,16 @@ public void testAutomaticRefresh() throws InterruptedException { // 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(() -> {