use of org.apache.kafka.clients.producer.MockProducer in project hono by eclipse.
the class CachingKafkaProducerFactoryTest method setUp.
@BeforeEach
void setUp() {
final VertxInternal vertxMock = mock(VertxInternal.class);
final ContextInternal context = VertxMockSupport.mockContextInternal(vertxMock);
final PromiseInternal<Void> promiseInternal = VertxMockSupport.promiseInternal();
when(promiseInternal.future()).thenReturn(Future.succeededFuture());
doAnswer(invocation -> {
return promiseInternal;
}).when(context).promise();
when(vertxMock.getOrCreateContext()).thenReturn(context);
doAnswer(invocation -> {
final Promise<Object> result = Promise.promise();
final Handler<Future<Object>> blockingCode = invocation.getArgument(0);
final Handler<AsyncResult<Object>> resultHandler = invocation.getArgument(1);
result.future().onComplete(resultHandler);
blockingCode.handle(result.future());
return null;
}).when(context).executeBlocking(VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler());
final BiFunction<String, Map<String, String>, KafkaProducer<String, Buffer>> instanceSupplier = (n, c) -> {
final MockProducer<String, Buffer> mockProducer = new MockProducer<>(true, new StringSerializer(), new BufferSerializer());
return KafkaProducer.create(vertxMock, mockProducer);
};
factory = CachingKafkaProducerFactory.testFactory(vertxMock, instanceSupplier);
configProperties.setProducerConfig(Map.of("bootstrap.servers", "localhost:9092"));
}
use of org.apache.kafka.clients.producer.MockProducer in project apache-kafka-on-k8s by banzaicloud.
the class StreamThreadTest method shouldCloseTaskAsZombieAndRemoveFromActiveTasksIfProducerWasFencedWhileProcessing.
@Test
public void shouldCloseTaskAsZombieAndRemoveFromActiveTasksIfProducerWasFencedWhileProcessing() throws Exception {
internalTopologyBuilder.addSource(null, "source", null, null, null, topic1);
internalTopologyBuilder.addSink("sink", "dummyTopic", null, null, null, "source");
final StreamThread thread = createStreamThread(clientId, new StreamsConfig(configProps(true)), true);
final MockConsumer<byte[], byte[]> consumer = clientSupplier.consumer;
consumer.updatePartitions(topic1, Collections.singletonList(new PartitionInfo(topic1, 1, null, null, null)));
thread.setState(StreamThread.State.RUNNING);
thread.rebalanceListener.onPartitionsRevoked(null);
final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
final List<TopicPartition> assignedPartitions = new ArrayList<>();
// assign single partition
assignedPartitions.add(t1p1);
activeTasks.put(task1, Collections.singleton(t1p1));
thread.taskManager().setAssignmentMetadata(activeTasks, Collections.<TaskId, Set<TopicPartition>>emptyMap());
final MockConsumer<byte[], byte[]> mockConsumer = (MockConsumer<byte[], byte[]>) thread.consumer;
mockConsumer.assign(assignedPartitions);
mockConsumer.updateBeginningOffsets(Collections.singletonMap(t1p1, 0L));
thread.rebalanceListener.onPartitionsAssigned(assignedPartitions);
thread.runOnce(-1);
assertThat(thread.tasks().size(), equalTo(1));
final MockProducer producer = clientSupplier.producers.get(0);
// change consumer subscription from "pattern" to "manual" to be able to call .addRecords()
consumer.updateBeginningOffsets(Collections.singletonMap(assignedPartitions.iterator().next(), 0L));
consumer.unsubscribe();
consumer.assign(new HashSet<>(assignedPartitions));
consumer.addRecord(new ConsumerRecord<>(topic1, 1, 0, new byte[0], new byte[0]));
mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1);
thread.runOnce(-1);
assertThat(producer.history().size(), equalTo(1));
assertFalse(producer.transactionCommitted());
mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1L);
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return producer.commitCount() == 1;
}
}, "StreamsThread did not commit transaction.");
producer.fenceProducer();
mockTime.sleep(config.getLong(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG) + 1L);
consumer.addRecord(new ConsumerRecord<>(topic1, 1, 0, new byte[0], new byte[0]));
try {
thread.runOnce(-1);
fail("Should have thrown TaskMigratedException");
} catch (final TaskMigratedException expected) {
/* ignore */
}
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return thread.tasks().isEmpty();
}
}, "StreamsThread did not remove fenced zombie task.");
assertThat(producer.commitCount(), equalTo(1L));
}
use of org.apache.kafka.clients.producer.MockProducer in project apache-kafka-on-k8s by banzaicloud.
the class RecordCollectorTest method shouldNotThrowStreamsExceptionOnCloseIfASendFailedWithContinueExceptionHandler.
@SuppressWarnings("unchecked")
@Test
public void shouldNotThrowStreamsExceptionOnCloseIfASendFailedWithContinueExceptionHandler() {
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.close();
}
use of org.apache.kafka.clients.producer.MockProducer in project apache-kafka-on-k8s by banzaicloud.
the class RecordCollectorTest method shouldNotThrowStreamsExceptionOnSubsequentCallIfASendFailsWithContinueExceptionHandler.
@SuppressWarnings("unchecked")
@Test
public void shouldNotThrowStreamsExceptionOnSubsequentCallIfASendFailsWithContinueExceptionHandler() {
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.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 shouldThrowIfTopicIsUnknownWithDefaultExceptionHandler.
@SuppressWarnings("unchecked")
@Test(expected = StreamsException.class)
public void shouldThrowIfTopicIsUnknownWithDefaultExceptionHandler() {
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 DefaultProductionExceptionHandler());
collector.send("topic1", "3", "0", null, stringSerializer, stringSerializer, streamPartitioner);
}
Aggregations