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 java.util.Collections; 021import java.util.Set; 022import java.util.concurrent.TimeUnit; 023 024import org.apache.hadoop.hbase.testclassification.IntegrationTests; 025import org.apache.hadoop.hbase.testclassification.LargeTests; 026import org.apache.hadoop.hbase.testclassification.MediumTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.yetus.audience.InterfaceAudience; 029import org.junit.experimental.categories.Category; 030import org.junit.rules.TestRule; 031import org.junit.rules.Timeout; 032import org.junit.runner.Description; 033import org.junit.runners.model.Statement; 034 035import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 036 037/** 038 * The class level TestRule for all the tests. Every test class should have a {@code ClassRule} with 039 * it. 040 * <p> 041 * For now it only sets a test method timeout based off the test categories small, medium, large. 042 * Based on junit Timeout TestRule; see https://github.com/junit-team/junit/wiki/Rules 043 */ 044@InterfaceAudience.Private 045public final class HBaseClassTestRule implements TestRule { 046 public static final Set<Class<?>> UNIT_TEST_CLASSES = Collections.unmodifiableSet( 047 Sets.<Class<?>> newHashSet(SmallTests.class, MediumTests.class, LargeTests.class)); 048 049 private final Class<?> clazz; 050 051 private final Timeout timeout; 052 053 private HBaseClassTestRule(Class<?> clazz, Timeout timeout) { 054 this.clazz = clazz; 055 this.timeout = timeout; 056 } 057 058 /** 059 * Mainly used for {@link HBaseClassTestRuleChecker} to confirm that we use the correct 060 * class to generate timeout ClassRule. 061 */ 062 public Class<?> getClazz() { 063 return clazz; 064 } 065 066 private static long getTimeoutInSeconds(Class<?> clazz) { 067 Category[] categories = clazz.getAnnotationsByType(Category.class); 068 069 // @Category is not repeatable -- it is only possible to get an array of length zero or one. 070 if (categories.length == 1) { 071 for (Class<?> c : categories[0].value()) { 072 if (UNIT_TEST_CLASSES.contains(c)) { 073 // All tests have a 13 minutes timeout. 074 return TimeUnit.MINUTES.toSeconds(13); 075 } 076 if (c == IntegrationTests.class) { 077 return TimeUnit.MINUTES.toSeconds(Long.MAX_VALUE); 078 } 079 } 080 } 081 throw new IllegalArgumentException( 082 clazz.getName() + " does not have SmallTests/MediumTests/LargeTests in @Category"); 083 } 084 085 public static HBaseClassTestRule forClass(Class<?> clazz) { 086 return new HBaseClassTestRule(clazz, Timeout.builder().withLookingForStuckThread(true) 087 .withTimeout(getTimeoutInSeconds(clazz), TimeUnit.SECONDS).build()); 088 } 089 090 @Override 091 public Statement apply(Statement base, Description description) { 092 return timeout.apply(base, description); 093 } 094}