Linchen-Xu commented on code in PR #13358:
URL: https://github.com/apache/dubbo/pull/13358#discussion_r1393927365


##########
dubbo-common/src/test/java/org/apache/dubbo/common/utils/JVMUtilTest.java:
##########
@@ -16,4 +16,190 @@
  */
 package org.apache.dubbo.common.utils;
 
-class JVMUtilTest {}
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.management.LockInfo;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MonitorInfo;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static java.lang.Thread.State.BLOCKED;
+import static java.lang.Thread.State.TIMED_WAITING;
+import static java.lang.Thread.State.WAITING;
+import static 
org.apache.dubbo.common.constants.CommonConstants.DUBBO_JSTACK_MAXLINE;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
+import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_UNEXPECTED_CREATE_DUMP;
+import static org.apache.dubbo.common.utils.JVMUtil.jstack;
+
+class JVMUtilTest {
+
+    protected static final ErrorTypeAwareLogger logger = 
LoggerFactory.getErrorTypeAwareLogger(JVMUtilTest.class);
+
+    @Test
+    void testPrintStackTraceWithSpecifiedDepth() {
+        test(10);
+    }
+
+    @Test
+    void testPrintStackTraceWithUnlimitedDepth() {
+        test(-1);
+    }
+
+    private void test(Integer depth) {
+        // read the old property, then set new property
+        String oldProperty = System.getProperty(DUBBO_JSTACK_MAXLINE);
+        System.setProperty(DUBBO_JSTACK_MAXLINE, depth.toString());
+
+        SimpleDateFormat sdf;
+        String os = System.getProperty(OS_NAME_KEY).toLowerCase();
+        // window system don't support ":" in file name
+        if (os.contains(OS_WIN_PREFIX)) {
+            sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss .SSS");
+        } else {
+            sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss .SSS");
+        }
+        String filename = String.format("Dubbo_JStack.log.%s", sdf.format(new 
Date()));
+
+        try (FileOutputStream jStackStream = new FileOutputStream(new 
File("/tmp", filename))) {
+            // output new stack trace
+            jstack(jStackStream);
+        } catch (Exception e) {
+            logger.error(COMMON_UNEXPECTED_CREATE_DUMP, "", "", "dump jStack 
error", e);
+        }
+
+        // compare the depth between new trace and old trace
+        try {
+            byte[] encoded = 
Files.readAllBytes(Paths.get(String.format("/tmp/%s", filename)));
+            String newOutput = new String(encoded, StandardCharsets.UTF_8);
+            int newStackDepth = calculateStackDepth(newOutput);

Review Comment:
   I replaced it with `ByteArrayOutputStream` to get the result directly, 
there's no need to decode anymore.
   
   By the way, I tried dumping threads and using the same thread info to 
generate the stack trace info in both the old way and the new way. Therefore, 
we can compare the stack trace directly, which turns out to be working 
correctly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to