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