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;
045
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 class to
078   * 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  static int getNumParameters(Class<?> clazz) {
112    RunWith[] runWiths = clazz.getAnnotationsByType(RunWith.class);
113    boolean testParameterized = runWiths != null
114      && Arrays.stream(runWiths).anyMatch((r) -> r.value().equals(Parameterized.class));
115    if (!testParameterized) {
116      return 1;
117    }
118    for (Method method : clazz.getMethods()) {
119      if (!isParametersMethod(method)) {
120        continue;
121      }
122      // Found the parameters method. Figure out the number of parameters.
123      Object parameters;
124      try {
125        parameters = method.invoke(clazz);
126      } catch (IllegalAccessException | InvocationTargetException e) {
127        LOG.warn("Error invoking parameters method {} in test class {}", method.getName(), clazz,
128          e);
129        continue;
130      }
131      if (parameters instanceof List) {
132        return ((List) parameters).size();
133      } else if (parameters instanceof Collection) {
134        return ((Collection) parameters).size();
135      } else if (parameters instanceof Iterable) {
136        return Iterables.size((Iterable) parameters);
137      } else if (parameters instanceof Object[]) {
138        return ((Object[]) parameters).length;
139      }
140    }
141    LOG.warn("Unable to determine parameters size. Returning the default of 1.");
142    return 1;
143  }
144
145  /**
146   * Helper method that checks if the input method is a valid JUnit @Parameters method.
147   * @param method Input method.
148   * @return true if the method is a valid JUnit parameters method, false otherwise.
149   */
150  private static boolean isParametersMethod(@NonNull Method method) {
151    // A valid parameters method is public static and with @Parameters annotation.
152    boolean methodPublicStatic =
153      Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers());
154    Parameters[] params = method.getAnnotationsByType(Parameters.class);
155    return methodPublicStatic && (params != null && params.length > 0);
156  }
157
158  public static HBaseClassTestRule forClass(Class<?> clazz) {
159    return new HBaseClassTestRule(clazz, Timeout.builder().withLookingForStuckThread(true)
160      .withTimeout(getTimeoutInSeconds(clazz), TimeUnit.SECONDS).build());
161  }
162
163  @Override
164  public Statement apply(Statement base, Description description) {
165    return systemExitRule.apply(timeout.apply(base, description), description);
166  }
167
168}