test/sun/security/ssl/com/sun/net/ssl/internal/ssl/SSLEngineImpl/SSLEngineDeadlock.java

Print this page
8000456:  Add programmatic deadlock detection in SSLEngineDeadlock

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -19,19 +19,18 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
+// SunJSSE does not support dynamic system properties, no way to re-use
+// system properties in samevm/agentvm mode.
+
 /*
  * @test
  * @bug 6492872
  * @summary Deadlock in SSLEngine
  * @run main/othervm SSLEngineDeadlock
- *
- *     SunJSSE does not support dynamic system properties, no way to re-use
- *     system properties in samevm/agentvm mode.
- *
  * @author Brad R. Wetmore
  */
 
 /**
  * A SSLEngine usage example which simplifies the presentation

@@ -72,10 +71,11 @@
 import javax.net.ssl.*;
 import javax.net.ssl.SSLEngineResult.*;
 import java.io.*;
 import java.security.*;
 import java.nio.*;
+import java.lang.management.*;
 
 public class SSLEngineDeadlock {
 
     /*
      * Enables logging of the SSLEngine operations.

@@ -142,10 +142,12 @@
             if ((i % 5) == 0) {
                 System.out.println("Test #: " + i);
             }
             SSLEngineDeadlock test = new SSLEngineDeadlock();
             test.runTest();
+
+            detectDeadLock();
         }
         System.out.println("Test Passed.");
     }
 
     /*

@@ -358,10 +360,26 @@
         b.position(b.limit());
         a.limit(a.capacity());
         b.limit(b.capacity());
     }
 
+    /*
+     * Detect dead lock
+     */
+    private static void detectDeadLock() throws Exception {
+        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
+        long[] threadIds = threadBean.findDeadlockedThreads();
+        if (threadIds != null && threadIds.length != 0) {
+            for (long id : threadIds) {
+                ThreadInfo info =
+                    threadBean.getThreadInfo(id, Integer.MAX_VALUE);
+                System.out.println("Deadlocked ThreadInfo: " + info);
+            }
+            throw new Exception("Found Deadlock!");
+        }
+    }
+
     /*
      * Logging code
      */
     private static boolean resultOnce = true;