From 9f34a6a818b7a5ec2ef53282f9c77dd93d9dda2e Mon Sep 17 00:00:00 2001 From: sullis Date: Sun, 10 Mar 2024 12:27:01 -0700 Subject: [PATCH 1/2] add unit test for ServerChannels --- .../core/transport/ServerChannelsTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java diff --git a/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java b/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java new file mode 100644 index 00000000000..09d71a6b725 --- /dev/null +++ b/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java @@ -0,0 +1,90 @@ +/** + * 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.pinot.core.transport; + +import com.sun.net.httpserver.HttpServer; +import java.net.InetSocketAddress; +import org.apache.pinot.common.config.NettyConfig; +import org.apache.pinot.common.metrics.BrokerMetrics; +import org.apache.pinot.common.request.BrokerRequest; +import org.apache.pinot.common.request.InstanceRequest; +import org.apache.pinot.spi.config.table.TableType; +import org.apache.pinot.spi.env.PinotConfiguration; +import org.apache.pinot.spi.metrics.PinotMetricUtils; +import org.apache.pinot.spi.metrics.PinotMetricsRegistry; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.mockito.Mockito.mock; + + +public class ServerChannelsTest { + private static HttpServer _dummyServer; + + @DataProvider + public Object[][] parameters() { + return new Object[][]{ + new Object[]{true}, new Object[]{false}, + }; + } + + @BeforeClass + public void beforeClass() + throws Exception { + _dummyServer = HttpServer.create(); + _dummyServer.bind(new InetSocketAddress("localhost", 0), 0); + _dummyServer.start(); + } + + @AfterClass + public void afterClass() + throws Exception { + if (_dummyServer != null) { + _dummyServer.stop(0); + } + } + + @Test(dataProvider = "parameters") + public void testConnect(final boolean nativeTransportEnabled) + throws Exception { + PinotMetricUtils.init(new PinotConfiguration()); + PinotMetricsRegistry registry = PinotMetricUtils.getPinotMetricsRegistry(); + BrokerMetrics brokerMetrics = new BrokerMetrics(registry); + NettyConfig nettyConfig = new NettyConfig(); + nettyConfig.setNativeTransportsEnabled(nativeTransportEnabled); + QueryRouter queryRouter = mock(QueryRouter.class); + + ServerRoutingInstance serverRoutingInstance = + new ServerRoutingInstance("localhost", _dummyServer.getAddress().getPort(), TableType.REALTIME); + ServerChannels serverChannels = new ServerChannels(queryRouter, brokerMetrics, nettyConfig, null); + serverChannels.connect(serverRoutingInstance); + + final long requestId = System.currentTimeMillis(); + + AsyncQueryResponse asyncQueryResponse = mock(AsyncQueryResponse.class); + BrokerRequest brokerRequest = new BrokerRequest(); + InstanceRequest instanceRequest = new InstanceRequest(); + instanceRequest.setRequestId(requestId); + instanceRequest.setQuery(brokerRequest); + serverChannels.sendRequest("dummy_table_name", asyncQueryResponse, serverRoutingInstance, instanceRequest, 1000); + serverChannels.shutDown(); + } +} From 5ed97c4ebd7e059a8ffff4554542ce8d17989e63 Mon Sep 17 00:00:00 2001 From: sullis Date: Mon, 11 Mar 2024 17:23:41 -0700 Subject: [PATCH 2/2] incorporate code review feedback --- .../pinot/core/transport/ServerChannelsTest.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java b/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java index 09d71a6b725..abbbe5be56c 100644 --- a/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java +++ b/pinot-core/src/test/java/org/apache/pinot/core/transport/ServerChannelsTest.java @@ -25,9 +25,6 @@ import org.apache.pinot.common.request.BrokerRequest; import org.apache.pinot.common.request.InstanceRequest; import org.apache.pinot.spi.config.table.TableType; -import org.apache.pinot.spi.env.PinotConfiguration; -import org.apache.pinot.spi.metrics.PinotMetricUtils; -import org.apache.pinot.spi.metrics.PinotMetricsRegistry; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; @@ -47,7 +44,7 @@ public Object[][] parameters() { } @BeforeClass - public void beforeClass() + public void setUp() throws Exception { _dummyServer = HttpServer.create(); _dummyServer.bind(new InetSocketAddress("localhost", 0), 0); @@ -55,7 +52,7 @@ public void beforeClass() } @AfterClass - public void afterClass() + public void tearDown() throws Exception { if (_dummyServer != null) { _dummyServer.stop(0); @@ -63,11 +60,9 @@ public void afterClass() } @Test(dataProvider = "parameters") - public void testConnect(final boolean nativeTransportEnabled) + public void testConnect(boolean nativeTransportEnabled) throws Exception { - PinotMetricUtils.init(new PinotConfiguration()); - PinotMetricsRegistry registry = PinotMetricUtils.getPinotMetricsRegistry(); - BrokerMetrics brokerMetrics = new BrokerMetrics(registry); + BrokerMetrics brokerMetrics = mock(BrokerMetrics.class); NettyConfig nettyConfig = new NettyConfig(); nettyConfig.setNativeTransportsEnabled(nativeTransportEnabled); QueryRouter queryRouter = mock(QueryRouter.class);