You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2020/04/21 05:20:59 UTC
[hbase] branch master updated: HBASE-24185: Junit tests do not
behave well with System.exit or Runtime.halt or JVM exits in general.
(#1540)
This is an automated email from the ASF dual-hosted git repository.
stack pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hbase.git
The following commit(s) were added to refs/heads/master by this push:
new f48b509 HBASE-24185: Junit tests do not behave well with System.exit or Runtime.halt or JVM exits in general. (#1540)
f48b509 is described below
commit f48b50964c9c8f772ef07d8ae97fdb07dcf23dfd
Author: Mark Robert Miller <ma...@apache.org>
AuthorDate: Tue Apr 21 00:20:08 2020 -0500
HBASE-24185: Junit tests do not behave well with System.exit or Runtime.halt or JVM exits in general. (#1540)
Signed-off-by: Viraj Jasani <vj...@apache.org>
Signed-off-by: stack <st...@apache.org>
---
.../apache/hadoop/hbase/HBaseClassTestRule.java | 5 +-
.../org/apache/hadoop/hbase/SystemExitRule.java | 55 ++++++++
.../apache/hadoop/hbase/TestSecurityManager.java | 140 +++++++++++++++++++++
.../apache/hadoop/hbase/TestSystemExitInTest.java | 39 ++++++
4 files changed, 238 insertions(+), 1 deletion(-)
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
index 0880ad0..16bce3e 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseClassTestRule.java
@@ -66,6 +66,8 @@ public final class HBaseClassTestRule implements TestRule {
private final Timeout timeout;
+ private final SystemExitRule systemExitRule = new SystemExitRule();
+
private HBaseClassTestRule(Class<?> clazz, Timeout timeout) {
this.clazz = clazz;
this.timeout = timeout;
@@ -161,6 +163,7 @@ public final class HBaseClassTestRule implements TestRule {
@Override
public Statement apply(Statement base, Description description) {
- return timeout.apply(base, description);
+ return timeout.apply(systemExitRule.apply(base, description), description);
}
+
}
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/SystemExitRule.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/SystemExitRule.java
new file mode 100644
index 0000000..d2c9f26
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/SystemExitRule.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/*
+* Test rules to prevent System.exit or Runtime.halt from exiting
+* the JVM - instead an exception is thrown.
+* */
+public class SystemExitRule implements TestRule {
+ final static SecurityManager securityManager = new TestSecurityManager();
+
+ @Override
+ public Statement apply(final Statement s, Description d) {
+ return new Statement() {
+ @Override public void evaluate() throws Throwable {
+
+ try {
+ forbidSystemExitCall();
+ s.evaluate();
+ } finally {
+ System.setSecurityManager(null);
+ }
+ }
+
+ };
+ };
+
+ // Exiting the JVM is not allowed in tests and this exception is thrown instead
+ // when it is done
+ public static class SystemExitInTestException extends SecurityException {
+ }
+
+ private static void forbidSystemExitCall() {
+ System.setSecurityManager(securityManager);
+ }
+}
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSecurityManager.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSecurityManager.java
new file mode 100644
index 0000000..030efa9
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSecurityManager.java
@@ -0,0 +1,140 @@
+/*
+ * 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.io.FileDescriptor;
+import java.net.InetAddress;
+import java.security.Permission;
+
+/*
+* A security manager for policing tests.
+* Note: some tests will override the security manager to use there own
+* TODO: Look at still using this in those tests
+* */
+public class TestSecurityManager extends SecurityManager {
+ @Override
+ public void checkExit(int status) {
+ throw new SystemExitRule.SystemExitInTestException();
+ }
+
+ @Override
+ public void checkPermission(Permission permission) {
+
+ }
+
+ @Override
+ public void checkPermission(Permission var1, Object var2) {
+
+ }
+
+ @Override
+ public void checkSecurityAccess(String var1) {
+
+ }
+
+ @Override
+ public void checkConnect(String var1, int var2, Object var3) {
+
+ }
+
+ @Override
+ public void checkWrite(String var1) {
+
+ }
+
+ @Override
+ public void checkDelete(String var1) {
+
+ }
+
+ @Override
+ public void checkConnect(String var1, int var2) {
+
+ }
+
+ @Override
+ public void checkLink(String var1) {
+
+ }
+
+ @Override
+ public void checkRead(FileDescriptor var1) {
+
+ }
+
+ @Override
+ public void checkAccess(Thread var1) {
+
+ }
+
+ @Override
+ public void checkAccess(ThreadGroup var1) {
+
+ }
+
+ @Override
+ public void checkCreateClassLoader() {
+
+ }
+
+ @Override
+ public void checkListen(int var1) {
+
+ }
+
+ @Override
+ public void checkAccept(String var1, int var2) {
+
+ }
+
+ @Override
+ public void checkMulticast(InetAddress var1) {
+
+ }
+
+ @Override
+ public void checkMulticast(InetAddress var1, byte var2) {
+
+ }
+
+ @Override
+ public void checkPropertiesAccess() {
+
+ }
+
+ @Override
+ public void checkPropertyAccess(String var1) {
+
+ }
+
+ @Override
+ public void checkPackageAccess(String var1) {
+
+ }
+
+ @Override
+ public void checkPackageDefinition(String var1) {
+
+ }
+
+ @Override
+ public void checkSetFactory() {
+
+ }
+
+}
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSystemExitInTest.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSystemExitInTest.java
new file mode 100644
index 0000000..803d46b
--- /dev/null
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestSystemExitInTest.java
@@ -0,0 +1,39 @@
+/**
+ * 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 org.apache.hadoop.hbase.testclassification.MiscTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category({MiscTests.class, SmallTests.class})
+public class TestSystemExitInTest {
+
+ @ClassRule
+ public static final HBaseClassTestRule CLASS_RULE =
+ HBaseClassTestRule.forClass(TestSystemExitInTest.class);
+
+
+ @Test(expected = SystemExitRule.SystemExitInTestException.class)
+ public void testSystemExit() {
+ System.exit(1);
+ }
+
+}