Search in sources :

Example 1 with MockPartitioner

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

the class KafkaProducerTest method testPartitionerClose.

@Test
public void testPartitionerClose() {
    try {
        Properties props = new Properties();
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
        MockPartitioner.resetCounters();
        props.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG, MockPartitioner.class.getName());
        KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer());
        assertEquals(1, MockPartitioner.INIT_COUNT.get());
        assertEquals(0, MockPartitioner.CLOSE_COUNT.get());
        producer.close();
        assertEquals(1, MockPartitioner.INIT_COUNT.get());
        assertEquals(1, MockPartitioner.CLOSE_COUNT.get());
    } finally {
        // cleanup since we are using mutable static variables in MockPartitioner
        MockPartitioner.resetCounters();
    }
}
Also used : MockPartitioner(org.apache.kafka.test.MockPartitioner) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with MockPartitioner

use of org.apache.kafka.test.MockPartitioner in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method testPartitionerClose.

@Test
public void testPartitionerClose() throws Exception {
    try {
        Properties props = new Properties();
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
        MockPartitioner.resetCounters();
        props.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG, MockPartitioner.class.getName());
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props, new StringSerializer(), new StringSerializer());
        assertEquals(1, MockPartitioner.INIT_COUNT.get());
        assertEquals(0, MockPartitioner.CLOSE_COUNT.get());
        producer.close();
        assertEquals(1, MockPartitioner.INIT_COUNT.get());
        assertEquals(1, MockPartitioner.CLOSE_COUNT.get());
    } finally {
        // cleanup since we are using mutable static variables in MockPartitioner
        MockPartitioner.resetCounters();
    }
}
Also used : MockPartitioner(org.apache.kafka.test.MockPartitioner) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Aggregations

Properties (java.util.Properties)2 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)2 MockPartitioner (org.apache.kafka.test.MockPartitioner)2 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)1