Search in sources :

Example 26 with ProducerRecord

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

the class WorkerSourceTaskTest method testSendRecordsNoTimestamp.

@Test
public void testSendRecordsNoTimestamp() throws Exception {
    final Long timestamp = -1L;
    createWorkerTask();
    List<SourceRecord> records = Collections.singletonList(new SourceRecord(PARTITION, OFFSET, "topic", null, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD, timestamp));
    Capture<ProducerRecord<byte[], byte[]>> sent = expectSendRecordAnyTimes();
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", records);
    Whitebox.invokeMethod(workerTask, "sendRecords");
    assertEquals(null, sent.getValue().timestamp());
    PowerMock.verifyAll();
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) SourceRecord(org.apache.kafka.connect.source.SourceRecord) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) Test(org.junit.Test)

Example 27 with ProducerRecord

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

the class RocksDBWindowStoreSupplierTest method shouldCreateLoggingEnabledStoreWhenWindowStoreLogged.

@Test
public void shouldCreateLoggingEnabledStoreWhenWindowStoreLogged() throws Exception {
    store = createStore(true, false);
    final List<ProducerRecord> logged = new ArrayList<>();
    final NoOpRecordCollector collector = new NoOpRecordCollector() {

        @Override
        public <K, V> void send(final String topic, K key, V value, Integer partition, Long timestamp, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
            logged.add(new ProducerRecord<K, V>(topic, partition, timestamp, key, value));
        }
    };
    final MockProcessorContext context = new MockProcessorContext(TestUtils.tempDirectory(), Serdes.String(), Serdes.String(), collector, cache);
    context.setTime(1);
    store.init(context, store);
    store.put("a", "b");
    assertFalse(logged.isEmpty());
}
Also used : NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 28 with ProducerRecord

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

the class RocksDBKeyValueStoreSupplierTest method shouldNotBeLoggingEnabledStoreWhenLoggingNotEnabled.

@Test
public void shouldNotBeLoggingEnabledStoreWhenLoggingNotEnabled() throws Exception {
    store = createStore(false, false);
    final List<ProducerRecord> logged = new ArrayList<>();
    final NoOpRecordCollector collector = new NoOpRecordCollector() {

        @Override
        public <K, V> void send(final String topic, K key, V value, Integer partition, Long timestamp, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
            logged.add(new ProducerRecord<K, V>(topic, partition, timestamp, key, value));
        }
    };
    final MockProcessorContext context = new MockProcessorContext(TestUtils.tempDirectory(), Serdes.String(), Serdes.String(), collector, cache);
    context.setTime(1);
    store.init(context, store);
    store.put("a", "b");
    assertTrue(logged.isEmpty());
}
Also used : NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 29 with ProducerRecord

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

the class KafkaBoltTest method testSimple.

@SuppressWarnings({ "unchecked", "serial" })
@Test
public void testSimple() {
    final KafkaProducer<String, String> producer = mock(KafkaProducer.class);
    when(producer.send(any(), any())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Callback c = (Callback) invocation.getArguments()[1];
            c.onCompletion(null, null);
            return null;
        }
    });
    KafkaBolt<String, String> bolt = new KafkaBolt<String, String>() {

        @Override
        protected KafkaProducer<String, String> mkProducer(Properties props) {
            return producer;
        }
    };
    bolt.withTopicSelector("MY_TOPIC");
    OutputCollector collector = mock(OutputCollector.class);
    TopologyContext context = mock(TopologyContext.class);
    Map<String, Object> conf = new HashMap<>();
    bolt.prepare(conf, context, collector);
    MkTupleParam param = new MkTupleParam();
    param.setFields("key", "message");
    Tuple testTuple = Testing.testTuple(Arrays.asList("KEY", "VALUE"), param);
    bolt.execute(testTuple);
    verify(producer).send(argThat(new ArgumentMatcher<ProducerRecord<String, String>>() {

        @Override
        public boolean matches(Object argument) {
            LOG.info("GOT {} ->", argument);
            ProducerRecord<String, String> arg = (ProducerRecord<String, String>) argument;
            LOG.info("  {} {} {}", arg.topic(), arg.key(), arg.value());
            return "MY_TOPIC".equals(arg.topic()) && "KEY".equals(arg.key()) && "VALUE".equals(arg.value());
        }
    }), any(Callback.class));
    verify(collector).ack(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) HashMap(java.util.HashMap) Properties(java.util.Properties) Callback(org.apache.kafka.clients.producer.Callback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatcher(org.mockito.ArgumentMatcher) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MkTupleParam(org.apache.storm.testing.MkTupleParam) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 30 with ProducerRecord

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

the class KafkaBasedLogTest method testProducerError.

@Test
public void testProducerError() throws Exception {
    expectStart();
    TestFuture<RecordMetadata> tp0Future = new TestFuture<>();
    ProducerRecord<String, String> tp0Record = new ProducerRecord<>(TOPIC, TP0_KEY, TP0_VALUE);
    Capture<org.apache.kafka.clients.producer.Callback> callback0 = EasyMock.newCapture();
    EasyMock.expect(producer.send(EasyMock.eq(tp0Record), EasyMock.capture(callback0))).andReturn(tp0Future);
    expectStop();
    PowerMock.replayAll();
    Map<TopicPartition, Long> endOffsets = new HashMap<>();
    endOffsets.put(TP0, 0L);
    endOffsets.put(TP1, 0L);
    consumer.updateEndOffsets(endOffsets);
    store.start();
    assertEquals(CONSUMER_ASSIGNMENT, consumer.assignment());
    assertEquals(0L, consumer.position(TP0));
    assertEquals(0L, consumer.position(TP1));
    final AtomicReference<Throwable> setException = new AtomicReference<>();
    store.send(TP0_KEY, TP0_VALUE, new org.apache.kafka.clients.producer.Callback() {

        @Override
        public void onCompletion(RecordMetadata metadata, Exception exception) {
            // Should only be invoked once
            assertNull(setException.get());
            setException.set(exception);
        }
    });
    KafkaException exc = new LeaderNotAvailableException("Error");
    tp0Future.resolve(exc);
    callback0.getValue().onCompletion(null, exc);
    assertNotNull(setException.get());
    store.stop();
    assertFalse(Whitebox.<Thread>getInternalState(store, "thread").isAlive());
    assertTrue(consumer.closed());
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) KafkaException(org.apache.kafka.common.KafkaException) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) WakeupException(org.apache.kafka.common.errors.WakeupException) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaException(org.apache.kafka.common.KafkaException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)51 Test (org.junit.Test)29 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)14 Future (java.util.concurrent.Future)11 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)10 Callback (org.apache.kafka.clients.producer.Callback)10 TopicPartition (org.apache.kafka.common.TopicPartition)8 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)7 ExecutionException (java.util.concurrent.ExecutionException)6 TimeoutException (org.apache.kafka.common.errors.TimeoutException)6 Serializer (org.apache.kafka.common.serialization.Serializer)6 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)6 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)6 MockProducer (org.apache.kafka.clients.producer.MockProducer)5 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)5 SourceRecord (org.apache.kafka.connect.source.SourceRecord)5 HashMap (java.util.HashMap)4 Endpoint (org.apache.camel.Endpoint)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4