FrankYang0529 commented on code in PR #21693:
URL: https://github.com/apache/kafka/pull/21693#discussion_r2911675335


##########
server/src/test/java/org/apache/kafka/server/NodeToControllerRequestThreadTest.java:
##########
@@ -0,0 +1,549 @@
+/*
+ * 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.server;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.clients.ManualMetadataUpdater;
+import org.apache.kafka.clients.Metadata;
+import org.apache.kafka.clients.MockClient;
+import org.apache.kafka.clients.NodeApiVersions;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.config.AbstractConfig;
+import org.apache.kafka.common.message.EnvelopeResponseData;
+import org.apache.kafka.common.message.MetadataRequestData;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.EnvelopeRequest;
+import org.apache.kafka.common.requests.EnvelopeResponse;
+import org.apache.kafka.common.requests.MetadataRequest;
+import org.apache.kafka.common.requests.MetadataResponse;
+import org.apache.kafka.common.requests.RequestTestUtils;
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import 
org.apache.kafka.common.security.authenticator.DefaultKafkaPrincipalBuilder;
+import org.apache.kafka.common.utils.MockTime;
+import org.apache.kafka.server.common.ControllerRequestCompletionHandler;
+import org.apache.kafka.server.config.ReplicationConfigs;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.mock;
+
+class NodeToControllerRequestThreadTest {
+
+    private static AbstractConfig createConfig() {
+        return new AbstractConfig(ReplicationConfigs.CONFIG_DEF, Map.of());
+    }
+
+    private static ControllerInformation controllerInfo(Optional<Node> node) {
+        return new ControllerInformation(node, new ListenerName(""), 
SecurityProtocol.PLAINTEXT, "");
+    }
+
+    private static ControllerInformation emptyControllerInfo() {
+        return controllerInfo(Optional.empty());
+    }
+
+    /**
+     * Creates a supplier that returns {@code first} on the first call,
+     * and {@code second} on all subsequent calls, matching the behavior
+     * of Mockito's {@code thenReturn(first, second)}.
+     *
+     * <p>This avoids mocking {@code Supplier<ControllerInformation>} which 
would
+     * require {@code @SuppressWarnings("unchecked")} due to generic type 
erasure.
+     */
+    private static Supplier<ControllerInformation> sequentialProvider(
+            ControllerInformation first, ControllerInformation second) {
+        AtomicReference<ControllerInformation> ref = new 
AtomicReference<>(first);
+        return () -> ref.getAndSet(second);
+    }
+
+    @Test
+    void testRetryTimeoutWhileControllerNotAvailable() {
+        MockTime time = new MockTime();
+        AbstractConfig config = createConfig();
+        Metadata metadata = mock(Metadata.class);
+        MockClient mockClient = new MockClient(time, metadata);
+
+        Supplier<ControllerInformation> controllerNodeProvider = 
NodeToControllerRequestThreadTest::emptyControllerInfo;
+
+        long retryTimeoutMs = 30000;
+        NodeToControllerRequestThread testRequestThread = new 
NodeToControllerRequestThread(
+            mockClient, new ManualMetadataUpdater(),
+            controllerNodeProvider, config, time, "", retryTimeoutMs);
+        testRequestThread.setStarted(true);
+
+        TestControllerRequestCompletionHandler completionHandler =
+            new TestControllerRequestCompletionHandler(null);
+        NodeToControllerQueueItem queueItem = new NodeToControllerQueueItem(
+            time.milliseconds(),
+            new MetadataRequest.Builder(new MetadataRequestData()),
+            completionHandler
+        );
+
+        testRequestThread.enqueue(queueItem);
+        testRequestThread.doWork();
+        assertEquals(1, testRequestThread.queueSize());
+
+        time.sleep(retryTimeoutMs);
+        testRequestThread.doWork();
+        assertEquals(0, testRequestThread.queueSize());
+        assertTrue(completionHandler.timedOut.get());
+    }
+
+    @Test
+    void testRequestsSent() {
+        // just a simple test that tests whether the request from 1 -> 2 is 
sent and the response callback is called
+        MockTime time = new MockTime();
+        AbstractConfig config = createConfig();
+        int controllerId = 2;
+
+        Metadata metadata = mock(Metadata.class);
+        MockClient mockClient = new MockClient(time, metadata);
+
+        Node activeController = new Node(controllerId, "host", 1234);
+        Supplier<ControllerInformation> controllerNodeProvider =
+            () -> controllerInfo(Optional.of(activeController));
+
+        MetadataResponse expectedResponse = 
RequestTestUtils.metadataUpdateWith(2, Map.of("a", 2));
+        NodeToControllerRequestThread testRequestThread = new 
NodeToControllerRequestThread(
+            mockClient, new ManualMetadataUpdater(),
+            controllerNodeProvider, config, time, "", Long.MAX_VALUE);
+        testRequestThread.setStarted(true);
+        mockClient.prepareResponse(expectedResponse);
+
+        TestControllerRequestCompletionHandler completionHandler =
+            new TestControllerRequestCompletionHandler(expectedResponse);
+        NodeToControllerQueueItem queueItem = new NodeToControllerQueueItem(
+            time.milliseconds(),
+            new MetadataRequest.Builder(new MetadataRequestData()),
+            completionHandler
+        );
+
+        testRequestThread.enqueue(queueItem);
+        assertEquals(1, testRequestThread.queueSize());
+
+        // initialize to the controller
+        testRequestThread.doWork();
+        // send and process the request
+        testRequestThread.doWork();
+
+        assertEquals(0, testRequestThread.queueSize());
+        assertTrue(completionHandler.completed.get());
+    }
+
+    @Test
+    void testControllerChanged() {
+        // in this test the current broker is 1, and the controller changes 
from 2 -> 3 then back: 3 -> 2
+        MockTime time = new MockTime();
+        AbstractConfig config = createConfig();
+        int oldControllerId = 1;
+        int newControllerId = 2;

Review Comment:
   It looks like the controller id and the comment is mismatch.



-- 
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