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();
}
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());
}
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());
}
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);
}
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();
}
Aggregations