Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-10503

MockProducer doesn't throw ClassCastException when no partition for topic

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 2.6.0
    • 2.7.0
    • clients, producer

    Description

      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 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:

          @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) {}
          }
      

      Currently, for obtaining the ClassCastException is needed to define the topic into a partition:

      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());
      

      Attachments

        Issue Links

          Activity

            People

              gmunozfe Gonzalo Muñoz Fernández
              gmunozfe Gonzalo Muñoz Fernández
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 1h
                  1h
                  Remaining:
                  Remaining Estimate - 1h
                  1h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified