|
28 | 28 | import java.util.stream.Stream;
|
29 | 29 |
|
30 | 30 | import org.testcontainers.containers.localstack.LocalStackContainer;
|
| 31 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest; |
| 32 | +import software.amazon.awssdk.services.kinesis.model.DescribeStreamResponse; |
| 33 | +import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest; |
| 34 | +import software.amazon.awssdk.services.kinesis.model.GetRecordsResponse; |
| 35 | +import software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest; |
| 36 | +import software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse; |
| 37 | +import software.amazon.awssdk.services.kinesis.model.Record; |
| 38 | +import software.amazon.awssdk.services.kinesis.model.ShardIteratorType; |
31 | 39 |
|
32 | 40 | import static org.hamcrest.Matchers.is;
|
33 | 41 | import static org.awaitility.Awaitility.await;
|
@@ -98,7 +106,11 @@ public void testKinesisIngestion_RewindByOffset() throws InterruptedException {
|
98 | 106 | String sequenceNumber = produceData("3", "name3", "20");
|
99 | 107 | logger.info("Produced message with sequence number: {}", sequenceNumber);
|
100 | 108 | produceData("4", "name4", "21");
|
101 |
| - Thread.sleep(2000); |
| 109 | + |
| 110 | + await() |
| 111 | + .atMost(5, TimeUnit.SECONDS) |
| 112 | + .until(() -> isRewinded(sequenceNumber)); |
| 113 | + |
102 | 114 |
|
103 | 115 | // create an index with ingestion source from kinesis
|
104 | 116 | createIndex(
|
@@ -128,4 +140,32 @@ public void testKinesisIngestion_RewindByOffset() throws InterruptedException {
|
128 | 140 | assertThat(response.getHits().getTotalHits().value(), is(2L));
|
129 | 141 | });
|
130 | 142 | }
|
| 143 | + |
| 144 | + private boolean isRewinded(String sequenceNumber) { |
| 145 | + DescribeStreamResponse describeStreamResponse = |
| 146 | + kinesisClient.describeStream(DescribeStreamRequest.builder().streamName(streamName).build()); |
| 147 | + |
| 148 | + String shardId = describeStreamResponse.streamDescription().shards().get(0).shardId(); |
| 149 | + |
| 150 | + GetShardIteratorRequest iteratorRequest = GetShardIteratorRequest.builder() |
| 151 | + .streamName(streamName) |
| 152 | + .shardId(shardId) |
| 153 | + .shardIteratorType(ShardIteratorType.AT_SEQUENCE_NUMBER) |
| 154 | + .startingSequenceNumber(sequenceNumber) |
| 155 | + .build(); |
| 156 | + |
| 157 | + GetShardIteratorResponse iteratorResponse = kinesisClient.getShardIterator(iteratorRequest); |
| 158 | + String shardIterator = iteratorResponse.shardIterator(); |
| 159 | + |
| 160 | + // Use the iterator to read the record |
| 161 | + GetRecordsRequest recordsRequest = GetRecordsRequest.builder().shardIterator(shardIterator).limit(1) // Adjust as needed |
| 162 | + .build(); |
| 163 | + |
| 164 | + GetRecordsResponse recordsResponse = kinesisClient.getRecords(recordsRequest); |
| 165 | + List<Record> records = recordsResponse.records(); |
| 166 | + if (records.size() != 1) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + return records.get(0).partitionKey().equals("3"); |
| 170 | + } |
131 | 171 | }
|
0 commit comments