private KafkaConsumer<String, String> newConsumer(Time time, KafkaClient client, Metadata metadata, PartitionAssignor assignor, boolean autoCommitEnabled) { return newConsumer(time, client, metadata, assignor, OffsetResetStrategy.EARLIEST, autoCommitEnabled, groupId); }
private KafkaConsumer<byte[], byte[]> newConsumer(String groupId) { return newConsumer(groupId, Optional.empty()); }
private KafkaConsumer<byte[], byte[]> newConsumer(String groupId, Optional<Boolean> enableAutoCommit) { Properties props = new Properties(); props.setProperty(ConsumerConfig.CLIENT_ID_CONFIG, "my.consumer"); props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); props.setProperty(ConsumerConfig.METRIC_REPORTER_CLASSES_CONFIG, MockMetricsReporter.class.getName()); if (groupId != null) props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId); if (enableAutoCommit.isPresent()) props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit.get().toString()); return newConsumer(props); }
private KafkaConsumer<String, String> newConsumerNoAutoCommit(Time time, KafkaClient client, Metadata metadata) { return newConsumer(time, client, metadata, new RangeAssignor(), OffsetResetStrategy.EARLIEST, false, groupId); }
@Test(expected = IllegalStateException.class) public void testPollWithNoSubscription() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null)) { consumer.poll(Duration.ZERO); } }
@Test(expected = IllegalStateException.class) public void testSubscriptionWithEmptyPartitionAssignment() { Properties props = new Properties(); props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); props.setProperty(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, ""); props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId); try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(props)) { consumer.subscribe(singletonList(topic)); } }
@Test(expected = IllegalArgumentException.class) public void testAssignOnEmptyTopicInPartition() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null)) { consumer.assign(singleton(new TopicPartition(" ", 0))); } }
@Test(expected = IllegalStateException.class) public void testPollWithEmptyUserAssignment() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.assign(Collections.<TopicPartition>emptySet()); consumer.poll(Duration.ZERO); } }
@Test(expected = IllegalStateException.class) public void testPollWithEmptySubscription() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.subscribe(Collections.<String>emptyList()); consumer.poll(Duration.ZERO); } }
@Test(expected = IllegalArgumentException.class) public void testAssignOnNullTopicInPartition() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null)) { consumer.assign(singleton(new TopicPartition(null, 0))); } }
@Test(expected = IllegalArgumentException.class) public void testSubscriptionOnEmptyTopic() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { String emptyTopic = " "; consumer.subscribe(singletonList(emptyTopic)); } }
@Test(expected = IllegalArgumentException.class) public void testSubscriptionOnNullTopic() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.subscribe(singletonList((String) null)); } }
@Test(expected = IllegalArgumentException.class) public void testSeekNegative() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null)) { consumer.assign(singleton(new TopicPartition("nonExistTopic", 0))); consumer.seek(new TopicPartition("nonExistTopic", 0), -1); } }
@Test(expected = IllegalArgumentException.class) public void testAssignOnNullTopicPartition() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null)) { consumer.assign(null); } }
@Test(expected = IllegalArgumentException.class) public void testSubscriptionOnNullPattern() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.subscribe((Pattern) null); } }
@Test(expected = IllegalArgumentException.class) public void testSubscriptionOnNullTopicCollection() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.subscribe((List<String>) null); } }
@Test public void testAssignOnEmptyTopicPartition() { try (KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId)) { consumer.assign(Collections.<TopicPartition>emptyList()); assertTrue(consumer.subscription().isEmpty()); assertTrue(consumer.assignment().isEmpty()); } }
@Test public void closeShouldBeIdempotent() { KafkaConsumer<byte[], byte[]> consumer = newConsumer((String) null); consumer.close(); consumer.close(); consumer.close(); }
@Test public void testPause() { KafkaConsumer<byte[], byte[]> consumer = newConsumer(groupId); consumer.assign(singletonList(tp0)); assertEquals(singleton(tp0), consumer.assignment()); assertTrue(consumer.paused().isEmpty()); consumer.pause(singleton(tp0)); assertEquals(singleton(tp0), consumer.paused()); consumer.resume(singleton(tp0)); assertTrue(consumer.paused().isEmpty()); consumer.unsubscribe(); assertTrue(consumer.paused().isEmpty()); consumer.close(); }
private KafkaConsumer<String, String> consumerWithPendingAuthentication() { Time time = new MockTime(); Metadata metadata = createMetadata(); MockClient client = new MockClient(time, metadata); initMetadata(client, singletonMap(topic, 1)); Node node = metadata.fetch().nodes().get(0); PartitionAssignor assignor = new RangeAssignor(); client.createPendingAuthenticationError(node, 0); return newConsumer(time, client, metadata, assignor, false); }