You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "gabriellefu (via GitHub)" <gi...@apache.org> on 2026/10/26 19:33:20 UTC
[PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
gabriellefu opened a new pull request, #23968:
URL: https://github.com/apache/kafka/pull/23968
Add separate test file to ensure the three deprecated subscribe methods are covered
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "gabriellefu (via GitHub) via jira" <ji...@kafka.apache.org>.
gabriellefu commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4540123418
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
Review Comment:
sounds good, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "adikou (via GitHub)" <gi...@apache.org>.
adikou commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4382400246
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
Review Comment:
Can we continue to verify the right exception is thrown for CLASSIC against subscription pattern to make sure existing behaviour is maintained? Right now, it's a blind spot by _not_ invoking the method
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
Review Comment:
Missing onPartitionsLost to flow through the 2-arg listener
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
Review Comment:
For parity, the other Plaintext Consumer Callback test checks for the right partitions being invoked and not just that they were invoked
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
Review Comment:
You can also verify that if you create a ConsumerRebalanceListener that overrides the 2-arg parameters then the 1-arg variants are _not_ invoked since this class extends RebalanceListener
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "adikou (via GitHub) via jira" <ji...@kafka.apache.org>.
adikou commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4540962730
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(UnsupportedOperationException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var assignedPartitions = new AtomicReference<Set<TopicPartition>>();
+ var revokedPartitions = new AtomicReference<Set<TopicPartition>>();
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ assignedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ revokedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (var consumer = createConsumer(groupProtocol)) {
+ subscribe.accept(consumer, listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> assignedPartitions.get() != null,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+ assertEquals(Set.of(tp), assignedPartitions.get());
+
+ consumer.unsubscribe();
+ assertEquals(Set.of(tp), revokedPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CLASSIC);
+ }
+
+ @ClusterTest(serverProperties = {
+ @ClusterConfigProperty(key = CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500"),
+ @ClusterConfigProperty(key = CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500")
+ })
+ public void testAsyncConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOnPartitionsLost(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var lostPartitions = new AtomicReference<Set<TopicPartition>>();
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ }
+
+ @Override
+ public void onPartitionsLost(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ lostPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (Consumer<byte[], byte[]> consumer = cluster.consumer(Map.of(
+ GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT),
+ ENABLE_AUTO_COMMIT_CONFIG, "false",
+ MAX_POLL_INTERVAL_MS_CONFIG, "1000"
+ ))) {
+ consumer.subscribe(List.of(topic), listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ partitionsAssigned::get,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+
+ // Exceed max.poll.interval.ms so the consumer leaves the group and its partitions are lost
+ TimeUnit.MILLISECONDS.sleep(3000);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> lostPartitions.get() != null,
+ "Timed out before onPartitionsLost was invoked"
+ );
+ assertEquals(Set.of(tp), lostPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CLASSIC);
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOverridingTwoArgCallbacks(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ var oneArgCallbackInvoked = new AtomicBoolean(false);
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ oneArgCallbackInvoked.set(true);
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ oneArgCallbackInvoked.set(true);
+ }
+
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions, RebalanceConsumer rc) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions, RebalanceConsumer rc) {
+ if (partitions.contains(tp)) {
+ partitionsRevoked.set(true);
+ }
+ }
+ };
+ try (var consumer = createConsumer(groupProtocol)) {
+ consumer.subscribe(List.of(topic), listener);
Review Comment:
I think I merged #23115 today. So this should fail the build? Worth checking now, but either case we should switch to the setRebalanceListener call here
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(UnsupportedOperationException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var assignedPartitions = new AtomicReference<Set<TopicPartition>>();
+ var revokedPartitions = new AtomicReference<Set<TopicPartition>>();
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ assignedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ revokedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (var consumer = createConsumer(groupProtocol)) {
+ subscribe.accept(consumer, listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> assignedPartitions.get() != null,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+ assertEquals(Set.of(tp), assignedPartitions.get());
+
+ consumer.unsubscribe();
+ assertEquals(Set.of(tp), revokedPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CLASSIC);
+ }
+
+ @ClusterTest(serverProperties = {
+ @ClusterConfigProperty(key = CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500"),
+ @ClusterConfigProperty(key = CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500")
+ })
+ public void testAsyncConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOnPartitionsLost(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var lostPartitions = new AtomicReference<Set<TopicPartition>>();
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ }
+
+ @Override
+ public void onPartitionsLost(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ lostPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (Consumer<byte[], byte[]> consumer = cluster.consumer(Map.of(
+ GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT),
+ ENABLE_AUTO_COMMIT_CONFIG, "false",
+ MAX_POLL_INTERVAL_MS_CONFIG, "1000"
+ ))) {
+ consumer.subscribe(List.of(topic), listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ partitionsAssigned::get,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+
+ // Exceed max.poll.interval.ms so the consumer leaves the group and its partitions are lost
+ TimeUnit.MILLISECONDS.sleep(3000);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> lostPartitions.get() != null,
+ "Timed out before onPartitionsLost was invoked"
+ );
+ assertEquals(Set.of(tp), lostPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CLASSIC);
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOverridingTwoArgCallbacks(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ var oneArgCallbackInvoked = new AtomicBoolean(false);
+ var listener = new ConsumerRebalanceListener() {
Review Comment:
For test coverage, we should also test onPartitionsLost 1-arg not invoked
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "gabriellefu (via GitHub) via jira" <ji...@kafka.apache.org>.
gabriellefu commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4540132172
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
Review Comment:
added, thanks
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
Review Comment:
added, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "gabriellefu (via GitHub) via jira" <ji...@kafka.apache.org>.
gabriellefu commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4540136774
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
Review Comment:
Updated, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-21365: Add separate test file to ensure the three deprecated subscribe methods are covered [kafka]
Posted by "gabriellefu (via GitHub) via jira" <ji...@kafka.apache.org>.
gabriellefu commented on code in PR #23968:
URL: https://github.com/apache/kafka/pull/23968#discussion_r4541143098
##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerDeprecatedSubscribeTest.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.ClientsTestUtils;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+
+import org.junit.jupiter.api.BeforeEach;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiConsumer;
+import java.util.regex.Pattern;
+
+import static org.apache.kafka.clients.consumer.ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
+import static org.apache.kafka.clients.consumer.ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CLASSIC;
+import static org.apache.kafka.clients.consumer.GroupProtocol.CONSUMER;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.apache.kafka.coordinator.group.GroupCoordinatorConfig.CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests the deprecated {@code subscribe} overloads that take a {@link ConsumerRebalanceListener}.
+ * Remove this class together with those overloads in 5.0.
+ */
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = PlaintextConsumerDeprecatedSubscribeTest.BROKER_COUNT
+)
+@SuppressWarnings("removal")
+public class PlaintextConsumerDeprecatedSubscribeTest {
+
+ public static final int BROKER_COUNT = 3;
+ private final ClusterInstance cluster;
+ private final String topic = "topic";
+ private final TopicPartition tp = new TopicPartition(topic, 0);
+
+ public PlaintextConsumerDeprecatedSubscribeTest(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ cluster.createTopic(topic, 1, (short) BROKER_COUNT);
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeTopicsWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(List.of(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CLASSIC, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribePatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(Pattern.compile(topic), listener));
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeSubscriptionPatternWithListener() throws InterruptedException {
+ testSubscribeWithListener(CONSUMER, (consumer, listener) -> consumer.subscribe(new SubscriptionPattern(topic), listener));
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CLASSIC)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(UnsupportedOperationException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithNullListener() {
+ try (var consumer = createConsumer(CONSUMER)) {
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(List.of(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(Pattern.compile(topic), null));
+ assertThrows(IllegalArgumentException.class, () -> consumer.subscribe(new SubscriptionPattern(topic), null));
+ }
+ }
+
+ private void testSubscribeWithListener(
+ GroupProtocol groupProtocol,
+ BiConsumer<Consumer<byte[], byte[]>, ConsumerRebalanceListener> subscribe
+ ) throws InterruptedException {
+ var assignedPartitions = new AtomicReference<Set<TopicPartition>>();
+ var revokedPartitions = new AtomicReference<Set<TopicPartition>>();
+ // Only the one-argument callbacks are implemented, so they must be reached through the two-argument defaults.
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ assignedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ revokedPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (var consumer = createConsumer(groupProtocol)) {
+ subscribe.accept(consumer, listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> assignedPartitions.get() != null,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+ assertEquals(Set.of(tp), assignedPartitions.get());
+
+ consumer.unsubscribe();
+ assertEquals(Set.of(tp), revokedPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CLASSIC);
+ }
+
+ @ClusterTest(serverProperties = {
+ @ClusterConfigProperty(key = CONSUMER_GROUP_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500"),
+ @ClusterConfigProperty(key = CONSUMER_GROUP_MIN_HEARTBEAT_INTERVAL_MS_CONFIG, value = "500")
+ })
+ public void testAsyncConsumerSubscribeWithListenerOnPartitionsLost() throws InterruptedException {
+ testSubscribeWithListenerOnPartitionsLost(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOnPartitionsLost(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var lostPartitions = new AtomicReference<Set<TopicPartition>>();
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ }
+
+ @Override
+ public void onPartitionsLost(Collection<TopicPartition> partitions) {
+ if (partitions.contains(tp)) {
+ lostPartitions.set(Set.copyOf(partitions));
+ }
+ }
+ };
+ try (Consumer<byte[], byte[]> consumer = cluster.consumer(Map.of(
+ GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT),
+ ENABLE_AUTO_COMMIT_CONFIG, "false",
+ MAX_POLL_INTERVAL_MS_CONFIG, "1000"
+ ))) {
+ consumer.subscribe(List.of(topic), listener);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ partitionsAssigned::get,
+ "Timed out before onPartitionsAssigned was invoked"
+ );
+
+ // Exceed max.poll.interval.ms so the consumer leaves the group and its partitions are lost
+ TimeUnit.MILLISECONDS.sleep(3000);
+ ClientsTestUtils.pollUntilTrue(
+ consumer,
+ () -> lostPartitions.get() != null,
+ "Timed out before onPartitionsLost was invoked"
+ );
+ assertEquals(Set.of(tp), lostPartitions.get());
+ }
+ }
+
+ @ClusterTest
+ public void testClassicConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CLASSIC);
+ }
+
+ @ClusterTest
+ public void testAsyncConsumerSubscribeWithListenerOverridingTwoArgCallbacks() throws InterruptedException {
+ testSubscribeWithListenerOverridingTwoArgCallbacks(CONSUMER);
+ }
+
+ private void testSubscribeWithListenerOverridingTwoArgCallbacks(GroupProtocol groupProtocol) throws InterruptedException {
+ var partitionsAssigned = new AtomicBoolean(false);
+ var partitionsRevoked = new AtomicBoolean(false);
+ var oneArgCallbackInvoked = new AtomicBoolean(false);
+ var listener = new ConsumerRebalanceListener() {
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
+ oneArgCallbackInvoked.set(true);
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
+ oneArgCallbackInvoked.set(true);
+ }
+
+ @Override
+ public void onPartitionsAssigned(Collection<TopicPartition> partitions, RebalanceConsumer rc) {
+ if (partitions.contains(tp)) {
+ partitionsAssigned.set(true);
+ }
+ }
+
+ @Override
+ public void onPartitionsRevoked(Collection<TopicPartition> partitions, RebalanceConsumer rc) {
+ if (partitions.contains(tp)) {
+ partitionsRevoked.set(true);
+ }
+ }
+ };
+ try (var consumer = createConsumer(groupProtocol)) {
+ consumer.subscribe(List.of(topic), listener);
Review Comment:
sounds good, I will switch it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org