Skip to content

Commit becb848

Browse files
committed
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
1 parent b59a141 commit becb848

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ Trunk (unreleased changes)
136136

137137
HADOOP-6856. Simplify constructors for SequenceFile, and MapFile. (omalley)
138138

139+
HADOOP-6987. Use JUnit Rule to optionally fail test cases that run more
140+
than 10 seconds (jghoman)
141+
139142
OPTIMIZATIONS
140143

141144
HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.test;
19+
20+
import org.junit.Rule;
21+
import org.junit.rules.MethodRule;
22+
import org.junit.rules.Timeout;
23+
24+
/**
25+
* Class for test units to extend in order that their individual tests will
26+
* be timed out and fail automatically should they run more than 10 seconds.
27+
* This provides an automatic regression check for tests that begin running
28+
* longer than expected.
29+
*/
30+
public class UnitTestcaseTimeLimit {
31+
public final int timeOutSecs = 10;
32+
33+
@Rule public MethodRule globalTimeout = new Timeout(timeOutSecs * 1000);
34+
}

src/test/core/org/apache/hadoop/util/TestStringUtils.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
package org.apache.hadoop.util;
2020

21+
import static org.junit.Assert.assertArrayEquals;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.fail;
24+
2125
import java.util.ArrayList;
2226
import java.util.List;
2327

24-
import junit.framework.TestCase;
25-
import static org.junit.Assert.assertArrayEquals;
28+
import org.apache.hadoop.test.UnitTestcaseTimeLimit;
29+
import org.junit.Test;
2630

27-
public class TestStringUtils extends TestCase {
31+
public class TestStringUtils extends UnitTestcaseTimeLimit {
2832
final private static String NULL_STR = null;
2933
final private static String EMPTY_STR = "";
3034
final private static String STR_WO_SPECIAL_CHARS = "AB";
@@ -36,6 +40,7 @@ public class TestStringUtils extends TestCase {
3640
final private static String ESCAPED_STR_WITH_BOTH2 =
3741
"\\,A\\\\\\,\\,B\\\\\\\\\\,";
3842

43+
@Test
3944
public void testEscapeString() throws Exception {
4045
assertEquals(NULL_STR, StringUtils.escapeString(NULL_STR));
4146
assertEquals(EMPTY_STR, StringUtils.escapeString(EMPTY_STR));
@@ -49,6 +54,7 @@ public void testEscapeString() throws Exception {
4954
StringUtils.escapeString(STR_WITH_BOTH2));
5055
}
5156

57+
@Test
5258
public void testSplit() throws Exception {
5359
assertEquals(NULL_STR, StringUtils.split(NULL_STR));
5460
String[] splits = StringUtils.split(EMPTY_STR);
@@ -78,6 +84,7 @@ public void testSplit() throws Exception {
7884
assertEquals(ESCAPED_STR_WITH_BOTH2, splits[0]);
7985
}
8086

87+
@Test
8188
public void testSimpleSplit() throws Exception {
8289
final String[] TO_TEST = {
8390
"a/b/c",
@@ -93,6 +100,7 @@ public void testSimpleSplit() throws Exception {
93100
}
94101
}
95102

103+
@Test
96104
public void testUnescapeString() throws Exception {
97105
assertEquals(NULL_STR, StringUtils.unEscapeString(NULL_STR));
98106
assertEquals(EMPTY_STR, StringUtils.unEscapeString(EMPTY_STR));
@@ -124,6 +132,7 @@ public void testUnescapeString() throws Exception {
124132
StringUtils.unEscapeString(ESCAPED_STR_WITH_BOTH2));
125133
}
126134

135+
@Test
127136
public void testTraditionalBinaryPrefix() throws Exception {
128137
String[] symbol = {"k", "m", "g", "t", "p", "e"};
129138
long m = 1024;
@@ -138,6 +147,7 @@ public void testTraditionalBinaryPrefix() throws Exception {
138147
assertEquals(956703965184L, StringUtils.TraditionalBinaryPrefix.string2long("891g"));
139148
}
140149

150+
@Test
141151
public void testJoin() {
142152
List<String> s = new ArrayList<String>();
143153
s.add("a");
@@ -149,6 +159,7 @@ public void testJoin() {
149159
assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3)));
150160
}
151161

162+
@Test
152163
public void testGetTrimmedStrings() throws Exception {
153164
String compactDirList = "/spindle1/hdfs,/spindle2/hdfs,/spindle3/hdfs";
154165
String spacedDirList = "/spindle1/hdfs, /spindle2/hdfs, /spindle3/hdfs";
@@ -169,6 +180,7 @@ public void testGetTrimmedStrings() throws Exception {
169180
assertArrayEquals(emptyArray, StringUtils.getTrimmedStrings(emptyList2));
170181
}
171182

183+
@Test
172184
public void testCamelize() {
173185
// common use cases
174186
assertEquals("Map", StringUtils.camelize("MAP"));

0 commit comments

Comments
 (0)