Search in sources :

Example 11 with MockProducer

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

the class RecordCollectorTest method shouldThrowStreamsExceptionOnAnyExceptionButProducerFencedException.

@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowStreamsExceptionOnAnyExceptionButProducerFencedException() {
    final RecordCollector collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {

        @Override
        public synchronized Future<RecordMetadata> send(final ProducerRecord record, final Callback callback) {
            throw new KafkaException();
        }
    }, "test", logContext, new DefaultProductionExceptionHandler());
    collector.send("topic1", "3", "0", null, stringSerializer, stringSerializer, streamPartitioner);
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) Callback(org.apache.kafka.clients.producer.Callback) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) DefaultProductionExceptionHandler(org.apache.kafka.streams.errors.DefaultProductionExceptionHandler) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 12 with MockProducer

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

the class RecordCollectorTest method shouldNotThrowStreamsExceptionOnFlushIfASendFailedWithContinueExceptionHandler.

@SuppressWarnings("unchecked")
@Test
public void shouldNotThrowStreamsExceptionOnFlushIfASendFailedWithContinueExceptionHandler() {
    final RecordCollector collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {

        @Override
        public synchronized Future<RecordMetadata> send(final ProducerRecord record, final Callback callback) {
            callback.onCompletion(null, new Exception());
            return null;
        }
    }, "test", logContext, new AlwaysContinueProductionExceptionHandler());
    collector.send("topic1", "3", "0", null, stringSerializer, stringSerializer, streamPartitioner);
    collector.flush();
}
Also used : AlwaysContinueProductionExceptionHandler(org.apache.kafka.streams.errors.AlwaysContinueProductionExceptionHandler) MockProducer(org.apache.kafka.clients.producer.MockProducer) Callback(org.apache.kafka.clients.producer.Callback) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) KafkaException(org.apache.kafka.common.KafkaException) StreamsException(org.apache.kafka.streams.errors.StreamsException) Test(org.junit.Test)

Example 13 with MockProducer

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

the class RecordCollectorTest method shouldThrowIfTopicIsUnknownWithContinueExceptionHandler.

@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowIfTopicIsUnknownWithContinueExceptionHandler() {
    final RecordCollector collector = new RecordCollectorImpl(new MockProducer(cluster, true, new DefaultPartitioner(), byteArraySerializer, byteArraySerializer) {

        @Override
        public List<PartitionInfo> partitionsFor(final String topic) {
            return Collections.EMPTY_LIST;
        }
    }, "test", logContext, new AlwaysContinueProductionExceptionHandler());
    collector.send("topic1", "3", "0", null, stringSerializer, stringSerializer, streamPartitioner);
}
Also used : AlwaysContinueProductionExceptionHandler(org.apache.kafka.streams.errors.AlwaysContinueProductionExceptionHandler) MockProducer(org.apache.kafka.clients.producer.MockProducer) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) List(java.util.List) Test(org.junit.Test)

Example 14 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project storm by apache.

the class KafkaBoltTest method testCustomCallbackIsWrappedByDefaultCallbackBehavior.

@Test
public void testCustomCallbackIsWrappedByDefaultCallbackBehavior() {
    MockProducer<String, String> producer = new MockProducer<>(Cluster.empty(), false, null, null, null);
    KafkaBolt<String, String> bolt = makeBolt(producer);
    PreparableCallback customCallback = mock(PreparableCallback.class);
    bolt.withProducerCallback(customCallback);
    OutputCollector collector = mock(OutputCollector.class);
    TopologyContext context = mock(TopologyContext.class);
    Map<String, Object> topoConfig = new HashMap<>();
    bolt.prepare(topoConfig, context, collector);
    verify(customCallback).prepare(topoConfig, context);
    String key = "KEY";
    String value = "VALUE";
    Tuple testTuple = createTestTuple(key, value);
    bolt.execute(testTuple);
    assertThat(producer.history().size(), is(1));
    ProducerRecord<String, String> arg = producer.history().get(0);
    LOG.info("GOT {} ->", arg);
    LOG.info("{}, {}, {}", arg.topic(), arg.key(), arg.value());
    assertThat(arg.topic(), is("MY_TOPIC"));
    assertThat(arg.key(), is(key));
    assertThat(arg.value(), is(value));
    // Force a send error
    KafkaException ex = new KafkaException();
    producer.errorNext(ex);
    verify(customCallback).onCompletion(any(), eq(ex));
    verify(collector).reportError(ex);
    verify(collector).fail(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) KafkaException(org.apache.kafka.common.KafkaException) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 15 with MockProducer

use of org.apache.kafka.clients.producer.MockProducer in project storm by apache.

the class KafkaBoltTest method testSimpleWithError.

@Test
public void testSimpleWithError() {
    MockProducer<String, String> producer = new MockProducer<>(Cluster.empty(), false, null, null, null);
    KafkaBolt<String, String> bolt = makeBolt(producer);
    OutputCollector collector = mock(OutputCollector.class);
    TopologyContext context = mock(TopologyContext.class);
    Map<String, Object> conf = new HashMap<>();
    bolt.prepare(conf, context, collector);
    String key = "KEY";
    String value = "VALUE";
    Tuple testTuple = createTestTuple(key, value);
    bolt.execute(testTuple);
    assertThat(producer.history().size(), is(1));
    ProducerRecord<String, String> arg = producer.history().get(0);
    LOG.info("GOT {} ->", arg);
    LOG.info("{}, {}, {}", arg.topic(), arg.key(), arg.value());
    assertThat(arg.topic(), is("MY_TOPIC"));
    assertThat(arg.key(), is(key));
    assertThat(arg.value(), is(value));
    // Force a send error
    KafkaException ex = new KafkaException();
    producer.errorNext(ex);
    verify(collector).reportError(ex);
    verify(collector).fail(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) KafkaException(org.apache.kafka.common.KafkaException) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Aggregations

MockProducer (org.apache.kafka.clients.producer.MockProducer)32 Test (org.junit.Test)25 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)17 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)14 StreamsException (org.apache.kafka.streams.errors.StreamsException)14 Callback (org.apache.kafka.clients.producer.Callback)13 Future (java.util.concurrent.Future)12 KafkaException (org.apache.kafka.common.KafkaException)10 Map (java.util.Map)8 HashMap (java.util.HashMap)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 Vertx (io.vertx.core.Vertx)5 Buffer (io.vertx.core.buffer.Buffer)5 VertxExtension (io.vertx.junit5.VertxExtension)5 VertxTestContext (io.vertx.junit5.VertxTestContext)5 TimeoutException (org.apache.kafka.common.errors.TimeoutException)5 KafkaClientUnitTestHelper (org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Test (org.junit.jupiter.api.Test)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5