diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/referenceType/refType001.java b/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/referenceType/refType001.java index 70ad2461817..b5830bd421f 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/referenceType/refType001.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/referenceType/refType001.java @@ -28,6 +28,8 @@ import com.sun.jdi.event.*; import com.sun.jdi.request.*; import java.io.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.Iterator; import nsk.share.*; @@ -68,7 +70,8 @@ public class refType001 { {"AnotherThread", "Inter", "0"} }; - static private volatile boolean testFailed, eventsReceived, threadsStarted; + static private volatile boolean testFailed; + static private CountDownLatch eventsReceivedLatch; static private int eventTimeout; public static void main (String argv[]) { @@ -82,9 +85,7 @@ public class refType001 { String command; testFailed = false; - eventsReceived = false; - threadsStarted = false; - + eventsReceivedLatch = new CountDownLatch(1); argHandler = new ArgumentHandler(args); log = new Log(out, argHandler); @@ -114,8 +115,10 @@ public class refType001 { public void run() { - // handle events until all threads started and all expected events received - while (!(threadsStarted && eventsReceived)) { + boolean isConnected = true; + boolean allEventsReceived = false; + // handle events until debuggee is disconnected + while (isConnected) { EventSet eventSet = null; try { eventSet = vm.eventQueue().remove(TIMEOUT_DELTA); @@ -133,8 +136,10 @@ public class refType001 { Event event = eventIterator.nextEvent(); // log.display("\nEvent received:\n " + event); - // handle ClassPrepareEvent - if (event instanceof ClassPrepareEvent) { + if (event instanceof VMDeathEvent || event instanceof VMDisconnectEvent) { + log.display("eventHandler got " + event); + isConnected = false; + } else if (event instanceof ClassPrepareEvent) { ClassPrepareEvent castedEvent = (ClassPrepareEvent)event; log.display("\nClassPrepareEvent received:\n " + event); @@ -198,11 +203,20 @@ public class refType001 { } } - // Check that all expected ClassPrepareEvent are received - eventsReceived = true; - for (int i = 0; i < checkedTypes.length; i++) { - if (checkedTypes[i][2] == "0") - eventsReceived = false; + // Check that all expected ClassPrepareEvent(s) are received. + if (!allEventsReceived) { + allEventsReceived = true; + for (int i = 0; i < checkedTypes.length; i++) { + // checkedTypes[i][2] is "0" initially, + // "1" after corresponding ClassPrepareEvent is received. + if (checkedTypes[i][2] == "0") { + allEventsReceived = false; + break; + } + } + if (allEventsReceived) { + eventsReceivedLatch.countDown(); + } } } } @@ -219,7 +233,9 @@ public class refType001 { } // event handled // log.display("Resuming event set"); - eventSet.resume(); + if (isConnected) { + eventSet.resume(); + } } // event set handled @@ -260,17 +276,12 @@ public class refType001 { testFailed = true; } - // notify EventHandler that all threads started - threadsStarted = true; - // wait for all expected events received or timeout exceeds try { - eventHandler.join(eventTimeout); - if (eventHandler.isAlive()) { - log.complain("FAILURE 20: Timeout for waiting event was exceeded"); - eventHandler.interrupt(); - testFailed = true; - } + if (!eventsReceivedLatch.await(eventTimeout, TimeUnit.MILLISECONDS)) { + log.complain("FAILURE 20: Timeout waiting for all events was exceeded"); + testFailed = true; + } } catch (InterruptedException e) { log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death"); testFailed = true; diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/thread/thread001.java b/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/thread/thread001.java index 5d9057a55a9..1619f2acd86 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/thread/thread001.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/jdi/ClassPrepareEvent/thread/thread001.java @@ -28,6 +28,8 @@ import com.sun.jdi.event.*; import com.sun.jdi.request.*; import java.io.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.Iterator; import nsk.share.*; @@ -72,7 +74,8 @@ public class thread001 { static private int threadStatus; - static private volatile boolean testFailed, eventsReceived, threadsStarted; + static private volatile boolean testFailed; + static private CountDownLatch eventsReceivedLatch; static private int eventTimeout; public static void main (String argv[]) { @@ -86,8 +89,7 @@ public class thread001 { String command; testFailed = false; - eventsReceived = false; - threadsStarted = false; + eventsReceivedLatch = new CountDownLatch(1); argHandler = new ArgumentHandler(args); log = new Log(out, argHandler); @@ -134,8 +136,10 @@ public class thread001 { public void run() { - // handle events until all threads started and all expected events received - while (!(threadsStarted && eventsReceived)) { + boolean isConnected = true; + boolean allEventsReceived = false; + // handle events until debuggee is disconnected + while (isConnected) { EventSet eventSet = null; try { eventSet = vm.eventQueue().remove(TIMEOUT_DELTA); @@ -153,8 +157,10 @@ public class thread001 { Event event = eventIterator.nextEvent(); // log.display("\nEvent received:\n " + event); - // handle ClassPrepareEvent - if (event instanceof ClassPrepareEvent) { + if (event instanceof VMDeathEvent || event instanceof VMDisconnectEvent) { + log.display("eventHandler got " + event); + isConnected = false; + } else if (event instanceof ClassPrepareEvent) { ClassPrepareEvent castedEvent = (ClassPrepareEvent)event; log.display("\nClassPrepareEvent received:\n " + event); @@ -227,12 +233,20 @@ public class thread001 { } } - // Check that all expected ClassPrepareEvent are received - eventsReceived = true; - for (int i = 0; i < checkedThreads.length; i++) { - if (checkedThreads[i][2] == "0") { - eventsReceived = false; - } + // Check that all expected ClassPrepareEvent(s) are received. + if (!allEventsReceived) { + allEventsReceived = true; + for (int i = 0; i < checkedThreads.length; i++) { + // checkedTypes[i][2] is "0" initially, + // "1" after corresponding ClassPrepareEvent is received. + if (checkedThreads[i][2] == "0") { + allEventsReceived = false; + break; + } + } + if (allEventsReceived) { + eventsReceivedLatch.countDown(); + } } } } @@ -243,7 +257,9 @@ public class thread001 { } // event handled // log.display("Resuming event set"); - eventSet.resume(); + if (isConnected) { + eventSet.resume(); + } } // event set handled @@ -283,17 +299,12 @@ public class thread001 { testFailed = true; } - // notify EventHandler that all threads started - threadsStarted = true; - // wait for all expected events received or timeout exceeds try { - eventHandler.join(eventTimeout); - if (eventHandler.isAlive()) { - log.complain("FAILURE 20: Timeout for waiting event was exceeded"); - eventHandler.interrupt(); - testFailed = true; - } + if (!eventsReceivedLatch.await(eventTimeout, TimeUnit.MILLISECONDS)) { + log.complain("FAILURE 20: Timeout waiting for all events was exceeded"); + testFailed = true; + } } catch (InterruptedException e) { log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for eventHandler's death"); testFailed = true; diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadDeathRequests/thrdeathreq001.java b/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadDeathRequests/thrdeathreq001.java index dc35af74955..485d3506a73 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadDeathRequests/thrdeathreq001.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadDeathRequests/thrdeathreq001.java @@ -171,8 +171,8 @@ public class thrdeathreq001 { } private int quitDebuggee() { + pipe.println(COMMAND_QUIT); if (elThread != null) { - elThread.isConnected = false; try { if (elThread.isAlive()) elThread.join(); @@ -183,7 +183,6 @@ public class thrdeathreq001 { } } - pipe.println(COMMAND_QUIT); debuggee.waitFor(); int debStat = debuggee.getStatus(); if (debStat != (JCK_STATUS_BASE + PASSED)) { @@ -198,30 +197,24 @@ public class thrdeathreq001 { } class EventListener extends Thread { - public volatile boolean isConnected = true; public void run() { try { + boolean isConnected = true; do { EventSet eventSet = vm.eventQueue().remove(1000); if (eventSet != null) { // there is not a timeout EventIterator it = eventSet.eventIterator(); while (it.hasNext()) { Event event = it.nextEvent(); - if (event instanceof VMDeathEvent) { - tot_res = FAILED; + if (event instanceof VMDeathEvent || event instanceof VMDisconnectEvent) { + log.display("EventListener: got " + event); isConnected = false; - log.complain("TEST FAILED: unexpected VMDeathEvent"); - } else if (event instanceof VMDisconnectEvent) { - tot_res = FAILED; - isConnected = false; - log.complain("TEST FAILED: unexpected VMDisconnectEvent"); - } else + } else { log.display("EventListener: following JDI event occured: " + event.toString()); - } - if (isConnected) { - eventSet.resume(); + eventSet.resume(); + } } } } while (isConnected);