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