Search in sources :

Example 1 with ProducerInterceptors

use of org.apache.kafka.clients.producer.internals.ProducerInterceptors in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method testInterceptorPartitionSetOnTooLargeRecord.

@PrepareOnlyThisForTest(Metadata.class)
@Test
public void testInterceptorPartitionSetOnTooLargeRecord() throws Exception {
    Properties props = new Properties();
    props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    props.setProperty(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, "1");
    String topic = "topic";
    ProducerRecord<String, String> record = new ProducerRecord<>(topic, "value");
    KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer());
    Metadata metadata = PowerMock.createNiceMock(Metadata.class);
    MemberModifier.field(KafkaProducer.class, "metadata").set(producer, metadata);
    final Cluster cluster = new Cluster("dummy", Collections.singletonList(new Node(0, "host1", 1000)), Arrays.asList(new PartitionInfo(topic, 0, null, null, null)), Collections.<String>emptySet(), Collections.<String>emptySet());
    EasyMock.expect(metadata.fetch()).andReturn(cluster).once();
    // Mock interceptors field
    ProducerInterceptors interceptors = PowerMock.createMock(ProducerInterceptors.class);
    EasyMock.expect(interceptors.onSend(record)).andReturn(record);
    interceptors.onSendError(EasyMock.eq(record), EasyMock.<TopicPartition>notNull(), EasyMock.<Exception>notNull());
    EasyMock.expectLastCall();
    MemberModifier.field(KafkaProducer.class, "interceptors").set(producer, interceptors);
    PowerMock.replay(metadata);
    EasyMock.replay(interceptors);
    producer.send(record);
    EasyMock.verify(interceptors);
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) ProducerInterceptors(org.apache.kafka.clients.producer.internals.ProducerInterceptors) PartitionInfo(org.apache.kafka.common.PartitionInfo) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Aggregations

Properties (java.util.Properties)1 Metadata (org.apache.kafka.clients.Metadata)1 ProducerInterceptors (org.apache.kafka.clients.producer.internals.ProducerInterceptors)1 Cluster (org.apache.kafka.common.Cluster)1 Node (org.apache.kafka.common.Node)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 Test (org.junit.Test)1 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)1