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.Assert.assertThrows;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.lang.reflect.InvocationTargetException;
027import java.lang.reflect.Method;
028import java.lang.reflect.Modifier;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
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.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
044
045@Category({ ClientTests.class, SmallTests.class })
046public class TestNettyRpcConnection {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestNettyRpcConnection.class);
051
052  private static final Logger LOG = LoggerFactory.getLogger(TestNettyRpcConnection.class);
053
054  private static NettyRpcClient CLIENT;
055
056  private static NettyRpcConnection CONN;
057
058  @BeforeClass
059  public static void setUp() throws IOException {
060    CLIENT = new NettyRpcClient(HBaseConfiguration.create());
061    CONN = new NettyRpcConnection(CLIENT,
062      new ConnectionId(User.getCurrent(), "test", Address.fromParts("localhost", 1234)));
063  }
064
065  @AfterClass
066  public static void tearDown() throws IOException {
067    Closeables.close(CLIENT, true);
068  }
069
070  @Test
071  public void testPrivateMethodExecutedInEventLoop() throws IllegalAccessException {
072    // make sure the test is executed with "-ea"
073    assertThrows(AssertionError.class, () -> {
074      assert false;
075    });
076    for (Method method : NettyRpcConnection.class.getDeclaredMethods()) {
077      if (Modifier.isPrivate(method.getModifiers()) && !method.getName().contains("$")) {
078        LOG.info("checking {}", method);
079        method.setAccessible(true);
080        // all private methods should be called inside the event loop thread, so calling it from
081        // this thread will cause the "assert eventLoop.inEventLoop();" to fail
082        try {
083          // now there is no primitive parameters for the private methods so let's pass null
084          method.invoke(CONN, new Object[method.getParameterCount()]);
085          fail("should fail with AssertionError");
086        } catch (InvocationTargetException e) {
087          assertThat(e.getCause(), instanceOf(AssertionError.class));
088        }
089      }
090    }
091  }
092}