Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventQueue/remove/remove002.java
41161 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jdi.EventQueue.remove;2425import com.sun.jdi.VirtualMachine;26import com.sun.jdi.request.EventRequest;27import com.sun.jdi.VMDisconnectedException;28import com.sun.jdi.event.*;2930import java.io.*;3132import nsk.share.*;33import nsk.share.jpda.*;34import nsk.share.jdi.*;3536/**37* The test checks that a VMDisconnectedException thrown by38* the JDI method <b>com.sun.jdi.request.EventQueue.remove()</b>39* will be preceded by a <code>VMDisconnectEvent</code>40* after <code>com.sun.jdi.VirtualMachine.dispose()</code> call.41*/42public class remove002 {43public static final int PASSED = 0;44public static final int FAILED = 2;45public static final int JCK_STATUS_BASE = 95;46static final String DEBUGGEE_CLASS =47"nsk.jdi.EventQueue.remove.remove002t";48static final String COMMAND_READY = "ready";49static final String COMMAND_QUIT = "quit";5051private ArgumentHandler argHandler;52private Log log;53private IOPipe pipe;54private Debugee debuggee;55private CheckEvents chkEvents;56private EventQueue eventQ;57private volatile int tot_res = FAILED;5859public static void main(String argv[]) {60System.exit(run(argv,System.out) + JCK_STATUS_BASE);61}6263public static int run(String argv[], PrintStream out) {64return new remove002().runIt(argv, out);65}6667private int runIt(String args[], PrintStream out) {68argHandler = new ArgumentHandler(args);69log = new Log(out, argHandler);70Binder binder = new Binder(argHandler, log);7172debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);73debuggee.redirectStderr(log, "remove002t.err> ");74pipe = debuggee.createIOPipe();75VirtualMachine vm = debuggee.VM();7677eventQ = vm.eventQueue();78chkEvents = new CheckEvents();79chkEvents.setDaemon(true);80chkEvents.start();8182debuggee.resume();83String cmd = pipe.readln();84if (!cmd.equals(COMMAND_READY)) {85log.complain("TEST BUG: unknown debuggee's command: " + cmd);86tot_res = FAILED;87return quitDebuggee(FAILED);88}8990log.display("Invoking VirtualMachine.dispose() ...");91vm.dispose();9293return quitDebuggee(PASSED);94}9596class CheckEvents extends Thread {97public void run() {98boolean gotVMDisconnect = false; // VMDisconnectEvent is received99boolean gotVMDeath = false; // VMDeathEvent is received100EventSet eventSet = null;101102log.display("CheckEvents: starts JDI events processing");103while (true) {104try {105eventSet = eventQ.remove();106EventIterator eventIter = eventSet.eventIterator();107while (eventIter.hasNext()) {108Event event = eventIter.nextEvent();109if (event instanceof VMDisconnectEvent) {110gotVMDisconnect = true;111log.display("CheckEvents: got expected VMDisconnectEvent");112break;113} else if (event instanceof VMStartEvent) {114log.display("CheckEvents: got VMStartEvent");115} else if (event instanceof VMDeathEvent) {116gotVMDeath = true;117log.display("CheckEvents: got VMDeathEvent");118}119if (!gotVMDisconnect && !gotVMDeath &&120eventSet.suspendPolicy() !=121EventRequest.SUSPEND_NONE) {122log.display("CheckEvents: calling EventSet.resume() ...");123eventSet.resume();124}125}126} catch(InterruptedException e) {127log.complain("TEST INCOMPLETE: caught " + e);128tot_res = FAILED;129} catch(VMDisconnectedException e) {130if (gotVMDisconnect) {131log.display("\nCHECK PASSED: caught expected VMDisconnectedException preceded by a VMDisconnectEvent\n");132tot_res = PASSED;133} else {134log.complain("\nTEST FAILED: caught VMDisconnectedException without preceding VMDisconnectEvent\n");135e.printStackTrace();136tot_res = FAILED;137}138break;139}140}141log.display("CheckEvents: stopped JDI events processing");142}143}144145private int quitDebuggee(int stat) {146if (chkEvents != null) {147try {148if (chkEvents.isAlive())149chkEvents.join(argHandler.getWaitTime()*60000);150} catch (InterruptedException e) {151log.complain("TEST INCOMPLETE: caught InterruptedException "152+ e);153tot_res = FAILED;154}155if (chkEvents.isAlive()) {156if (stat == PASSED) {157log.complain("TEST FAILED: CheckEvents thread is still alive,\n"158+ "\tbut it should stop JDI events processing and exit");159chkEvents.interrupt();160tot_res = FAILED;161}162}163}164165pipe.println(COMMAND_QUIT);166debuggee.waitFor();167int debStat = debuggee.getStatus();168if (debStat != (JCK_STATUS_BASE + PASSED)) {169log.complain("TEST FAILED: debuggee's process finished with status: "170+ debStat);171tot_res = FAILED;172} else173log.display("Debuggee's process finished with status: "174+ debStat);175176return tot_res;177}178}179180181