Search in sources :

Example 1 with MockSerializer

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

the class MockProducerTest method testManualCompletion.

@Test
public void testManualCompletion() throws Exception {
    MockProducer<byte[], byte[]> producer = new MockProducer<>(false, new MockSerializer(), new MockSerializer());
    ProducerRecord<byte[], byte[]> record1 = new ProducerRecord<>(topic, "key1".getBytes(), "value1".getBytes());
    ProducerRecord<byte[], byte[]> record2 = new ProducerRecord<>(topic, "key2".getBytes(), "value2".getBytes());
    Future<RecordMetadata> md1 = producer.send(record1);
    assertFalse("Send shouldn't have completed", md1.isDone());
    Future<RecordMetadata> md2 = producer.send(record2);
    assertFalse("Send shouldn't have completed", md2.isDone());
    assertTrue("Complete the first request", producer.completeNext());
    assertFalse("Requst should be successful", isError(md1));
    assertFalse("Second request still incomplete", md2.isDone());
    IllegalArgumentException e = new IllegalArgumentException("blah");
    assertTrue("Complete the second request with an error", producer.errorNext(e));
    try {
        md2.get();
        fail("Expected error to be thrown");
    } catch (ExecutionException err) {
        assertEquals(e, err.getCause());
    }
    assertFalse("No more requests to complete", producer.completeNext());
    Future<RecordMetadata> md3 = producer.send(record1);
    Future<RecordMetadata> md4 = producer.send(record2);
    assertTrue("Requests should not be completed.", !md3.isDone() && !md4.isDone());
    producer.flush();
    assertTrue("Requests should be completed.", md3.isDone() && md4.isDone());
}
Also used : MockSerializer(org.apache.kafka.test.MockSerializer) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with MockSerializer

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

the class MockProducerTest method testAutoCompleteMock.

@Test
@SuppressWarnings("unchecked")
public void testAutoCompleteMock() throws Exception {
    MockProducer<byte[], byte[]> producer = new MockProducer<>(true, new MockSerializer(), new MockSerializer());
    ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(topic, "key".getBytes(), "value".getBytes());
    Future<RecordMetadata> metadata = producer.send(record);
    assertTrue("Send should be immediately complete", metadata.isDone());
    assertFalse("Send should be successful", isError(metadata));
    assertEquals("Offset should be 0", 0L, metadata.get().offset());
    assertEquals(topic, metadata.get().topic());
    assertEquals("We should have the record in our history", singletonList(record), producer.history());
    producer.clear();
    assertEquals("Clear should erase our history", 0, producer.history().size());
}
Also used : MockSerializer(org.apache.kafka.test.MockSerializer) Test(org.junit.Test)

Example 3 with MockSerializer

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

the class KafkaProducerTest method testSerializerClose.

@Test
public void testSerializerClose() throws Exception {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.CLIENT_ID_CONFIG, "testConstructorClose");
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    configs.put(ProducerConfig.METRIC_REPORTER_CLASSES_CONFIG, MockMetricsReporter.class.getName());
    configs.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, CommonClientConfigs.DEFAULT_SECURITY_PROTOCOL);
    final int oldInitCount = MockSerializer.INIT_COUNT.get();
    final int oldCloseCount = MockSerializer.CLOSE_COUNT.get();
    KafkaProducer<byte[], byte[]> producer = new KafkaProducer<byte[], byte[]>(configs, new MockSerializer(), new MockSerializer());
    Assert.assertEquals(oldInitCount + 2, MockSerializer.INIT_COUNT.get());
    Assert.assertEquals(oldCloseCount, MockSerializer.CLOSE_COUNT.get());
    producer.close();
    Assert.assertEquals(oldInitCount + 2, MockSerializer.INIT_COUNT.get());
    Assert.assertEquals(oldCloseCount + 2, MockSerializer.CLOSE_COUNT.get());
}
Also used : MockSerializer(org.apache.kafka.test.MockSerializer) MockMetricsReporter(org.apache.kafka.test.MockMetricsReporter) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)

Aggregations

MockSerializer (org.apache.kafka.test.MockSerializer)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 MockMetricsReporter (org.apache.kafka.test.MockMetricsReporter)1 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)1