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.junit.Assert.assertThat;
022
023import java.lang.reflect.Constructor;
024import java.lang.reflect.InvocationTargetException;
025import java.net.InetSocketAddress;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.concurrent.TimeoutException;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.exceptions.ClientExceptionsUtil;
031import org.apache.hadoop.hbase.exceptions.TimeoutIOException;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ ClientTests.class, SmallTests.class })
039public class TestIPCUtil {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestIPCUtil.class);
044
045  private static Throwable create(Class<? extends Throwable> clazz) throws InstantiationException,
046      IllegalAccessException, InvocationTargetException, NoSuchMethodException {
047    try {
048      Constructor<? extends Throwable> c = clazz.getDeclaredConstructor();
049      c.setAccessible(true);
050      return c.newInstance();
051    } catch (NoSuchMethodException e) {
052      // fall through
053    }
054
055    try {
056      Constructor<? extends Throwable> c = clazz.getDeclaredConstructor(String.class);
057      c.setAccessible(true);
058      return c.newInstance("error");
059    } catch (NoSuchMethodException e) {
060      // fall through
061    }
062
063    try {
064      Constructor<? extends Throwable> c = clazz.getDeclaredConstructor(Throwable.class);
065      c.setAccessible(true);
066      return c.newInstance(new Exception("error"));
067    } catch (NoSuchMethodException e) {
068      // fall through
069    }
070
071    try {
072      Constructor<? extends Throwable> c =
073        clazz.getDeclaredConstructor(String.class, Throwable.class);
074      c.setAccessible(true);
075      return c.newInstance("error", new Exception("error"));
076    } catch (NoSuchMethodException e) {
077      // fall through
078    }
079
080    Constructor<? extends Throwable> c =
081      clazz.getDeclaredConstructor(Throwable.class, Throwable.class);
082    c.setAccessible(true);
083    return c.newInstance(new Exception("error"), "error");
084  }
085
086  /**
087   * See HBASE-21862, it is very important to keep the original exception type for connection
088   * exceptions.
089   */
090  @Test
091  public void testWrapConnectionException() throws Exception {
092    List<Throwable> exceptions = new ArrayList<>();
093    for (Class<? extends Throwable> clazz : ClientExceptionsUtil.getConnectionExceptionTypes()) {
094      exceptions.add(create(clazz));
095    }
096    InetSocketAddress addr = InetSocketAddress.createUnresolved("127.0.0.1", 12345);
097    for (Throwable exception : exceptions) {
098      if (exception instanceof TimeoutException) {
099        assertThat(IPCUtil.wrapException(addr, exception), instanceOf(TimeoutIOException.class));
100      } else {
101        assertThat(IPCUtil.wrapException(addr, exception), instanceOf(exception.getClass()));
102      }
103    }
104  }
105}