From 5fe802ea0a094289b6989748da75fb2942058aba Mon Sep 17 00:00:00 2001 From: Jakob Homan Date: Fri, 8 Oct 2010 19:21:42 +0000 Subject: [PATCH] HADOOP-6987. Use JUnit Rule to optionally fail test cases that run more than 10 seconds. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1005977 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 ++ .../hadoop/test/UnitTestcaseTimeLimit.java | 34 +++++++++++++++++++ .../apache/hadoop/util/TestStringUtils.java | 18 ++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/test/core/org/apache/hadoop/test/UnitTestcaseTimeLimit.java diff --git a/CHANGES.txt b/CHANGES.txt index 4a6782840ba..ef95bd7654b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -136,6 +136,9 @@ Trunk (unreleased changes) HADOOP-6856. Simplify constructors for SequenceFile, and MapFile. (omalley) + HADOOP-6987. Use JUnit Rule to optionally fail test cases that run more + than 10 seconds (jghoman) + OPTIMIZATIONS HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..). diff --git a/src/test/core/org/apache/hadoop/test/UnitTestcaseTimeLimit.java b/src/test/core/org/apache/hadoop/test/UnitTestcaseTimeLimit.java new file mode 100644 index 00000000000..5581c7d75da --- /dev/null +++ b/src/test/core/org/apache/hadoop/test/UnitTestcaseTimeLimit.java @@ -0,0 +1,34 @@ +/** + * 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.test; + +import org.junit.Rule; +import org.junit.rules.MethodRule; +import org.junit.rules.Timeout; + +/** + * Class for test units to extend in order that their individual tests will + * be timed out and fail automatically should they run more than 10 seconds. + * This provides an automatic regression check for tests that begin running + * longer than expected. + */ +public class UnitTestcaseTimeLimit { + public final int timeOutSecs = 10; + + @Rule public MethodRule globalTimeout = new Timeout(timeOutSecs * 1000); +} diff --git a/src/test/core/org/apache/hadoop/util/TestStringUtils.java b/src/test/core/org/apache/hadoop/util/TestStringUtils.java index 0c5479d12c2..7135a463de9 100644 --- a/src/test/core/org/apache/hadoop/util/TestStringUtils.java +++ b/src/test/core/org/apache/hadoop/util/TestStringUtils.java @@ -18,13 +18,17 @@ package org.apache.hadoop.util; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; -import static org.junit.Assert.assertArrayEquals; +import org.apache.hadoop.test.UnitTestcaseTimeLimit; +import org.junit.Test; -public class TestStringUtils extends TestCase { +public class TestStringUtils extends UnitTestcaseTimeLimit { final private static String NULL_STR = null; final private static String EMPTY_STR = ""; final private static String STR_WO_SPECIAL_CHARS = "AB"; @@ -36,6 +40,7 @@ public class TestStringUtils extends TestCase { final private static String ESCAPED_STR_WITH_BOTH2 = "\\,A\\\\\\,\\,B\\\\\\\\\\,"; + @Test public void testEscapeString() throws Exception { assertEquals(NULL_STR, StringUtils.escapeString(NULL_STR)); assertEquals(EMPTY_STR, StringUtils.escapeString(EMPTY_STR)); @@ -49,6 +54,7 @@ public class TestStringUtils extends TestCase { StringUtils.escapeString(STR_WITH_BOTH2)); } + @Test public void testSplit() throws Exception { assertEquals(NULL_STR, StringUtils.split(NULL_STR)); String[] splits = StringUtils.split(EMPTY_STR); @@ -78,6 +84,7 @@ public class TestStringUtils extends TestCase { assertEquals(ESCAPED_STR_WITH_BOTH2, splits[0]); } + @Test public void testSimpleSplit() throws Exception { final String[] TO_TEST = { "a/b/c", @@ -93,6 +100,7 @@ public class TestStringUtils extends TestCase { } } + @Test public void testUnescapeString() throws Exception { assertEquals(NULL_STR, StringUtils.unEscapeString(NULL_STR)); assertEquals(EMPTY_STR, StringUtils.unEscapeString(EMPTY_STR)); @@ -124,6 +132,7 @@ public class TestStringUtils extends TestCase { StringUtils.unEscapeString(ESCAPED_STR_WITH_BOTH2)); } + @Test public void testTraditionalBinaryPrefix() throws Exception { String[] symbol = {"k", "m", "g", "t", "p", "e"}; long m = 1024; @@ -138,6 +147,7 @@ public class TestStringUtils extends TestCase { assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g")); } + @Test public void testJoin() { List s = new ArrayList(); s.add("a"); @@ -149,6 +159,7 @@ public class TestStringUtils extends TestCase { assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3))); } + @Test public void testGetTrimmedStrings() throws Exception { String compactDirList = "/spindle1/hdfs,/spindle2/hdfs,/spindle3/hdfs"; String spacedDirList = "/spindle1/hdfs, /spindle2/hdfs, /spindle3/hdfs"; @@ -169,6 +180,7 @@ public class TestStringUtils extends TestCase { assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2)); } + @Test public void testCamelize() { // common use cases assertEquals("Map", StringUtils.camelize("MAP"));