From cebb7c677896b602deaff4cc655227efbf67f9dc Mon Sep 17 00:00:00 2001 From: Akhilesh Dubey Date: Thu, 11 Feb 2021 17:56:36 +0000 Subject: [PATCH] KAFKA-12297 - Test case for callback --- .../clients/producer/MockProducerTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java b/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java index 08dc8a73ebc1..115b914f7615 100644 --- a/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java +++ b/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java @@ -24,6 +24,7 @@ import org.apache.kafka.common.PartitionInfo; import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.errors.ProducerFencedException; +import org.apache.kafka.common.record.RecordBatch; import org.apache.kafka.common.serialization.IntegerSerializer; import org.apache.kafka.common.serialization.StringSerializer; import org.apache.kafka.test.MockSerializer; @@ -42,6 +43,7 @@ import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -735,6 +737,27 @@ public void shouldNotBeFlushedAfterFlush() { assertTrue(producer.flushed()); } + @Test + public void testMetadataOnException() throws InterruptedException { + buildMockProducer(false); + Future md1 = producer.send(record2, (md, exception) -> { + assertNotNull(md); + assertEquals(md.offset(), -1L, "Invalid offset"); + assertEquals(md.timestamp(), RecordBatch.NO_TIMESTAMP, "Invalid timestamp"); + //assertEquals(md.checksum(), -1L, "Invalid checksum"); + assertEquals(md.serializedKeySize(), -1L, "Invalid Serialized Key size"); + assertEquals(md.serializedValueSize(), -1L, "Invalid Serialized value size"); + }); + IllegalArgumentException e = new IllegalArgumentException("dummy exception"); + assertTrue(producer.errorNext(e), "Complete the second request with an error"); + try { + md1.get(); + fail("Something went wrong, expected an error"); + } catch (ExecutionException err) { + assertEquals(e, err.getCause()); + } + } + private boolean isError(Future future) { try { future.get();