This is an automated email from the ASF dual-hosted git repository.

zhangduo pushed a commit to branch branch-2.6
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.6 by this push:
     new c2e27df83d2 HBASE-29636 Implement TimedOutTestsListener for junit 5 
(#7352)
c2e27df83d2 is described below

commit c2e27df83d232f1a89c49c717670e8d07a3e4619
Author: Duo Zhang <[email protected]>
AuthorDate: Tue Oct 7 17:19:32 2025 +0800

    HBASE-29636 Implement TimedOutTestsListener for junit 5 (#7352)
    
    Signed-off by: Chandra Sekhar K <[email protected]>
    
    (cherry picked from commit 31520c7de69c252b5de78de58355159d596e75ac)
---
 .../apache/hadoop/hbase/HBaseJupiterExtension.java |  7 +++-
 .../hbase/TestBuildThreadDiagnosticString.java     | 39 ++++++++++++++++++++++
 .../apache/hadoop/hbase/TimedOutTestsListener.java | 19 ++++++-----
 3 files changed, 55 insertions(+), 10 deletions(-)

diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseJupiterExtension.java 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseJupiterExtension.java
index ff2ad14fe76..997e3dfa357 100644
--- 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseJupiterExtension.java
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseJupiterExtension.java
@@ -162,12 +162,17 @@ public class HBaseJupiterExtension
     } catch (ExecutionException e) {
       throw ExceptionUtils.throwAsUncheckedException(e.getCause());
     } catch (TimeoutException e) {
-
+      printThreadDump();
       throw new JUnitException(
         "Test " + ctx.getDisplayName() + " timed out, deadline is " + 
deadline, e);
     }
   }
 
+  private void printThreadDump() {
+    LOG.info("====> TEST TIMED OUT. PRINTING THREAD DUMP. <====");
+    LOG.info(TimedOutTestsListener.buildThreadDiagnosticString());
+  }
+
   @Override
   public void interceptBeforeAllMethod(Invocation<Void> invocation,
     ReflectiveInvocationContext<Method> invocationContext, ExtensionContext 
extensionContext)
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestBuildThreadDiagnosticString.java
 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestBuildThreadDiagnosticString.java
new file mode 100644
index 00000000000..4071f18e2dd
--- /dev/null
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestBuildThreadDiagnosticString.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 static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+
+import org.apache.hadoop.hbase.testclassification.MiscTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag(MiscTests.TAG)
+@Tag(SmallTests.TAG)
+public class TestBuildThreadDiagnosticString {
+
+  @Test
+  public void test() {
+    String threadDump = TimedOutTestsListener.buildThreadDiagnosticString();
+    System.out.println(threadDump);
+    assertThat(threadDump,
+      containsString(getClass().getName() + ".test(" + 
getClass().getSimpleName() + ".java:"));
+  }
+}
diff --git 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java
index 00860d0dde5..253d1735977 100644
--- 
a/hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java
+++ 
b/hbase-common/src/test/java/org/apache/hadoop/hbase/TimedOutTestsListener.java
@@ -26,9 +26,9 @@ import java.lang.management.MonitorInfo;
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import java.nio.charset.StandardCharsets;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.Locale;
 import java.util.Map;
 import org.junit.runner.notification.Failure;
@@ -40,7 +40,10 @@ import org.junit.runner.notification.RunListener;
  */
 public class TimedOutTestsListener extends RunListener {
 
-  static final String TEST_TIMED_OUT_PREFIX = "test timed out after";
+  private static final String TEST_TIMED_OUT_PREFIX = "test timed out after";
+
+  private static final DateTimeFormatter TIMESTAMP_FORMATTER =
+    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS 
Z").withZone(ZoneId.systemDefault());
 
   private static String INDENT = "    ";
 
@@ -67,13 +70,11 @@ public class TimedOutTestsListener extends RunListener {
     output.flush();
   }
 
-  @SuppressWarnings("JavaUtilDate")
   public static String buildThreadDiagnosticString() {
     StringWriter sw = new StringWriter();
     PrintWriter output = new PrintWriter(sw);
 
-    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
-    output.println(String.format("Timestamp: %s", dateFormat.format(new 
Date())));
+    output.println(String.format("Timestamp: %s", 
TIMESTAMP_FORMATTER.format(Instant.now())));
     output.println();
     output.println(buildThreadDump());
 
@@ -87,7 +88,7 @@ public class TimedOutTestsListener extends RunListener {
     return sw.toString();
   }
 
-  static String buildThreadDump() {
+  private static String buildThreadDump() {
     StringBuilder dump = new StringBuilder();
     Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
     for (Map.Entry<Thread, StackTraceElement[]> e : stackTraces.entrySet()) {
@@ -109,7 +110,7 @@ public class TimedOutTestsListener extends RunListener {
     return dump.toString();
   }
 
-  static String buildDeadlockInfo() {
+  private static String buildDeadlockInfo() {
     ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
     long[] threadIds = threadBean.findMonitorDeadlockedThreads();
     if (threadIds != null && threadIds.length > 0) {

Reply via email to