Skip to content

Commit 31520c7

Browse files
committed
HBASE-29636 Implement TimedOutTestsListener for junit 5 (#7352)
Signed-off by: Chandra Sekhar K <[email protected]>
1 parent a637774 commit 31520c7

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseJupiterExtension.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,17 @@ private <T> T runWithTimeout(Invocation<T> invocation, ExtensionContext ctx) thr
162162
} catch (ExecutionException e) {
163163
throw ExceptionUtils.throwAsUncheckedException(e.getCause());
164164
} catch (TimeoutException e) {
165-
165+
printThreadDump();
166166
throw new JUnitException(
167167
"Test " + ctx.getDisplayName() + " timed out, deadline is " + deadline, e);
168168
}
169169
}
170170

171+
private void printThreadDump() {
172+
LOG.info("====> TEST TIMED OUT. PRINTING THREAD DUMP. <====");
173+
LOG.info(TimedOutTestsListener.buildThreadDiagnosticString());
174+
}
175+
171176
@Override
172177
public void interceptBeforeAllMethod(Invocation<Void> invocation,
173178
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.hbase;
19+
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.containsString;
22+
23+
import org.apache.hadoop.hbase.testclassification.MiscTests;
24+
import org.apache.hadoop.hbase.testclassification.SmallTests;
25+
import org.junit.jupiter.api.Tag;
26+
import org.junit.jupiter.api.Test;
27+
28+
@Tag(MiscTests.TAG)
29+
@Tag(SmallTests.TAG)
30+
public class TestBuildThreadDiagnosticString {
31+
32+
@Test
33+
public void test() {
34+
String threadDump = TimedOutTestsListener.buildThreadDiagnosticString();
35+
System.out.println(threadDump);
36+
assertThat(threadDump,
37+
containsString(getClass().getName() + ".test(" + getClass().getSimpleName() + ".java:"));
38+
}
39+
}

hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import java.lang.management.ThreadInfo;
2727
import java.lang.management.ThreadMXBean;
2828
import java.nio.charset.StandardCharsets;
29-
import java.text.DateFormat;
30-
import java.text.SimpleDateFormat;
31-
import java.util.Date;
29+
import java.time.Instant;
30+
import java.time.ZoneId;
31+
import java.time.format.DateTimeFormatter;
3232
import java.util.Locale;
3333
import java.util.Map;
3434
import org.junit.runner.notification.Failure;
@@ -40,7 +40,10 @@
4040
*/
4141
public class TimedOutTestsListener extends RunListener {
4242

43-
static final String TEST_TIMED_OUT_PREFIX = "test timed out after";
43+
private static final String TEST_TIMED_OUT_PREFIX = "test timed out after";
44+
45+
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
46+
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS Z").withZone(ZoneId.systemDefault());
4447

4548
private static String INDENT = " ";
4649

@@ -67,13 +70,11 @@ public void testFailure(Failure failure) throws Exception {
6770
output.flush();
6871
}
6972

70-
@SuppressWarnings("JavaUtilDate")
7173
public static String buildThreadDiagnosticString() {
7274
StringWriter sw = new StringWriter();
7375
PrintWriter output = new PrintWriter(sw);
7476

75-
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
76-
output.println(String.format("Timestamp: %s", dateFormat.format(new Date())));
77+
output.println(String.format("Timestamp: %s", TIMESTAMP_FORMATTER.format(Instant.now())));
7778
output.println();
7879
output.println(buildThreadDump());
7980

@@ -87,7 +88,7 @@ public static String buildThreadDiagnosticString() {
8788
return sw.toString();
8889
}
8990

90-
static String buildThreadDump() {
91+
private static String buildThreadDump() {
9192
StringBuilder dump = new StringBuilder();
9293
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
9394
for (Map.Entry<Thread, StackTraceElement[]> e : stackTraces.entrySet()) {
@@ -109,7 +110,7 @@ static String buildThreadDump() {
109110
return dump.toString();
110111
}
111112

112-
static String buildDeadlockInfo() {
113+
private static String buildDeadlockInfo() {
113114
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
114115
long[] threadIds = threadBean.findMonitorDeadlockedThreads();
115116
if (threadIds != null && threadIds.length > 0) {

0 commit comments

Comments
 (0)