diff --git a/server/src/test/java/org/elasticsearch/search/SearchServiceTests.java b/server/src/test/java/org/elasticsearch/search/SearchServiceTests.java index f303d837f56e6..5a5df0e514052 100644 --- a/server/src/test/java/org/elasticsearch/search/SearchServiceTests.java +++ b/server/src/test/java/org/elasticsearch/search/SearchServiceTests.java @@ -891,7 +891,7 @@ public void testCreateSearchContext() throws IOException { * While we have no NPE in DefaultContext constructor anymore, we still want to guard against it (or other failures) in the future to * avoid leaking searchers. */ - public void testCreateSearchContextFailure() throws IOException { + public void testCreateSearchContextFailure() throws Exception { final String index = randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT); final IndexService indexService = createIndex(index); final SearchService service = getInstanceFromNode(SearchService.class); @@ -908,7 +908,9 @@ public SearchType searchType() { () -> service.createContext(reader, request, null, randomBoolean())); assertEquals("expected", e.getMessage()); } - assertEquals("should have 2 store refs (IndexService + InternalEngine)", 2, indexService.getShard(0).store().refCount()); + // Needs to busily assert because Engine#refreshNeeded can increase the refCount. + assertBusy(() -> + assertEquals("should have 2 store refs (IndexService + InternalEngine)", 2, indexService.getShard(0).store().refCount())); } public void testMatchNoDocsEmptyResponse() throws InterruptedException {