kirktrue commented on code in PR #14406:
URL: https://github.com/apache/kafka/pull/14406#discussion_r1364704339


##########
clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkThreadTest.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.internals;
+
+import org.apache.kafka.clients.MockClient;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.clients.consumer.internals.events.ApplicationEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.ApplicationEventProcessor;
+import 
org.apache.kafka.clients.consumer.internals.events.AssignmentChangeApplicationEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.CommitApplicationEvent;
+import org.apache.kafka.clients.consumer.internals.events.FetchEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.ListOffsetsApplicationEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.NewTopicsMetadataUpdateRequestEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.ResetPositionsApplicationEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.TopicMetadataApplicationEvent;
+import 
org.apache.kafka.clients.consumer.internals.events.ValidatePositionsApplicationEvent;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.message.FindCoordinatorRequestData;
+import org.apache.kafka.common.requests.FindCoordinatorRequest;
+import org.apache.kafka.common.requests.MetadataResponse;
+import org.apache.kafka.common.requests.RequestTestUtils;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.test.TestCondition;
+import org.apache.kafka.test.TestUtils;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
+
+import static 
org.apache.kafka.clients.consumer.internals.ConsumerTestBuilder.DEFAULT_REQUEST_TIMEOUT_MS;
+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.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@SuppressWarnings("ClassDataAbstractionCoupling")
+public class ConsumerNetworkThreadTest {
+
+    private ConsumerTestBuilder.ConsumerNetworkThreadTestBuilder testBuilder;
+    private Time time;
+    private ConsumerMetadata metadata;
+    private NetworkClientDelegate networkClient;
+    private BlockingQueue<ApplicationEvent> applicationEventsQueue;
+    private ApplicationEventProcessor applicationEventProcessor;
+    private OffsetsRequestManager offsetsRequestManager;
+    private CommitRequestManager commitManager;
+    private ConsumerNetworkThread consumerNetworkThread;
+    private MockClient client;
+
+    @BeforeEach
+    public void setup() {
+        testBuilder = new 
ConsumerTestBuilder.ConsumerNetworkThreadTestBuilder();
+        time = testBuilder.time;
+        metadata = testBuilder.metadata;
+        networkClient = testBuilder.networkClientDelegate;
+        client = testBuilder.client;
+        applicationEventsQueue = testBuilder.applicationEventQueue;
+        applicationEventProcessor = testBuilder.applicationEventProcessor;
+        commitManager = 
testBuilder.commitRequestManager.orElseThrow(IllegalStateException::new);
+        offsetsRequestManager = testBuilder.offsetsRequestManager;
+        consumerNetworkThread = testBuilder.consumerNetworkThread;
+        consumerNetworkThread.initializeResources();
+    }
+
+    @AfterEach
+    public void tearDown() {
+        if (testBuilder != null)
+            testBuilder.close();
+    }
+
+    @Test
+    public void testStartupAndTearDown() throws InterruptedException {
+        // The consumer is closed in 
ConsumerTestBuilder.ConsumerNetworkThreadTestBuilder.close()
+        // which is called from tearDown().
+        consumerNetworkThread.start();
+
+        TestCondition isStarted = () -> consumerNetworkThread.isRunning();
+        TestCondition isClosed = () -> !(consumerNetworkThread.isRunning() || 
consumerNetworkThread.isAlive());
+
+        // There's a nonzero amount of time between starting the thread and 
having it
+        // begin to execute our code. Wait for a bit before checking...
+        TestUtils.waitForCondition(isStarted,
+                "The consumer network thread did not start within " + 
DEFAULT_MAX_WAIT_MS + " ms");
+
+        consumerNetworkThread.close(Duration.ofMillis(DEFAULT_MAX_WAIT_MS));
+
+        TestUtils.waitForCondition(isClosed,
+                "The consumer network thread did not stop within " + 
DEFAULT_MAX_WAIT_MS + " ms");
+    }
+
+    @Test
+    public void testApplicationEvent() {
+        FetchEvent e = new FetchEvent();
+        applicationEventsQueue.add(e);
+        consumerNetworkThread.runOnce();
+        verify(applicationEventProcessor, times(1)).process(e);
+    }
+
+    @Test
+    public void testMetadataUpdateEvent() {
+        ApplicationEvent e = new NewTopicsMetadataUpdateRequestEvent();
+        applicationEventsQueue.add(e);
+        consumerNetworkThread.runOnce();
+        verify(metadata).requestUpdateForNewTopics();
+    }
+
+    @Test
+    public void testCommitEvent() {
+        ApplicationEvent e = new CommitApplicationEvent(new HashMap<>());
+        applicationEventsQueue.add(e);
+        consumerNetworkThread.runOnce();
+        
verify(applicationEventProcessor).process(any(CommitApplicationEvent.class));
+    }
+
+    @Test
+    public void testListOffsetsEventIsProcessed() {
+        Map<TopicPartition, Long> timestamps = Collections.singletonMap(new 
TopicPartition("topic1", 1), 5L);
+        ApplicationEvent e = new ListOffsetsApplicationEvent(timestamps, true);
+        applicationEventsQueue.add(e);
+        consumerNetworkThread.runOnce();
+        
verify(applicationEventProcessor).process(any(ListOffsetsApplicationEvent.class));
+        assertTrue(applicationEventsQueue.isEmpty());
+    }
+
+    @Test
+    public void testResetPositionsEventIsProcessed() {
+        ResetPositionsApplicationEvent e = new 
ResetPositionsApplicationEvent();
+        applicationEventsQueue.add(e);
+        consumerNetworkThread.runOnce();
+        
verify(applicationEventProcessor).process(any(ResetPositionsApplicationEvent.class));
+        assertTrue(applicationEventsQueue.isEmpty());
+    }
+
+    @Test
+    public void testResetPositionsProcessFailureIsIgnored() {
+        doThrow(new 
NullPointerException()).when(offsetsRequestManager).resetPositionsIfNeeded();

Review Comment:
   Filed KAFKA-15639.



-- 
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-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to