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  * @summary EventQueueDisconnectTest checks to see that
  30  * VMDisconnectedException is never thrown before VMDisconnectEvent.
  31  *
  32  * Failure mode for this test is throwing VMDisconnectedException
  33  * on vm.eventQueue().remove();
  34  * Does not use a scaffold since we don't want that hiding the exception.
  35  *
  36  * @run build VMConnection
  37  * @run compile -g EventQueueDisconnectTest.java
  38  * @run driver EventQueueDisconnectTest
  39  */
  40 import com.sun.jdi.*;
  41 import com.sun.jdi.event.*;
  42 import com.sun.jdi.request.*;
  43 
  44 
  45     /********** target program **********/
  46 
  47 class EventQueueDisconnectTarg {
  48     public static void main(String args[]) {
  49         for (int i=0; i < 10; ++i) {
  50             Say(i);
  51         }
  52     }
  53     static void Say(int what) {
  54         System.out.println("Say " + what);
  55     }
  56 }
  57 
  58     /********** test program **********/
  59 
  60 public class EventQueueDisconnectTest {
  61 
  62     public static void main(String args[]) throws Exception {
  63         VMConnection connection = new VMConnection(
  64                                        "com.sun.jdi.CommandLineLaunch:",
  65                                        VirtualMachine.TRACE_NONE);
  66         connection.setConnectorArg("main", "EventQueueDisconnectTarg");
  67         String debuggeeVMOptions = VMConnection.getDebuggeeVMOptions();
  68         if (!debuggeeVMOptions.equals("")) {
  69             if (connection.connectorArg("options").length() > 0) {
  70                 throw new IllegalArgumentException("VM options in two places");
  71             }
  72             connection.setConnectorArg("options", debuggeeVMOptions);
  73         }
  74         VirtualMachine vm = connection.open();
  75         EventRequestManager requestManager = vm.eventRequestManager();
  76         MethodEntryRequest req = requestManager.createMethodEntryRequest();
  77         req.addClassFilter("EventQueueDisconnectTarg");
  78         req.setSuspendPolicy(EventRequest.SUSPEND_NONE);
  79         req.enable();
  80 
  81         // We need to have the BE stop when VMDeath comes
  82         VMDeathRequest ourVMDeathRequest = requestManager.createVMDeathRequest();
  83         ourVMDeathRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
  84         ourVMDeathRequest.enable();
  85 
  86         vm.resume();
  87         while (true) {
  88             EventSet set = vm.eventQueue().remove();
  89             Event event = set.eventIterator().nextEvent();
  90 
  91             System.err.println("EventSet with: " + event.getClass());
  92 
  93             if (event instanceof VMDisconnectEvent) {
  94                 System.err.println("Disconnecting successfully");
  95                 break;
  96             }
  97 
  98             if (event instanceof VMDeathEvent) {
  99                 System.err.println("Pausing after VM death");
 100 
 101                 // sleep a few seconds
 102                 try {
 103                     Thread.sleep(40 * 1000);
 104                 } catch (InterruptedException exc) {
 105                     // ignore
 106                 }
 107             }
 108 
 109             set.resume();
 110         }
 111 
 112         System.err.println("EventQueueDisconnectTest passed");
 113     }
 114 }