diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java index e3e9fc569aee6..6906f61607ba0 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java @@ -1045,19 +1045,24 @@ public void testScrollWithOngoingSegmentReplication() throws Exception { // wait for segrep to start and copy temporary files waitForFileCopy.await(); - // verify replica contains temporary files - IndexShard replicaShard = getIndexShard(replica, INDEX_NAME); - List temporaryFiles = Arrays.stream(replicaShard.store().directory().listAll()) - .filter(fileName -> fileName.startsWith(REPLICATION_PREFIX)) - .collect(Collectors.toList()); - logger.info("--> temporaryFiles {}", temporaryFiles); - assertTrue(temporaryFiles.size() > 0); + final IndexShard replicaShard = getIndexShard(replica, INDEX_NAME); + // Wait until replica has written a tmp file to disk. + List temporaryFiles = new ArrayList<>(); + assertBusy(() -> { + // verify replica contains temporary files + temporaryFiles.addAll( + Arrays.stream(replicaShard.store().directory().listAll()) + .filter(fileName -> fileName.startsWith(REPLICATION_PREFIX)) + .collect(Collectors.toList()) + ); + logger.info("--> temporaryFiles {}", temporaryFiles); + assertTrue(temporaryFiles.size() > 0); + }); // Clear scroll query, this should clean up files on replica client(replica).prepareClearScroll().addScrollId(searchResponse.getScrollId()).get(); // verify temporary files still exist - replicaShard = getIndexShard(replica, INDEX_NAME); List temporaryFilesPostClear = Arrays.stream(replicaShard.store().directory().listAll()) .filter(fileName -> fileName.startsWith(REPLICATION_PREFIX)) .collect(Collectors.toList()); @@ -1066,7 +1071,6 @@ public void testScrollWithOngoingSegmentReplication() throws Exception { // Unblock segment replication blockFileCopy.countDown(); - assertEquals(temporaryFiles.size(), temporaryFilesPostClear.size()); assertTrue(temporaryFilesPostClear.containsAll(temporaryFiles)); // wait for replica to catch up and verify doc count