001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.hamcrest.CoreMatchers.instanceOf;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.junit.jupiter.api.Assertions.assertThrows;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024import static org.junit.jupiter.api.Assertions.fail;
025
026import java.io.IOException;
027import java.lang.reflect.InvocationTargetException;
028import java.lang.reflect.Method;
029import java.lang.reflect.Modifier;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.net.Address;
032import org.apache.hadoop.hbase.security.User;
033import org.apache.hadoop.hbase.testclassification.ClientTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.jupiter.api.AfterAll;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
043
044@Tag(ClientTests.TAG)
045@Tag(SmallTests.TAG)
046public class TestNettyRpcConnection {
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestNettyRpcConnection.class);
049
050  private static NettyRpcClient CLIENT;
051
052  private static NettyRpcConnection CONN;
053
054  @BeforeAll
055  public static void setUp() throws IOException {
056    CLIENT = new NettyRpcClient(HBaseConfiguration.create());
057    CONN = new NettyRpcConnection(CLIENT,
058      new ConnectionId(User.getCurrent(), "test", Address.fromParts("localhost", 1234)));
059  }
060
061  @AfterAll
062  public static void tearDown() throws IOException {
063    Closeables.close(CLIENT, true);
064  }
065
066  @Test
067  public void testPrivateMethodExecutedInEventLoop() throws IllegalAccessException {
068    assertThrows(AssertionError.class, () -> {
069      assertTrue(false);
070    });
071    for (Method method : NettyRpcConnection.class.getDeclaredMethods()) {
072      if (Modifier.isPrivate(method.getModifiers()) && !method.getName().contains("$")) {
073        LOG.info("checking {}", method);
074        method.setAccessible(true);
075        // all private methods should be called inside the event loop thread, so calling it from
076        // this thread will cause the "assert eventLoop.inEventLoop();" to fail
077        try {
078          // now there is no primitive parameters for the private methods so let's pass null
079          method.invoke(CONN, new Object[method.getParameterCount()]);
080          fail("should fail with AssertionError");
081        } catch (InvocationTargetException e) {
082          assertThat(e.getCause(), instanceOf(AssertionError.class));
083        }
084      }
085    }
086  }
087}