You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "Dennis Hunziker (Jira)" <ji...@apache.org> on 2021/12/07 14:18:00 UTC

[jira] [Commented] (KAFKA-10503) MockProducer doesn't throw ClassCastException when no partition for topic

    [ https://issues.apache.org/jira/browse/KAFKA-10503?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17454673#comment-17454673 ] 

Dennis Hunziker commented on KAFKA-10503:
-----------------------------------------

This change did break our tests. When using the no-arg constructor the serializers are null but now that they're referenced in any case we'll just get an NPE. Was that on purpose? Shouldn't the change made here only be applied if the serializers aren't null?

Happy to create a separate ticket and fix if you agree.

> 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
>            Assignee: Gonzalo Muñoz Fernández
>            Priority: Minor
>              Labels: mock, producer
>             Fix For: 2.7.0
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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.20.1#820001)