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
This commit is contained in:
Jakob Homan 2010-10-08 19:21:42 +00:00
parent a503521a2c
commit 5fe802ea0a
3 changed files with 52 additions and 3 deletions

View File

@ -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(..).

View File

@ -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);
}

View File

@ -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 void testEscapeString() throws Exception {
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 void testSplit() throws Exception {
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 void testSimpleSplit() throws Exception {
}
}
@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 void testUnescapeString() throws Exception {
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 void testTraditionalBinaryPrefix() throws Exception {
assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g"));
}
@Test
public void testJoin() {
List<String> s = new ArrayList<String>();
s.add("a");
@ -149,6 +159,7 @@ public void testJoin() {
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 void testGetTrimmedStrings() throws Exception {
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2));
}
@Test
public void testCamelize() {
// common use cases
assertEquals("Map", StringUtils.camelize("MAP"));