Search in sources :

Example 6 with MockConsumer

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;
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) Collection(java.util.Collection) PartitionInfo(org.apache.kafka.common.PartitionInfo) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with MockConsumer

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
    }
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) List(java.util.List) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) MockTime(org.apache.kafka.common.utils.MockTime) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 8 with MockConsumer

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();
}
Also used : Consumer(org.apache.kafka.clients.consumer.Consumer) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) AuthorizationException(org.apache.kafka.common.errors.AuthorizationException) Test(org.junit.Test)

Aggregations

MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)8 Test (org.junit.Test)6 PartitionInfo (org.apache.kafka.common.PartitionInfo)4 Consumer (org.apache.kafka.clients.consumer.Consumer)3 MockTime (org.apache.kafka.common.utils.MockTime)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 TimeoutException (org.apache.kafka.common.errors.TimeoutException)2 StreamsException (org.apache.kafka.streams.errors.StreamsException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PCollectionList (org.apache.beam.sdk.values.PCollectionList)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1