[27/30] hbase git commit: HBASE-19873 Add a CategoryBasedTimeout ClassRule for all UTs

2018-01-29 Thread stack
http://git-wip-us.apache.org/repos/asf/hbase/blob/bbf3bae7/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java
--
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java
 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java
index f4687fa..997a0bb 100644
--- 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteBufferUtils.java
@@ -1,18 +1,19 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership. The ASF
- * licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
  *
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 package org.apache.hadoop.hbase.util;
 
@@ -43,7 +44,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
-
+import org.apache.hadoop.hbase.HBaseClassTestRule;
 import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
 import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.testclassification.MiscTests;
@@ -51,6 +52,7 @@ import org.apache.hadoop.hbase.testclassification.SmallTests;
 import org.apache.hadoop.io.WritableUtils;
 import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
@@ -59,6 +61,11 @@ import org.junit.runners.Parameterized;
 @Category({MiscTests.class, SmallTests.class})
 @RunWith(Parameterized.class)
 public class TestByteBufferUtils {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+  HBaseClassTestRule.forClass(TestByteBufferUtils.class);
+
   private static final String UNSAFE_AVAIL_NAME = "UNSAFE_AVAIL";
   private static final String UNSAFE_UNALIGNED_NAME = "UNSAFE_UNALIGNED";
   private byte[] array;

http://git-wip-us.apache.org/repos/asf/hbase/blob/bbf3bae7/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java
--
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java
 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java
index 297bd62..b7de95b 100644
--- 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestByteRangeWithKVSerialization.java
@@ -19,19 +19,24 @@ package org.apache.hadoop.hbase.util;
 
 import java.util.ArrayList;
 import java.util.List;
-
 import org.apache.hadoop.hbase.ArrayBackedTag;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.Tag;
 import org.apache.hadoop.hbase.testclassification.MiscTests;
 import org.apache.hadoop.hbase.testclassification.SmallTests;
 import org.junit.Assert;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
 @Category({MiscTests.class, SmallTests.class})
 public class TestByteRangeWithKVSerialization {
 
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+  HBaseClassTestRule.forClass(TestByteRangeWithKVSerialization.class);
+
   static void writeCell(PositionedByteRange pbr, KeyValue kv) throws Exception 
{
 pbr.putInt(kv.getKeyLength());
 

[27/30] hbase git commit: HBASE-19873 Add a CategoryBasedTimeout ClassRule for all UTs

2018-01-28 Thread zhangduo
http://git-wip-us.apache.org/repos/asf/hbase/blob/918599ef/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java
--
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java
new file mode 100644
index 000..74bd70e
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase;
+
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hbase.testclassification.LargeTests;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TestRule;
+import org.junit.rules.Timeout;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * The class level TestRule for all the tests. Every test class should have a 
{@code ClassRule} with
+ * it.
+ * 
+ * For now it only sets a test method timeout based off the test categories 
small, medium, large.
+ * Based on junit Timeout TestRule; see 
https://github.com/junit-team/junit/wiki/Rules
+ */
+@InterfaceAudience.Private
+public final class HBaseClassTestRule implements TestRule {
+
+  private final Class clazz;
+
+  private final Timeout timeout;
+
+  private HBaseClassTestRule(Class clazz, Timeout timeout) {
+this.clazz = clazz;
+this.timeout = timeout;
+  }
+
+  /**
+   * Mainly used for {@link HBaseClassTestRuleChecker} to confirm that we use 
the correct
+   * class to generate timeout ClassRule.
+   */
+  public Class getClazz() {
+return clazz;
+  }
+
+  private static long getTimeoutInSeconds(Class clazz) {
+Category[] categories = clazz.getAnnotationsByType(Category.class);
+if (categories.length == 0) {
+  throw new IllegalArgumentException(clazz.getName() + " is not annotated 
with @Category");
+}
+for (Class c : categories[0].value()) {
+  if (c == SmallTests.class) {
+// See SmallTests. Supposed to run 15 seconds.
+return 30;
+  } else if (c == MediumTests.class) {
+// See MediumTests. Supposed to run 50 seconds.
+return 180;
+  } else if (c == LargeTests.class) {
+// Let large tests have a ten minute timeout.
+return TimeUnit.MINUTES.toSeconds(10);
+  }
+}
+throw new IllegalArgumentException(
+clazz.getName() + " does not have SmallTests/MediumTests/LargeTests in 
@Category");
+  }
+
+  public static HBaseClassTestRule forClass(Class clazz) {
+return new HBaseClassTestRule(clazz, 
Timeout.builder().withLookingForStuckThread(true)
+.withTimeout(getTimeoutInSeconds(clazz), TimeUnit.SECONDS).build());
+  }
+
+  @Override
+  public Statement apply(Statement base, Description description) {
+return timeout.apply(base, description);
+  }
+}

http://git-wip-us.apache.org/repos/asf/hbase/blob/918599ef/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRuleChecker.java
--
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRuleChecker.java
 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRuleChecker.java
new file mode 100644
index 000..97c657f
--- /dev/null
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRuleChecker.java
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or