View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.lang.annotation.Annotation;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.apache.hadoop.hbase.testclassification.LargeTests;
24  import org.apache.hadoop.hbase.testclassification.MediumTests;
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.junit.experimental.categories.Category;
27  import org.junit.internal.runners.statements.FailOnTimeout;
28  import org.junit.rules.TestRule;
29  import org.junit.rules.Timeout;
30  import org.junit.runner.Description;
31  import org.junit.runners.model.Statement;
32  
33  /**
34   * Set a test method timeout based off the test categories small, medium, large.
35   * Based on junit Timeout TestRule; see https://github.com/junit-team/junit/wiki/Rules
36   */
37  public class CategoryBasedTimeout extends Timeout {
38  
39    @Deprecated
40    public CategoryBasedTimeout(int millis) {
41      super(millis);
42    }
43  
44    public CategoryBasedTimeout(long timeout, TimeUnit timeUnit) {
45      super(timeout, timeUnit);
46    }
47  
48    protected CategoryBasedTimeout(Builder builder) {
49      super(builder);
50    }
51  
52    public static Builder builder() {
53      return new CategoryBasedTimeout.Builder();
54    }
55  
56    public static class Builder extends Timeout.Builder {
57      public Timeout.Builder withTimeout(Class<?> clazz) {
58        Annotation annotation = clazz.getAnnotation(Category.class);
59        if (annotation != null) {
60          Category category = (Category)annotation;
61          for (Class<?> c: category.value()) {
62            if (c == SmallTests.class) {
63              // See SmallTests. Supposed to run 15 seconds.
64              return withTimeout(30, TimeUnit.SECONDS);
65            } else if (c == MediumTests.class) {
66              // See MediumTests. Supposed to run 50 seconds.
67              return withTimeout(180, TimeUnit.SECONDS);
68            } else if (c == LargeTests.class) {
69              // Let large tests have a ten minute timeout.
70              return withTimeout(10, TimeUnit.MINUTES);
71            }
72          }
73        }
74        return this;
75      }
76    }
77  }