|
22 | 22 | import org.apache.kafka.clients.consumer.ShareConsumer;
|
23 | 23 | import org.apache.kafka.common.MessageFormatter;
|
24 | 24 | import org.apache.kafka.common.errors.TimeoutException;
|
| 25 | +import org.apache.kafka.common.header.internals.RecordHeaders; |
| 26 | +import org.apache.kafka.common.record.RecordBatch; |
| 27 | +import org.apache.kafka.common.record.TimestampType; |
25 | 28 | import org.apache.kafka.common.utils.Time;
|
26 | 29 | import org.apache.kafka.server.util.MockTime;
|
27 | 30 |
|
28 | 31 | import org.junit.jupiter.api.BeforeEach;
|
29 | 32 | import org.junit.jupiter.api.Test;
|
| 33 | +import org.mockito.ArgumentCaptor; |
30 | 34 |
|
31 | 35 | import java.io.PrintStream;
|
32 | 36 | import java.time.Duration;
|
| 37 | +import java.util.Optional; |
33 | 38 |
|
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
34 | 40 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
35 | 41 | import static org.mockito.ArgumentMatchers.any;
|
36 | 42 | import static org.mockito.ArgumentMatchers.eq;
|
@@ -131,4 +137,42 @@ public void testRejectMessageOnError() {
|
131 | 137 |
|
132 | 138 | consumer.cleanup();
|
133 | 139 | }
|
| 140 | + |
| 141 | + @Test |
| 142 | + public void shouldUpgradeDeliveryCount() { |
| 143 | + // Mock dependencies |
| 144 | + ConsoleShareConsumer.ConsumerWrapper consumer = mock(ConsoleShareConsumer.ConsumerWrapper.class); |
| 145 | + MessageFormatter formatter = mock(MessageFormatter.class); |
| 146 | + PrintStream printStream = mock(PrintStream.class); |
| 147 | + |
| 148 | + short deliveryCount = 1; |
| 149 | + // Mock a ConsumerRecord with a delivery count |
| 150 | + ConsumerRecord<byte[], byte[]> record = new ConsumerRecord<>( |
| 151 | + "test-topic", 0, 0, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, 0, |
| 152 | + 0, new byte[0], new byte[0], new RecordHeaders(), Optional.empty(), Optional.of(deliveryCount) |
| 153 | + ); |
| 154 | + |
| 155 | + // Mock consumer behavior |
| 156 | + when(consumer.receive()).thenReturn(record); |
| 157 | + |
| 158 | + // Process the record |
| 159 | + ConsoleShareConsumer.process(1, formatter, consumer, printStream, false, AcknowledgeType.ACCEPT); |
| 160 | + |
| 161 | + // Capture the actual ConsumerRecord passed to formatter.writeTo |
| 162 | + ArgumentCaptor<ConsumerRecord> captor = ArgumentCaptor.forClass(ConsumerRecord.class); |
| 163 | + verify(formatter).writeTo(captor.capture(), eq(printStream)); |
| 164 | + |
| 165 | + // Assert that the captured ConsumerRecord matches the expected values |
| 166 | + ConsumerRecord<byte[], byte[]> capturedRecord = captor.getValue(); |
| 167 | + assertEquals("test-topic", capturedRecord.topic()); |
| 168 | + assertEquals(0, capturedRecord.partition()); |
| 169 | + assertEquals(0, capturedRecord.offset()); |
| 170 | + assertEquals(deliveryCount, capturedRecord.deliveryCount().orElse((short) 0)); |
| 171 | + |
| 172 | + // Verify that the consumer acknowledges the record |
| 173 | + verify(consumer).acknowledge(record, AcknowledgeType.ACCEPT); |
| 174 | + |
| 175 | + // Cleanup |
| 176 | + consumer.cleanup(); |
| 177 | + } |
134 | 178 | }
|
0 commit comments