use of org.apache.kafka.common.record.RecordBatch.MAGIC_VALUE_V2 in project kafka by apache.
the class MemoryRecordsBuilderTest method testWriteLeaderChangeControlBatch.
@ParameterizedTest
@ArgumentsSource(MemoryRecordsBuilderArgumentsProvider.class)
public void testWriteLeaderChangeControlBatch(Args args) {
ByteBuffer buffer = allocateBuffer(128, args);
final int leaderId = 1;
final int leaderEpoch = 5;
final List<Integer> voters = Arrays.asList(2, 3);
Supplier<MemoryRecordsBuilder> supplier = () -> new MemoryRecordsBuilder(buffer, args.magic, args.compressionType, TimestampType.CREATE_TIME, 0L, 0L, RecordBatch.NO_PRODUCER_ID, RecordBatch.NO_PRODUCER_EPOCH, RecordBatch.NO_SEQUENCE, false, true, leaderEpoch, buffer.capacity());
if (args.magic < MAGIC_VALUE_V2) {
assertThrows(IllegalArgumentException.class, supplier::get);
} else {
MemoryRecordsBuilder builder = supplier.get();
builder.appendLeaderChangeMessage(RecordBatch.NO_TIMESTAMP, new LeaderChangeMessage().setLeaderId(leaderId).setVoters(voters.stream().map(voterId -> new Voter().setVoterId(voterId)).collect(Collectors.toList())));
MemoryRecords built = builder.build();
List<Record> records = TestUtils.toList(built.records());
assertEquals(1, records.size());
LeaderChangeMessage leaderChangeMessage = ControlRecordUtils.deserializeLeaderChangeMessage(records.get(0));
assertEquals(leaderId, leaderChangeMessage.leaderId());
assertEquals(voters, leaderChangeMessage.voters().stream().map(Voter::voterId).collect(Collectors.toList()));
}
}
Aggregations