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