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.assertTrue; 024import static org.junit.Assert.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.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseConfiguration; 032import org.apache.hadoop.hbase.net.Address; 033import org.apache.hadoop.hbase.security.User; 034import org.apache.hadoop.hbase.testclassification.ClientTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.junit.AfterClass; 037import org.junit.BeforeClass; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.slf4j.Logger; 042import org.slf4j.LoggerFactory; 043 044import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 045 046@Category({ ClientTests.class, SmallTests.class }) 047public class TestNettyRpcConnection { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestNettyRpcConnection.class); 052 053 private static final Logger LOG = LoggerFactory.getLogger(TestNettyRpcConnection.class); 054 055 private static NettyRpcClient CLIENT; 056 057 private static NettyRpcConnection CONN; 058 059 @BeforeClass 060 public static void setUp() throws IOException { 061 CLIENT = new NettyRpcClient(HBaseConfiguration.create()); 062 CONN = new NettyRpcConnection(CLIENT, 063 new ConnectionId(User.getCurrent(), "test", Address.fromParts("localhost", 1234))); 064 } 065 066 @AfterClass 067 public static void tearDown() throws IOException { 068 Closeables.close(CLIENT, true); 069 } 070 071 @Test 072 public void testPrivateMethodExecutedInEventLoop() throws IllegalAccessException { 073 assertThrows(AssertionError.class, () -> { 074 assertTrue(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}