use of org.apache.kafka.clients.consumer.MockConsumer in project kafka by apache.
the class AbstractTaskTest method shouldThrowProcessorStateExceptionOnInitializeOffsetsWhenAuthorizationException.
@Test(expected = ProcessorStateException.class)
public void shouldThrowProcessorStateExceptionOnInitializeOffsetsWhenAuthorizationException() throws Exception {
final Consumer consumer = mockConsumer(new AuthorizationException("blah"));
final AbstractTask task = createTask(consumer);
task.initializeOffsetLimits();
}
use of org.apache.kafka.clients.consumer.MockConsumer in project kafka by apache.
the class ProcessorTopologyTestDriver method createRestoreConsumer.
/**
* Utility method that creates the {@link MockConsumer} used for restoring state, which should not be done by this
* driver object unless this method is overwritten with a functional consumer.
*
* @param id the ID of the stream task
* @param storeToChangelogTopic the map of the names of the stores to the changelog topics
* @return the mock consumer; never null
*/
protected MockConsumer<byte[], byte[]> createRestoreConsumer(TaskId id, Map<String, String> storeToChangelogTopic) {
MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(OffsetResetStrategy.LATEST) {
@Override
public synchronized void seekToEnd(Collection<TopicPartition> partitions) {
// do nothing ...
}
@Override
public synchronized void seekToBeginning(Collection<TopicPartition> partitions) {
// do nothing ...
}
@Override
public synchronized long position(TopicPartition partition) {
// do nothing ...
return 0L;
}
};
// For each store ...
for (Map.Entry<String, String> storeAndTopic : storeToChangelogTopic.entrySet()) {
String topicName = storeAndTopic.getValue();
// Set up the restore-state topic ...
// consumer.subscribe(new TopicPartition(topicName, 1));
// Set up the partition that matches the ID (which is what ProcessorStateManager expects) ...
List<PartitionInfo> partitionInfos = new ArrayList<>();
partitionInfos.add(new PartitionInfo(topicName, PARTITION_ID, null, null, null));
consumer.updatePartitions(topicName, partitionInfos);
consumer.updateEndOffsets(Collections.singletonMap(new TopicPartition(topicName, PARTITION_ID), 0L));
}
return consumer;
}
use of org.apache.kafka.clients.consumer.MockConsumer in project kafka by apache.
the class StoreChangelogReaderTest method shouldThrowStreamsExceptionWhenTimeoutExceptionThrown.
@SuppressWarnings("unchecked")
@Test
public void shouldThrowStreamsExceptionWhenTimeoutExceptionThrown() throws Exception {
final MockConsumer<byte[], byte[]> consumer = new MockConsumer(OffsetResetStrategy.EARLIEST) {
@Override
public Map<String, List<PartitionInfo>> listTopics() {
throw new TimeoutException("KABOOM!");
}
};
final StoreChangelogReader changelogReader = new StoreChangelogReader(consumer, new MockTime(), 0);
try {
changelogReader.validatePartitionExists(topicPartition, "store");
fail("Should have thrown streams exception");
} catch (final StreamsException e) {
// pass
}
}
use of org.apache.kafka.clients.consumer.MockConsumer in project apache-kafka-on-k8s by banzaicloud.
the class TopologyTestDriver method createRestoreConsumer.
private MockConsumer<byte[], byte[]> createRestoreConsumer(final Map<String, String> storeToChangelogTopic) {
final MockConsumer<byte[], byte[]> consumer = new MockConsumer<byte[], byte[]>(OffsetResetStrategy.LATEST) {
@Override
public synchronized void seekToEnd(final Collection<TopicPartition> partitions) {
}
@Override
public synchronized void seekToBeginning(final Collection<TopicPartition> partitions) {
}
@Override
public synchronized long position(final TopicPartition partition) {
return 0L;
}
};
// for each store
for (final Map.Entry<String, String> storeAndTopic : storeToChangelogTopic.entrySet()) {
final String topicName = storeAndTopic.getValue();
// Set up the restore-state topic ...
// consumer.subscribe(new TopicPartition(topicName, 0));
// Set up the partition that matches the ID (which is what ProcessorStateManager expects) ...
final List<PartitionInfo> partitionInfos = new ArrayList<>();
partitionInfos.add(new PartitionInfo(topicName, PARTITION_ID, null, null, null));
consumer.updatePartitions(topicName, partitionInfos);
consumer.updateEndOffsets(Collections.singletonMap(new TopicPartition(topicName, PARTITION_ID), 0L));
}
return consumer;
}
use of org.apache.kafka.clients.consumer.MockConsumer in project apache-kafka-on-k8s by banzaicloud.
the class StreamThreadTest method shouldInjectProducerPerTaskUsingClientSupplierOnCreateIfEosEnable.
@Test
public void shouldInjectProducerPerTaskUsingClientSupplierOnCreateIfEosEnable() {
internalTopologyBuilder.addSource(null, "source1", null, null, null, topic1);
final StreamThread thread = createStreamThread(clientId, new StreamsConfig(configProps(true)), true);
thread.setState(StreamThread.State.RUNNING);
thread.rebalanceListener.onPartitionsRevoked(Collections.<TopicPartition>emptyList());
final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
final List<TopicPartition> assignedPartitions = new ArrayList<>();
// assign single partition
assignedPartitions.add(t1p1);
assignedPartitions.add(t1p2);
activeTasks.put(task1, Collections.singleton(t1p1));
activeTasks.put(task2, Collections.singleton(t1p2));
thread.taskManager().setAssignmentMetadata(activeTasks, Collections.<TaskId, Set<TopicPartition>>emptyMap());
final MockConsumer<byte[], byte[]> mockConsumer = (MockConsumer<byte[], byte[]>) thread.consumer;
mockConsumer.assign(assignedPartitions);
Map<TopicPartition, Long> beginOffsets = new HashMap<>();
beginOffsets.put(t1p1, 0L);
beginOffsets.put(t1p2, 0L);
mockConsumer.updateBeginningOffsets(beginOffsets);
thread.rebalanceListener.onPartitionsAssigned(new HashSet<>(assignedPartitions));
thread.runOnce(-1);
assertEquals(thread.tasks().size(), clientSupplier.producers.size());
assertSame(clientSupplier.consumer, thread.consumer);
assertSame(clientSupplier.restoreConsumer, thread.restoreConsumer);
}
Aggregations