HBASE-25292 Improve InetSocketAddress usage discipline (#2669)
[hbase.git] / hbase-client / src / test / java / org / apache / hadoop / hbase / ipc / TestNettyRpcConnection.java
blob8782fe116b07ddc527a89401b657e86d91c0538f
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.ipc;
20 import static org.hamcrest.CoreMatchers.instanceOf;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.junit.Assert.assertThrows;
23 import static org.junit.Assert.fail;
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.Modifier;
29 import org.apache.hadoop.hbase.HBaseClassTestRule;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.net.Address;
32 import org.apache.hadoop.hbase.security.User;
33 import org.apache.hadoop.hbase.testclassification.ClientTests;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
45 @Category({ ClientTests.class, SmallTests.class })
46 public class TestNettyRpcConnection {
48 @ClassRule
49 public static final HBaseClassTestRule CLASS_RULE =
50 HBaseClassTestRule.forClass(TestNettyRpcConnection.class);
52 private static final Logger LOG = LoggerFactory.getLogger(TestNettyRpcConnection.class);
54 private static NettyRpcClient CLIENT;
56 private static NettyRpcConnection CONN;
58 @BeforeClass
59 public static void setUp() throws IOException {
60 CLIENT = new NettyRpcClient(HBaseConfiguration.create());
61 CONN = new NettyRpcConnection(CLIENT,
62 new ConnectionId(User.getCurrent(), "test", Address.fromParts("localhost", 1234)));
65 @AfterClass
66 public static void tearDown() throws IOException {
67 Closeables.close(CLIENT, true);
70 @Test
71 public void testPrivateMethodExecutedInEventLoop() throws IllegalAccessException {
72 // make sure the test is executed with "-ea"
73 assertThrows(AssertionError.class, () -> {
74 assert false;
75 });
76 for (Method method : NettyRpcConnection.class.getDeclaredMethods()) {
77 if (Modifier.isPrivate(method.getModifiers()) && !method.getName().contains("$")) {
78 LOG.info("checking {}", method);
79 method.setAccessible(true);
80 // all private methods should be called inside the event loop thread, so calling it from
81 // this thread will cause the "assert eventLoop.inEventLoop();" to fail
82 try {
83 // now there is no primitive parameters for the private methods so let's pass null
84 method.invoke(CONN, new Object[method.getParameterCount()]);
85 fail("should fail with AssertionError");
86 } catch (InvocationTargetException e) {
87 assertThat(e.getCause(), instanceOf(AssertionError.class));