Search in sources :

Example 31 with MockProducer

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

the class KafkaBoltTest method testSimple.

@Test
public void testSimple() {
    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));
    // Complete the send
    producer.completeNext();
    verify(collector).ack(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 32 with MockProducer

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

the class MockClientSupplier method getProducer.

@Override
public Producer<byte[], byte[]> getProducer(final Map<String, Object> config) {
    if (applicationId != null) {
        assertThat((String) config.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG), startsWith(applicationId + "-"));
    } else {
        assertFalse(config.containsKey(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
    }
    final MockProducer<byte[], byte[]> producer = new MockProducer<>(cluster, true, new DefaultPartitioner(), BYTE_ARRAY_SERIALIZER, BYTE_ARRAY_SERIALIZER);
    producers.add(producer);
    return producer;
}
Also used : MockProducer(org.apache.kafka.clients.producer.MockProducer) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner)

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