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.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseServerException;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ ClientTests.class, SmallTests.class })
034public class TestRemoteWithExtrasException {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestRemoteWithExtrasException.class);
039
040  /**
041   * test verifies that we honor the inherent value of an exception for isServerOverloaded. We don't
042   * want a false value passed into RemoteWithExtrasExceptions to override the inherent value of an
043   * exception if it's already true. This could be due to an out of date server not sending the
044   * proto field we expect.
045   */
046  @Test
047  public void itUsesExceptionDefaultValueForServerOverloaded() {
048    // pass false for server overloaded, we still expect the exception to be true due to
049    // the exception type
050    RemoteWithExtrasException ex = new RemoteWithExtrasException(
051      ServerOverloadedException.class.getName(), "server is overloaded", false, false);
052    IOException result = ex.unwrapRemoteException();
053
054    assertEquals(result.getClass(), ServerOverloadedException.class);
055    assertTrue(((ServerOverloadedException) result).isServerOverloaded());
056  }
057
058  @Test
059  public void itUsesPassedServerOverloadedValue() {
060    String exceptionClass = HBaseServerException.class.getName();
061    String message = "server is overloaded";
062    RemoteWithExtrasException ex =
063      new RemoteWithExtrasException(exceptionClass, message, false, false);
064    IOException result = ex.unwrapRemoteException();
065
066    assertTrue(result instanceof HBaseServerException);
067    assertFalse(((HBaseServerException) result).isServerOverloaded());
068
069    // run again with true value passed in
070    ex = new RemoteWithExtrasException(exceptionClass, message, false, true);
071    result = ex.unwrapRemoteException();
072
073    assertTrue(result instanceof HBaseServerException);
074    // expect true this time
075    assertTrue(((HBaseServerException) result).isServerOverloaded());
076  }
077
078  private static class ServerOverloadedException extends HBaseServerException {
079    public ServerOverloadedException(String message) {
080      super(true, message);
081    }
082  }
083
084}