diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java index 7f5bc93fc75..67cc05de8b7 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java @@ -20,6 +20,9 @@ import org.elasticsearch.test.ESSingleNodeTestCase; import org.mockito.Mockito; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.LongStream; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -46,12 +49,14 @@ public class ShardChangesActionTests extends ESSingleNodeTestCase { int min = randomIntBetween(0, numWrites - 1); int max = randomIntBetween(min, numWrites - 1); - int index = 0; - List operations = ShardChangesAction.getOperationsBetween(indexShard, min, max); - for (long expectedSeqNo = min; expectedSeqNo < max; expectedSeqNo++) { - Translog.Operation operation = operations.get(index++); - assertThat(operation.seqNo(), equalTo(expectedSeqNo)); - } + final List operations = ShardChangesAction.getOperationsBetween(indexShard, min, max); + /* + * We are not guaranteed that operations are returned to us in order they are in the translog (if our read crosses multiple + * generations) so the best we can assert is that we see the expected operations. + */ + final Set seenSeqNos = operations.stream().map(Translog.Operation::seqNo).collect(Collectors.toSet()); + final Set expectedSeqNos = LongStream.range(min, max + 1).boxed().collect(Collectors.toSet()); + assertThat(seenSeqNos, equalTo(expectedSeqNos)); } // get operations for a range no operations exists: