Search in sources :

Example 1 with ByteArraySerializer

use of org.apache.kafka.common.serialization.ByteArraySerializer in project kafka by apache.

the class KafkaProducerTest method testOsDefaultSocketBufferSizes.

@Test
public void testOsDefaultSocketBufferSizes() throws Exception {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    config.put(ProducerConfig.SEND_BUFFER_CONFIG, Selectable.USE_DEFAULT_BUFFER_SIZE);
    config.put(ProducerConfig.RECEIVE_BUFFER_CONFIG, Selectable.USE_DEFAULT_BUFFER_SIZE);
    KafkaProducer<byte[], byte[]> producer = new KafkaProducer<>(config, new ByteArraySerializer(), new ByteArraySerializer());
    producer.close();
}
Also used : HashMap(java.util.HashMap) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 2 with ByteArraySerializer

use of org.apache.kafka.common.serialization.ByteArraySerializer in project kafka by apache.

the class KafkaProducerTest method testInvalidSocketSendBufferSize.

@Test(expected = KafkaException.class)
public void testInvalidSocketSendBufferSize() throws Exception {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    config.put(ProducerConfig.SEND_BUFFER_CONFIG, -2);
    new KafkaProducer<>(config, new ByteArraySerializer(), new ByteArraySerializer());
}
Also used : HashMap(java.util.HashMap) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 3 with ByteArraySerializer

use of org.apache.kafka.common.serialization.ByteArraySerializer in project kafka by apache.

the class KafkaProducerTest method testInvalidSocketReceiveBufferSize.

@Test(expected = KafkaException.class)
public void testInvalidSocketReceiveBufferSize() throws Exception {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    config.put(ProducerConfig.RECEIVE_BUFFER_CONFIG, -2);
    new KafkaProducer<>(config, new ByteArraySerializer(), new ByteArraySerializer());
}
Also used : HashMap(java.util.HashMap) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 4 with ByteArraySerializer

use of org.apache.kafka.common.serialization.ByteArraySerializer in project kafka by apache.

the class KafkaProducerTest method testConstructorFailureCloseResource.

@Test
public void testConstructorFailureCloseResource() {
    Properties props = new Properties();
    props.setProperty(ProducerConfig.CLIENT_ID_CONFIG, "testConstructorClose");
    props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "some.invalid.hostname.foo.bar.local:9999");
    props.setProperty(ProducerConfig.METRIC_REPORTER_CLASSES_CONFIG, MockMetricsReporter.class.getName());
    final int oldInitCount = MockMetricsReporter.INIT_COUNT.get();
    final int oldCloseCount = MockMetricsReporter.CLOSE_COUNT.get();
    try {
        KafkaProducer<byte[], byte[]> producer = new KafkaProducer<byte[], byte[]>(props, new ByteArraySerializer(), new ByteArraySerializer());
    } catch (KafkaException e) {
        Assert.assertEquals(oldInitCount + 1, MockMetricsReporter.INIT_COUNT.get());
        Assert.assertEquals(oldCloseCount + 1, MockMetricsReporter.CLOSE_COUNT.get());
        Assert.assertEquals("Failed to construct kafka producer", e.getMessage());
        return;
    }
    fail("should have caught an exception and returned");
}
Also used : MockMetricsReporter(org.apache.kafka.test.MockMetricsReporter) KafkaException(org.apache.kafka.common.KafkaException) Properties(java.util.Properties) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Example 5 with ByteArraySerializer

use of org.apache.kafka.common.serialization.ByteArraySerializer in project kafka by apache.

the class KafkaProducerTest method testConstructorWithSerializers.

@Test
public void testConstructorWithSerializers() {
    Properties producerProps = new Properties();
    producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    new KafkaProducer<>(producerProps, new ByteArraySerializer(), new ByteArraySerializer()).close();
}
Also used : Properties(java.util.Properties) ByteArraySerializer(org.apache.kafka.common.serialization.ByteArraySerializer) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Aggregations

ByteArraySerializer (org.apache.kafka.common.serialization.ByteArraySerializer)7 Test (org.junit.Test)5 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)5 Properties (java.util.Properties)4 HashMap (java.util.HashMap)3 File (java.io.File)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 KafkaException (org.apache.kafka.common.KafkaException)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 MockMetricsReporter (org.apache.kafka.test.MockMetricsReporter)1