8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected
authorsjiang
Wed, 07 Jan 2015 14:49:02 +0100
changeset 28300 bd5ad15efa76
parent 28299 7a7e7c08a9b5
child 28301 6a8523efd309
8068418: NotificationBufferDeadlockTest.java throw exception: java.lang.Exception: TEST FAILED: Deadlock detected Reviewed-by: dholmes
jdk/test/javax/management/remote/mandatory/notif/NotificationBufferDeadlockTest.java
--- a/jdk/test/javax/management/remote/mandatory/notif/NotificationBufferDeadlockTest.java	Wed Jan 07 03:59:06 2015 +0000
+++ b/jdk/test/javax/management/remote/mandatory/notif/NotificationBufferDeadlockTest.java	Wed Jan 07 14:49:02 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2015, 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
@@ -24,7 +24,8 @@
 /*
  * @test
  * @bug 6239400
- * @summary Tests NotificationBuffer doesn't hold locks when adding listeners.
+ * @summary Tests NotificationBuffer doesn't hold locks when adding listeners,
+ *  if test times out then deadlock is suspected.
  * @author Eamonn McManus
  * @run clean NotificationBufferDeadlockTest
  * @run build NotificationBufferDeadlockTest
@@ -38,6 +39,7 @@
 import java.util.List;
 import java.util.Set;
 import java.util.Vector;
+import java.util.concurrent.CountDownLatch;
 import javax.management.*;
 import javax.management.remote.*;
 
@@ -173,9 +175,7 @@
         for (ObjectName name : names)
             mbsc.invoke(name, "send", null, null);
 
-        if (!countListener.waiting(MAX_WAITING_TIME)) {
-            return "did not get " + names.size() + " notifs as expected\n";
-        }
+        countListener.waiting();
 
         if (!sources.containsAll(names))
             return "missing names: " + sources;
@@ -202,13 +202,13 @@
                 }
             };
             t.start();
+            System.out.println("DeadlockTest-addNotificationListener waiting for the sending thread to die...");
             try {
-                t.join(5000L);
+                t.join(); //if times out here then deadlock is suspected
+                System.out.println("DeadlockTest-addNotificationListener OK.");
             } catch (Exception e) {
                 thisFailure = "Join exception: " + e;
             }
-            if (t.isAlive())
-                thisFailure = "Deadlock detected";
         }
 
         public void send() {
@@ -244,9 +244,9 @@
                     }
                 };
                 t.start();
-                t.join(5000);
-                if (t.isAlive())
-                    failure = "Query deadlock detected";
+                System.out.println("CreateDuringQueryInvocationHandler-createMBeanIfQuery waiting for the creating thread to die...");
+                t.join();  // if times out here then deadlock is suspected
+                System.out.println("CreateDuringQueryInvocationHandler-createMBeanIfQuery OK");
             }
         }
 
@@ -264,50 +264,30 @@
 
     private static class MyListener implements NotificationListener {
         public MyListener(int waitNB) {
-            this.waitNB= waitNB;
+            count = new CountDownLatch(waitNB);
         }
 
         public void handleNotification(Notification n, Object h) {
-            System.out.println("MyListener got: "+n.getSource()+" "+n.getType());
+            System.out.println("MyListener got: " + n.getSource() + " " + n.getType());
 
-            synchronized(this) {
-                if (TESTING_TYPE.equals(n.getType())) {
-                    sources.add((ObjectName) n.getSource());
-
-                    if (sources.size() == waitNB) {
-                        this.notifyAll();
-                    }
-                }
+            if (TESTING_TYPE.equals(n.getType())) {
+                sources.add((ObjectName) n.getSource());
+                count.countDown();
             }
         }
 
-        public boolean waiting(long timeout) {
-            final long startTime = System.currentTimeMillis();
-            long toWait = timeout;
-
-            synchronized(this) {
-                while(sources.size() < waitNB && toWait > 0) {
-                    try {
-                        this.wait(toWait);
-                    } catch (InterruptedException ire) {
-                        break;
-                    }
-
-                    toWait = timeout -
-                        (System.currentTimeMillis() - startTime);
-                }
-            }
-
-            return sources.size() == waitNB;
+        public void waiting() throws InterruptedException {
+            System.out.println("MyListener-waiting ...");
+            count.await(); // if times out here then deadlock is suspected
+            System.out.println("MyListener-waiting done!");
         }
 
-        private final int waitNB;
+        private final CountDownLatch count;
     }
 
     static String thisFailure;
     static String failure;
     static int nextNameIndex;
-    static final long MAX_WAITING_TIME = 10000;
 
     private static MyListener countListener;
     private static final List<ObjectName> sources = new Vector();