You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "Gonzalo Muñoz Fernández (Jira)" <ji...@apache.org> on 2020/09/19 13:14:00 UTC
[jira] [Created] (KAFKA-10503) MockProducer doesn't throw
ClassCastException when no partition for topic
Gonzalo Muñoz Fernández created KAFKA-10503:
-----------------------------------------------
Summary: MockProducer doesn't throw ClassCastException when no partition for topic
Key: KAFKA-10503
URL: https://issues.apache.org/jira/browse/KAFKA-10503
Project: Kafka
Issue Type: Improvement
Components: clients, producer
Affects Versions: 2.6.0
Reporter: Gonzalo Muñoz Fernández
Though {{MockProducer}} admits serializers in its constructors, it doesn't check during {{send}} method that those serializers are the proper ones to serialize key/value included into the {{ProducerRecord}}.
[This check|https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/producer/MockProducer.java#L499-L500] is only done if there is a partition assigned for that topic.
It would be an enhancement if these serialize methods were also invoked in simple scenarios, where no partition is assigned to a topic.
eg:
{code:java}
@Test
public void shouldThrowClassCastException() {
MockProducer<Integer, String> producer = new MockProducer<>(true, new IntegerSerializer(), new StringSerializer());
ProducerRecord record = new ProducerRecord(TOPIC, "key1", "value1");
try {
producer.send(record);
fail("Should have thrown ClassCastException because record cannot be casted with serializers");
} catch (ClassCastException e) {}
}
{code}
Currently, for obtaining the ClassCastException is needed to define the topic into a partition:
{code:java}
PartitionInfo partitionInfo = new PartitionInfo(TOPIC, 0, null, null, null);
Cluster cluster = new Cluster(null, emptyList(), asList(partitionInfo),
emptySet(), emptySet());
producer = new MockProducer(cluster,
true,
new DefaultPartitioner(),
new IntegerSerializer(),
new StringSerializer());
{code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)