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;
019
020import edu.umd.cs.findbugs.annotations.NonNull;
021import java.lang.reflect.InvocationTargetException;
022import java.lang.reflect.Method;
023import java.lang.reflect.Modifier;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.List;
028import java.util.Set;
029import java.util.concurrent.TimeUnit;
030import org.apache.hadoop.hbase.testclassification.IntegrationTests;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.yetus.audience.InterfaceAudience;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestRule;
037import org.junit.rules.Timeout;
038import org.junit.runner.Description;
039import org.junit.runner.RunWith;
040import org.junit.runners.Parameterized;
041import org.junit.runners.Parameterized.Parameters;
042import org.junit.runners.model.Statement;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
046import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
047import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
048
049/**
050 * The class level TestRule for all the tests. Every test class should have a {@code ClassRule} with
051 * it.
052 * <p>
053 * For now it only sets a test method timeout based off the test categories small, medium, large.
054 * Based on junit Timeout TestRule; see https://github.com/junit-team/junit/wiki/Rules
055 */
056@InterfaceAudience.Private
057public final class HBaseClassTestRule implements TestRule {
058  private static final Logger LOG = LoggerFactory.getLogger(HBaseClassTestRule.class);
059  public static final Set<Class<?>> UNIT_TEST_CLASSES = Collections.unmodifiableSet(
060      Sets.<Class<?>> newHashSet(SmallTests.class, MediumTests.class, LargeTests.class));
061
062  // Each unit test has this timeout.
063  private static long PER_UNIT_TEST_TIMEOUT_MINS = 13;
064
065  private final Class<?> clazz;
066
067  private final Timeout timeout;
068
069  private final SystemExitRule systemExitRule = new SystemExitRule();
070
071  private HBaseClassTestRule(Class<?> clazz, Timeout timeout) {
072    this.clazz = clazz;
073    this.timeout = timeout;
074  }
075
076  /**
077   * Mainly used for {@link HBaseClassTestRuleChecker} to confirm that we use the correct
078   * class to generate timeout ClassRule.
079   */
080  public Class<?> getClazz() {
081    return clazz;
082  }
083
084  private static long getTimeoutInSeconds(Class<?> clazz) {
085    Category[] categories = clazz.getAnnotationsByType(Category.class);
086    // Starting JUnit 4.13, it appears that the timeout is applied across all the parameterized
087    // runs. So the timeout is multiplied by number of parameterized runs.
088    int numParams = getNumParameters(clazz);
089    // @Category is not repeatable -- it is only possible to get an array of length zero or one.
090    if (categories.length == 1) {
091      for (Class<?> c : categories[0].value()) {
092        if (UNIT_TEST_CLASSES.contains(c)) {
093          long timeout = numParams * PER_UNIT_TEST_TIMEOUT_MINS;
094          LOG.info("Test {} timeout: {} mins", clazz, timeout);
095          return TimeUnit.MINUTES.toSeconds(timeout);
096        }
097        if (c == IntegrationTests.class) {
098          return TimeUnit.MINUTES.toSeconds(Long.MAX_VALUE);
099        }
100      }
101    }
102    throw new IllegalArgumentException(
103        clazz.getName() + " does not have SmallTests/MediumTests/LargeTests in @Category");
104  }
105
106  /**
107   * @param clazz Test class that is running.
108   * @return the number of parameters for this given test class. If the test is not parameterized or
109   *   if there is any issue determining the number of parameters, returns 1.
110   */
111  @VisibleForTesting
112  static int getNumParameters(Class<?> clazz) {
113    RunWith[] runWiths = clazz.getAnnotationsByType(RunWith.class);
114    boolean testParameterized = runWiths != null && Arrays.stream(runWiths).anyMatch(
115      (r) -> r.value().equals(Parameterized.class));
116    if (!testParameterized) {
117      return 1;
118    }
119    for (Method method : clazz.getMethods()) {
120      if (!isParametersMethod(method)) {
121        continue;
122      }
123      // Found the parameters method. Figure out the number of parameters.
124      Object parameters;
125      try {
126        parameters = method.invoke(clazz);
127      } catch (IllegalAccessException | InvocationTargetException e) {
128        LOG.warn("Error invoking parameters method {} in test class {}",
129            method.getName(), clazz, e);
130        continue;
131      }
132      if (parameters instanceof List) {
133        return  ((List) parameters).size();
134      } else if (parameters instanceof Collection) {
135        return  ((Collection) parameters).size();
136      } else if (parameters instanceof Iterable) {
137        return Iterables.size((Iterable) parameters);
138      } else if (parameters instanceof Object[]) {
139        return ((Object[]) parameters).length;
140      }
141    }
142    LOG.warn("Unable to determine parameters size. Returning the default of 1.");
143    return 1;
144  }
145
146  /**
147   * Helper method that checks if the input method is a valid JUnit @Parameters method.
148   * @param method Input method.
149   * @return true if the method is a valid JUnit parameters method, false otherwise.
150   */
151  private static boolean isParametersMethod(@NonNull Method method) {
152    // A valid parameters method is public static and with @Parameters annotation.
153    boolean methodPublicStatic = Modifier.isPublic(method.getModifiers()) &&
154        Modifier.isStatic(method.getModifiers());
155    Parameters[] params = method.getAnnotationsByType(Parameters.class);
156    return methodPublicStatic && (params != null && params.length > 0);
157  }
158
159  public static HBaseClassTestRule forClass(Class<?> clazz) {
160    return new HBaseClassTestRule(clazz, Timeout.builder().withLookingForStuckThread(true)
161        .withTimeout(getTimeoutInSeconds(clazz), TimeUnit.SECONDS).build());
162  }
163
164  @Override
165  public Statement apply(Statement base, Description description) {
166    return timeout.apply(systemExitRule.apply(base, description), description);
167  }
168
169}