ShivsundarR commented on code in PR #17009:
URL: https://github.com/apache/kafka/pull/17009#discussion_r1738045779


##########
core/src/test/java/kafka/test/api/ShareConsumerTest.java:
##########
@@ -0,0 +1,1651 @@
+/*
+ * 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 kafka.test.api;
+
+import kafka.api.BaseConsumerTest;
+import kafka.testkit.KafkaClusterTestKit;
+import kafka.testkit.TestKitNodes;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.admin.RecordsToDelete;
+import org.apache.kafka.clients.consumer.AcknowledgeType;
+import org.apache.kafka.clients.consumer.AcknowledgementCommitCallback;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.KafkaShareConsumer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.InterruptException;
+import org.apache.kafka.common.errors.InvalidRecordStateException;
+import org.apache.kafka.common.errors.InvalidTopicException;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.common.header.Header;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.record.TimestampType;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+import org.apache.kafka.common.serialization.ByteArraySerializer;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serializer;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+@Timeout(1200)
+@Tag("integration")
+@SuppressWarnings("deprecation")
+public class ShareConsumerTest {
+    private KafkaClusterTestKit cluster;
+    private final TopicPartition tp = new TopicPartition("topic", 0);
+    private final TopicPartition warmupTp = new TopicPartition("warmup", 0);
+
+    @BeforeEach
+    public void createCluster() throws Exception {
+        cluster = new KafkaClusterTestKit.Builder(
+                new TestKitNodes.Builder()
+                        .setNumBrokerNodes(1)
+                        .setNumControllerNodes(1)
+                        .build())
+                .setConfigProp("auto.create.topics.enable", "false")
+                .setConfigProp("group.coordinator.rebalance.protocols", 
"classic,consumer,share")
+                .setConfigProp("group.share.enable", "true")
+                .setConfigProp("group.share.partition.max.record.locks", 
"10000")
+                .setConfigProp("group.share.persister.class.name", 
"org.apache.kafka.server.group.share.NoOpShareStatePersister")
+                .setConfigProp("group.share.record.lock.duration.ms", "15000")
+                .setConfigProp("offsets.topic.replication.factor", "1")
+                .setConfigProp("share.coordinator.state.topic.min.isr", "1")
+                
.setConfigProp("share.coordinator.state.topic.replication.factor", "1")
+                .setConfigProp("transaction.state.log.min.isr", "1")
+                .setConfigProp("transaction.state.log.replication.factor", "1")
+                .setConfigProp("unstable.api.versions.enable", "true")
+                .build();
+        cluster.format();
+        cluster.startup();
+        cluster.waitForActiveController();
+        cluster.waitForReadyBrokers();
+        createTopic("topic");
+        warmup();
+    }
+
+    @AfterEach
+    public void destroyCluster() throws Exception {
+        cluster.close();
+    }
+
+    @Test
+    public void testPollNoSubscribeFails() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        assertEquals(Collections.emptySet(), shareConsumer.subscription());
+        // "Consumer is not subscribed to any topics."
+        assertThrows(IllegalStateException.class, () -> 
shareConsumer.poll(Duration.ofMillis(500)));
+        shareConsumer.close();
+    }
+
+    @Test
+    public void testSubscribeAndPollNoRecords() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        Set<String> subscription = Collections.singleton(tp.topic());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(500));
+        shareConsumer.close();
+        assertEquals(0, records.count());
+    }
+
+    @Test
+    public void testSubscribePollUnsubscribe() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        Set<String> subscription = Collections.singleton(tp.topic());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(500));
+        shareConsumer.unsubscribe();
+        assertEquals(Collections.emptySet(), shareConsumer.subscription());
+        shareConsumer.close();
+        assertEquals(0, records.count());
+    }
+
+    @Test
+    public void testSubscribePollSubscribe() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        Set<String> subscription = Collections.singleton(tp.topic());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(500));
+        assertEquals(0, records.count());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        records = shareConsumer.poll(Duration.ofMillis(500));
+        shareConsumer.close();
+        assertEquals(0, records.count());
+    }
+
+    @Test
+    public void testSubscribeUnsubscribePollFails() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        Set<String> subscription = Collections.singleton(tp.topic());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(500));
+        shareConsumer.unsubscribe();
+        assertEquals(Collections.emptySet(), shareConsumer.subscription());
+        // "Consumer is not subscribed to any topics."
+        assertThrows(IllegalStateException.class, () -> 
shareConsumer.poll(Duration.ofMillis(500)));
+        shareConsumer.close();
+        assertEquals(0, records.count());
+    }
+
+    @Test
+    public void testSubscribeSubscribeEmptyPollFails() {
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        Set<String> subscription = Collections.singleton(tp.topic());
+        shareConsumer.subscribe(subscription);
+        assertEquals(subscription, shareConsumer.subscription());
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(500));
+        shareConsumer.subscribe(Collections.emptySet());
+        assertEquals(Collections.emptySet(), shareConsumer.subscription());
+        // "Consumer is not subscribed to any topics."
+        assertThrows(IllegalStateException.class, () -> 
shareConsumer.poll(Duration.ofMillis(500)));
+        shareConsumer.close();
+        assertEquals(0, records.count());
+    }
+
+    @Test
+    public void testSubscriptionAndPoll() {
+        ProducerRecord<byte[], byte[]> record = new 
ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), 
"value".getBytes());
+        KafkaProducer<byte[], byte[]> producer = createProducer(new 
ByteArraySerializer(), new ByteArraySerializer());
+        producer.send(record);
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        shareConsumer.subscribe(Collections.singleton(tp.topic()));
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(5000));
+        assertEquals(1, records.count());
+        shareConsumer.close();
+        producer.close();
+    }
+
+    @Test
+    public void testSubscriptionAndPollMultiple() {
+        ProducerRecord<byte[], byte[]> record = new 
ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), 
"value".getBytes());
+        KafkaProducer<byte[], byte[]> producer = createProducer(new 
ByteArraySerializer(), new ByteArraySerializer());
+        producer.send(record);
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        shareConsumer.subscribe(Collections.singleton(tp.topic()));
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(5000));
+        assertEquals(1, records.count());
+        producer.send(record);
+        records = shareConsumer.poll(Duration.ofMillis(5000));
+        assertEquals(1, records.count());
+        producer.send(record);
+        records = shareConsumer.poll(Duration.ofMillis(5000));
+        assertEquals(1, records.count());
+        shareConsumer.close();
+        producer.close();
+    }
+
+    @Test
+    public void testAcknowledgementCommitCallbackSuccessfulAcknowledgement() {
+        Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+        Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+        ProducerRecord<byte[], byte[]> record = new 
ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), 
"value".getBytes());
+        KafkaProducer<byte[], byte[]> producer = createProducer(new 
ByteArraySerializer(), new ByteArraySerializer());
+        producer.send(record);
+        KafkaShareConsumer<byte[], byte[]> shareConsumer = 
createShareConsumer(new ByteArrayDeserializer(), new ByteArrayDeserializer(), 
"group1");
+        shareConsumer.setAcknowledgementCommitCallback(new 
TestableAcknowledgeCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+        shareConsumer.subscribe(Collections.singleton(tp.topic()));
+
+        ConsumerRecords<byte[], byte[]> records = 
shareConsumer.poll(Duration.ofMillis(5000));
+        assertEquals(1, records.count());
+        // Now in the second poll, we implicitly acknowledge the record 
received in the first poll.
+        // We get back the acknowledgment error code after the second poll.
+        // When we start the 3rd poll, the acknowledgment commit callback is 
invoked.
+        shareConsumer.poll(Duration.ofMillis(5000));

Review Comment:
   Right yeah, makes sense, I have updated few of the tests now with a shorter 
duration.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to