dimitarndimitrov commented on code in PR #13856: URL: https://github.com/apache/kafka/pull/13856#discussion_r1233945004
########## server-common/src/test/java/org/apache/kafka/server/util/InterBrokerSendThreadTest.java: ########## @@ -0,0 +1,327 @@ +/* + * 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.util; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +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.same; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Queue; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import org.apache.kafka.clients.ClientRequest; +import org.apache.kafka.clients.ClientResponse; +import org.apache.kafka.clients.KafkaClient; +import org.apache.kafka.clients.RequestCompletionHandler; +import org.apache.kafka.common.Node; +import org.apache.kafka.common.errors.AuthenticationException; +import org.apache.kafka.common.errors.DisconnectException; +import org.apache.kafka.common.internals.FatalExitError; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.utils.Time; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; + +public class InterBrokerSendThreadTest { + + private final Time time = new MockTime(); + private final KafkaClient networkClient = mock(KafkaClient.class); + private final StubCompletionHandler completionHandler = new StubCompletionHandler(); + private final int requestTimeoutMs = 1000; + + class TestInterBrokerSendThread extends InterBrokerSendThread { + + private final Consumer<Throwable> exceptionCallback; + private final Queue<RequestAndCompletionHandler> queue = new ArrayDeque<>(); + + TestInterBrokerSendThread() { + this( + InterBrokerSendThreadTest.this.networkClient, + t -> { + throw (t instanceof RuntimeException) + ? ((RuntimeException) t) + : new RuntimeException(t); + }); + } + + TestInterBrokerSendThread(KafkaClient networkClient, Consumer<Throwable> exceptionCallback) { + super("name", networkClient, requestTimeoutMs, time); + this.exceptionCallback = exceptionCallback; + } + + void enqueue(RequestAndCompletionHandler request) { + queue.offer(request); + } + + @Override + public Collection<RequestAndCompletionHandler> generateRequests() { + return queue.isEmpty() ? Collections.emptyList() : Collections.singletonList(queue.poll()); + } + + @Override + protected void pollOnce(long maxTimeoutMs) { + try { + super.pollOnce(maxTimeoutMs); + } catch (Throwable t) { + exceptionCallback.accept(t); + } + } + } + + @Test + public void shutdownThreadShouldNotCauseException() throws InterruptedException, IOException { + // InterBrokerSendThread#shutdown calls NetworkClient#initiateClose first so NetworkClient#poll + // can throw DisconnectException when thread is running + when(networkClient.poll(anyLong(), anyLong())).thenThrow(new DisconnectException()); + when(networkClient.active()).thenReturn(false); + + AtomicReference<Throwable> exception = new AtomicReference<>(); + final InterBrokerSendThread thread = + new TestInterBrokerSendThread(networkClient, exception::getAndSet); + thread.shutdown(); + thread.pollOnce(100); + + verify(networkClient).poll(anyLong(), anyLong()); + verify(networkClient).initiateClose(); + verify(networkClient).close(); + verify(networkClient).active(); + verifyNoMoreInteractions(networkClient); + + assertNull(exception.get()); + } + + @Test + public void disconnectWithoutShutdownShouldCauseException() { + DisconnectException de = new DisconnectException(); + when(networkClient.poll(anyLong(), anyLong())).thenThrow(de); + when(networkClient.active()).thenReturn(true); + + AtomicReference<Throwable> throwable = new AtomicReference<>(); + final InterBrokerSendThread thread = + new TestInterBrokerSendThread(networkClient, throwable::getAndSet); + thread.pollOnce(100); + + verify(networkClient).poll(anyLong(), anyLong()); + verify(networkClient).active(); + verifyNoMoreInteractions(networkClient); + + Throwable thrown = throwable.get(); + assertNotNull(thrown); + assertTrue(thrown instanceof FatalExitError); + } + + @Test + public void shouldNotSendAnythingWhenNoRequests() { + final InterBrokerSendThread sendThread = new TestInterBrokerSendThread(); + + // poll is always called but there should be no further invocations on NetworkClient + when(networkClient.poll(anyLong(), anyLong())).thenReturn(new ArrayList<>()); + + sendThread.doWork(); + + verify(networkClient).poll(anyLong(), anyLong()); + verifyNoMoreInteractions(networkClient); + + assertFalse(completionHandler.executedWithDisconnectedResponse); + } + + @Test + public void shouldCreateClientRequestAndSendWhenNodeIsReady() { + final AbstractRequest.Builder<?> request = new StubRequestBuilder<>(); + final Node node = new Node(1, "", 8080); + final RequestAndCompletionHandler handler = + new RequestAndCompletionHandler(time.milliseconds(), node, request, completionHandler); + final TestInterBrokerSendThread sendThread = new TestInterBrokerSendThread(); + + final ClientRequest clientRequest = + new ClientRequest("dest", request, 0, "1", 0, true, requestTimeoutMs, handler.handler); + + when(networkClient.newClientRequest( + ArgumentMatchers.eq("1"), + same(handler.request), + anyLong(), + ArgumentMatchers.eq(true), + ArgumentMatchers.eq(requestTimeoutMs), + same(handler.handler))) + .thenReturn(clientRequest); Review Comment: Done. - Each closing parentheses on its own new line would be consistent with the non-test file, but it looks really unwieldy in tests, and inconsistent with the lines where everything fits on a single line. -- 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