Search in sources :

Example 1 with MockProducerInterceptor

use of org.apache.kafka.test.MockProducerInterceptor in project kafka by apache.

the class KafkaProducerTest method testInterceptorConstructClose.

@Test
public void testInterceptorConstructClose() throws Exception {
    try {
        Properties props = new Properties();
        // test with client ID assigned by KafkaProducer
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
        props.setProperty(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, MockProducerInterceptor.class.getName());
        props.setProperty(MockProducerInterceptor.APPEND_STRING_PROP, "something");
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props, new StringSerializer(), new StringSerializer());
        Assert.assertEquals(1, MockProducerInterceptor.INIT_COUNT.get());
        Assert.assertEquals(0, MockProducerInterceptor.CLOSE_COUNT.get());
        // Cluster metadata will only be updated on calling onSend.
        Assert.assertNull(MockProducerInterceptor.CLUSTER_META.get());
        producer.close();
        Assert.assertEquals(1, MockProducerInterceptor.INIT_COUNT.get());
        Assert.assertEquals(1, MockProducerInterceptor.CLOSE_COUNT.get());
    } finally {
        // cleanup since we are using mutable static variables in MockProducerInterceptor
        MockProducerInterceptor.resetCounters();
    }
}
Also used : MockProducerInterceptor(org.apache.kafka.test.MockProducerInterceptor) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Aggregations

Properties (java.util.Properties)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 MockProducerInterceptor (org.apache.kafka.test.MockProducerInterceptor)1 Test (org.junit.Test)1 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)1