Apache9 commented on code in PR #8058:
URL: https://github.com/apache/hbase/pull/8058#discussion_r3068219849


##########
hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestNettyServerRpcConnectionClientConnectionInfo.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.hadoop.hbase.ipc;
+
+import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Collection;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.Waiter;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.RPCTests;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
+import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
+import org.apache.hbase.thirdparty.io.netty.channel.Channel;
+import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
+import 
org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
+
+import 
org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos.EchoRequestProto;
+import 
org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto;
+
+/**
+ * Test to verify that ClientConnectionInfo is properly registered and 
unregistered in
+ * NettyServerRpcConnection lifecycle to prevent memory leaks.
+ */
+@Category({ RPCTests.class, MediumTests.class })
+public class TestNettyServerRpcConnectionClientConnectionInfo {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+    
HBaseClassTestRule.forClass(TestNettyServerRpcConnectionClientConnectionInfo.class);
+
+  private static Configuration CONF = HBaseConfiguration.create();
+  private NioEventLoopGroup group;
+  private NettyRpcServer server;
+  private NettyRpcClient client;
+  private TestProtobufRpcProto.BlockingInterface stub;
+
+  @Before
+  public void setUp() throws IOException {
+    group = new NioEventLoopGroup();
+    server = new NettyRpcServer(null, getClass().getSimpleName(),
+      Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, 
null)),
+      new InetSocketAddress("localhost", 0), CONF, new FifoRpcScheduler(CONF, 
1), true);
+    NettyRpcClientConfigHelper.setEventLoopConfig(CONF, group, 
NioSocketChannel.class);
+    client = new NettyRpcClient(CONF);
+    server.start();
+    stub = TestProtobufRpcServiceImpl.newBlockingStub(client, 
server.getListenerAddress());
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    Closeables.close(client, true);
+    server.stop();
+    group.shutdownGracefully().sync();
+  }
+
+  @Test
+  public void testClientConnectionInfoRegisteredAndUnregisteredFromRegistry() 
throws Exception {
+    ClientConnectionRegistry registry = server.getClientConnectionRegistry();
+    // Verify that the registry is not null
+    assertNotNull(registry);
+
+    // Get the initial number of client connections registered
+    Collection<ClientConnectionInfo> connections = 
registry.getClientConnections();
+    int initialSize = connections.size();
+
+    // Make an RPC call which will trigger connection header read and 
registration
+    assertEquals("test",
+      stub.echo(null, 
EchoRequestProto.newBuilder().setMessage("test").build()).getMessage());
+
+    // Wait for the ClientConnectionInfo to be registered
+    Waiter.waitFor(CONF, 5000, () -> registry.getClientConnections().size() > 
initialSize);
+
+    // Verify that one connection is registered
+    connections = registry.getClientConnections();
+    assertTrue(connections.size() >= initialSize + 1);
+
+    // Get the channel to verify it closes
+    Channel channel = server.allChannels.stream().filter(c -> c instanceof 
NioSocketChannel)
+      .findFirst().orElse(null);
+    assertNotNull(channel);
+
+    // Close the client connection
+    client.close();
+
+    // Wait for the channel to close
+    channel.closeFuture().await(5000, TimeUnit.MILLISECONDS);
+
+    // Wait for the ClientConnectionInfo to be unregistered
+    Waiter.waitFor(CONF, 5000, () -> registry.getClientConnections().size() <= 
initialSize);
+
+    // Verify that the connection is unregistered
+    connections = registry.getClientConnections();
+    assertTrue(connections.size() <= initialSize);

Review Comment:
   I do not think other tests can connect to the rpc server started in this 
test?



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