1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @bug 4425852
  27  *  @author Robert Field
  28  *
  29  *  @modules jdk.jdi
  30  *  @run build VMConnection
  31  *  @run compile -g EventQueueDisconnectTest.java
  32  *  @run driver EventQueueDisconnectTest
  33  *
  34  *  @summary EventQueueDisconnectTest checks to see that
  35  *  VMDisconnectedException is never thrown before VMDisconnectEvent.
  36  *
  37  * Failure mode for this test is throwing VMDisconnectedException
  38  * on vm.eventQueue().remove();
  39  * Does not use a scaffold since we don't want that hiding the exception.
  40  */
  41 import com.sun.jdi.*;
  42 import com.sun.jdi.event.*;
  43 import com.sun.jdi.request.*;
  44 
  45 
  46     /********** target program **********/
  47 
  48 class EventQueueDisconnectTarg {
  49     public static void main(String args[]) {
  50         for (int i=0; i < 10; ++i) {
  51             Say(i);
  52         }
  53     }
  54     static void Say(int what) {
  55         System.out.println("Say " + what);
  56     }
  57 }
  58 
  59     /********** test program **********/
  60 
  61 public class EventQueueDisconnectTest {
  62 
  63     public static void main(String args[]) throws Exception {
  64         VMConnection connection = new VMConnection(
  65                                        "com.sun.jdi.CommandLineLaunch:",
  66                                        VirtualMachine.TRACE_NONE);
  67         connection.setConnectorArg("main", "EventQueueDisconnectTarg");
  68         String debuggeeVMOptions = VMConnection.getDebuggeeVMOptions();
  69         if (!debuggeeVMOptions.equals("")) {
  70             if (connection.connectorArg("options").length() > 0) {
  71                 throw new IllegalArgumentException("VM options in two places");
  72             }
  73             connection.setConnectorArg("options", debuggeeVMOptions);
  74         }
  75         VirtualMachine vm = connection.open();
  76         EventRequestManager requestManager = vm.eventRequestManager();
  77         MethodEntryRequest req = requestManager.createMethodEntryRequest();
  78         req.addClassFilter("EventQueueDisconnectTarg");
  79         req.setSuspendPolicy(EventRequest.SUSPEND_NONE);
  80         req.enable();
  81 
  82         // We need to have the BE stop when VMDeath comes
  83         VMDeathRequest ourVMDeathRequest = requestManager.createVMDeathRequest();
  84         ourVMDeathRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
  85         ourVMDeathRequest.enable();
  86 
  87         vm.resume();
  88         while (true) {
  89             EventSet set = vm.eventQueue().remove();
  90             Event event = set.eventIterator().nextEvent();
  91 
  92             System.err.println("EventSet with: " + event.getClass());
  93 
  94             if (event instanceof VMDisconnectEvent) {
  95                 System.err.println("Disconnecting successfully");
  96                 break;
  97             }
  98 
  99             if (event instanceof VMDeathEvent) {
 100                 System.err.println("Pausing after VM death");
 101 
 102                 // sleep a few seconds
 103                 try {
 104                     Thread.sleep(40 * 1000);
 105                 } catch (InterruptedException exc) {
 106                     // ignore
 107                 }
 108             }
 109 
 110             set.resume();
 111         }
 112 
 113         System.err.println("EventQueueDisconnectTest passed");
 114     }
 115 }