6952105: TEST_BUG: testcase failure, not very often, com/sun/jdi/SuspendThreadTest.java
authorsla
Fri, 21 Feb 2014 16:26:22 +0100
changeset 23005 22b4b386a639
parent 23004 09124a80451a
child 23006 7cb4567eb213
child 23018 2de83a4d6b0c
6952105: TEST_BUG: testcase failure, not very often, com/sun/jdi/SuspendThreadTest.java Reviewed-by: dholmes, sspitsyn, dsamersoff
jdk/test/com/sun/jdi/SuspendThreadTest.java
--- a/jdk/test/com/sun/jdi/SuspendThreadTest.java	Fri Feb 21 14:01:57 2014 +0100
+++ b/jdk/test/com/sun/jdi/SuspendThreadTest.java	Fri Feb 21 16:26:22 2014 +0100
@@ -36,7 +36,6 @@
 import com.sun.jdi.event.*;
 import com.sun.jdi.request.*;
 
-import java.util.*;
 
     /********** target program **********/
 
@@ -78,7 +77,11 @@
 
     // 1000 makes the test take over 2 mins on win32
     static int maxBkpts = 200;
-    int bkptCount;
+    volatile int bkptCount;
+    // to guard against spurious wakeups from bkptSignal.wait()
+    boolean signalSent;
+    // signal that a breakpoint has happened
+    Object bkptSignal = new Object() {};
     BreakpointRequest bkptRequest;
     Field debuggeeCountField;
 
@@ -99,8 +102,12 @@
         // The main thread watchs the bkptCount to
         // see if bkpts stop coming in.  The
         // test _should_ fail well before maxBkpts bkpts.
-        if (bkptCount++ < maxBkpts) {
-            bkptRequest.enable();
+        synchronized (bkptSignal) {
+            if (bkptCount++ < maxBkpts) {
+                bkptRequest.enable();
+            }
+            signalSent = true;
+            bkptSignal.notifyAll();
         }
     }
 
@@ -130,29 +137,32 @@
 
         debuggeeCountField = targetClass.fieldByName("count");
         try {
-
             addListener (this);
         } catch (Exception ex){
             ex.printStackTrace();
             failure("failure: Could not add listener");
-            throw new Exception("SuspendThreadTest: failed");
+            throw new Exception("SuspendThreadTest: failed", ex);
         }
 
         int prevBkptCount;
         vm().resume();
-        while (bkptCount < maxBkpts) {
-            prevBkptCount = bkptCount;
-            // If we don't get a bkpt within 5 secs,
-            // the test fails
-            try {
-                Thread.sleep(5000);
-            } catch (InterruptedException ee) {
+        synchronized (bkptSignal) {
+            while (bkptCount < maxBkpts) {
+                prevBkptCount = bkptCount;
+                // If we don't get a bkpt within 5 secs,
+                // the test fails
+                signalSent = false;
+                do {
+                    try {
+                        bkptSignal.wait(5000);
+                    } catch (InterruptedException ee) {
+                    }
+                } while (signalSent == false);
+                if (prevBkptCount == bkptCount) {
+                    failure("failure: test hung");
+                    break;
+                }
             }
-            if (prevBkptCount == bkptCount) {
-                failure("failure: test hung");
-                break;
-            }
-            prevBkptCount = bkptCount;
         }
         println("done with loop");
         bkptRequest.disable();