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 14717f3cca1cc..c756c496fc75b 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationIT.java @@ -1046,19 +1046,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()); @@ -1067,7 +1072,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