6263966: TEST: com/sun/jdi/ClassesByName2Test.java has a race
authorjjh
Mon, 22 Sep 2008 19:20:08 -0700
changeset 1244 f58e00230694
parent 1243 f5d91fb6df32
child 1245 b23505b07d80
child 1321 36e6632d730b
6263966: TEST: com/sun/jdi/ClassesByName2Test.java has a race Summary: Have the debuggee stop at a bkpt instead of running to completion. Reviewed-by: tbell
jdk/test/com/sun/jdi/ClassesByName2Test.java
--- a/jdk/test/com/sun/jdi/ClassesByName2Test.java	Mon Sep 22 15:43:12 2008 +0200
+++ b/jdk/test/com/sun/jdi/ClassesByName2Test.java	Mon Sep 22 19:20:08 2008 -0700
@@ -41,8 +41,7 @@
     /********** target program **********/
 
 class ClassesByName2Targ {
-    public static void ready() {
-        System.out.println("Ready!");
+    static void bkpt() {
     }
 
     public static void main(String[] args){
@@ -74,22 +73,24 @@
                     }
                 };
 
-            ready();
-
             two.start();
             one.start();
             zero.start();
 
             try {
                 zero.join();
+                System.out.println("zero joined");
                 one.join();
+                System.out.println("one joined");
                 two.join();
+                System.out.println("two joined");
             } catch (InterruptedException iex) {
                 iex.printStackTrace();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
+        bkpt();
         System.out.println("Goodbye from ClassesByName2Targ!");
     }
 }
@@ -97,29 +98,64 @@
     /********** test program **********/
 
 public class ClassesByName2Test extends TestScaffold {
+    volatile boolean stop = false;
 
     ClassesByName2Test (String args[]) {
         super(args);
     }
 
+    public void breakpointReached(BreakpointEvent event) {
+        System.out.println("Got BreakpointEvent: " + event);
+        stop = true;
+    }
+
+    public void eventSetComplete(EventSet set) {
+        // Don't resume.
+    }
+
     public static void main(String[] args)      throws Exception {
         new ClassesByName2Test(args).startTests();
     }
 
+    void breakpointAtMethod(ReferenceType ref, String methodName)
+                                           throws Exception {
+        List meths = ref.methodsByName(methodName);
+        if (meths.size() != 1) {
+            throw new Exception("test error: should be one " +
+                                methodName);
+        }
+        Method meth = (Method)meths.get(0);
+        BreakpointRequest bkptReq = vm().eventRequestManager().
+            createBreakpointRequest(meth.location());
+        bkptReq.enable();
+        try {
+            addListener (this);
+        } catch (Exception ex){
+            ex.printStackTrace();
+            failure("failure: Could not add listener");
+            throw new Exception("ClassesByname2Test: failed");
+        }
+    }
+
     protected void runTests() throws Exception {
+        BreakpointEvent bpe = startToMain("ClassesByName2Targ");
+
         /*
-         * Get to the top of ready()
-         */
-        startTo("ClassesByName2Targ", "ready", "()V");
-
+          Bug 6263966 - Don't just resume because the debuggee can
+          complete and disconnect while the following loop is
+          accessing it.
+        */
+        breakpointAtMethod(bpe.location().declaringType(), "bkpt");
         vm().resume();
 
-        int i = 0;
-        while (i < 8 && !vmDisconnected) {
-            i++;
+        /* The test of 'stop' is so that we stop when the debuggee hits
+           the bkpt.  The 150 is so we stop if the debuggee
+           is slow (eg, -Xcomp -server) - we don't want to
+           spend all day waiting for it to get to the bkpt.
+        */
+        for (int i = 0; i < 150 && !stop; i++) {
             List all = vm().allClasses();
-            System.out.println("");
-            System.out.println("++++ Lookup number: " + i + ".  allClasses() returned " +
+            System.out.println("\n++++ Lookup number: " + i + ".  allClasses() returned " +
                                all.size() + " classes.");
             for (Iterator it = all.iterator(); it.hasNext(); ) {
                 ReferenceType cls = (ReferenceType)it.next();
@@ -135,9 +171,8 @@
             }
         }
 
-
-        // Doing vm().exit(0) instead of listenUntilVMDisconnect()
-        // speeds up the test up by more than 50% in -server -Xcomp (solsparc32-fastdebug)
+        // In case of a slow debuggee, we don't want to resume the debuggee and wait
+        // for it to complete.
         vm().exit(0);
 
         /*