Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/classUnloadRequests/clsunlreq001.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.EventRequestManager.classUnloadRequests;2425import com.sun.jdi.VMDisconnectedException;26import com.sun.jdi.VirtualMachine;27import com.sun.jdi.request.ClassUnloadRequest;28import com.sun.jdi.request.EventRequestManager;29import com.sun.jdi.event.*;3031import java.util.List;32import java.io.*;3334import nsk.share.*;35import nsk.share.jpda.*;36import nsk.share.jdi.*;3738/**39* The test checks that the JDI method40* <code>com.sun.jdi.request.EventRequestManager.classUnloadRequests()</code>41* properly returns all previously created ClassUnloadRequest objects when:42* <li>event requests are disabled43* <li>event requests are enabled.<br>44* ClassUnloadRequest objects are distinguished by the different45* <code>EventRequest</code>'s properties.<br>46* EventHandler was added as workaround for the bug 4430096.47* This prevents the target VM from potential hangup.48*/49public class clsunlreq001 {50public static final int PASSED = 0;51public static final int FAILED = 2;52public static final int JCK_STATUS_BASE = 95;53static final String DEBUGGEE_CLASS =54"nsk.jdi.EventRequestManager.classUnloadRequests.clsunlreq001t";55static final String COMMAND_READY = "ready";56static final String COMMAND_QUIT = "quit";5758static final int CLSUNL_NUM = 7;59static final String PROPS[][] = {60{"first", "a quick"},61{"second", "brown"},62{"third", "fox"},63{"fourth", "jumps"},64{"fifth", "over"},65{"sixth", "the lazy"},66{"seventh", "dog"}67};6869private ArgumentHandler argHandler;70private Log log;71private IOPipe pipe;72private Debugee debuggee;73private VirtualMachine vm;74private EventListener elThread;75private volatile int tot_res = PASSED;7677public static void main (String argv[]) {78System.exit(run(argv,System.out) + JCK_STATUS_BASE);79}8081public static int run(String argv[], PrintStream out) {82return new clsunlreq001().runIt(argv, out);83}8485private int runIt(String args[], PrintStream out) {86argHandler = new ArgumentHandler(args);87log = new Log(out, argHandler);88Binder binder = new Binder(argHandler, log);89ClassUnloadRequest clunlRequest[] = new ClassUnloadRequest[CLSUNL_NUM];90String cmd;9192debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);93pipe = debuggee.createIOPipe();94debuggee.redirectStderr(log, "clsunlreq001t.err> ");95vm = debuggee.VM();96EventRequestManager erManager = vm.eventRequestManager();97debuggee.resume();98cmd = pipe.readln();99if (!cmd.equals(COMMAND_READY)) {100log.complain("TEST BUG: unknown debuggee's command: " + cmd);101tot_res = FAILED;102return quitDebuggee();103}104105for (int i=0; i<CLSUNL_NUM; i++) {106log.display("Creating ClassUnloadRequest #" + i107+ " with the property ("108+ PROPS[i][0] + "," + PROPS[i][1] + ")...");109clunlRequest[i] = erManager.createClassUnloadRequest();110clunlRequest[i].putProperty(PROPS[i][0], PROPS[i][1]);111}112elThread = new EventListener();113elThread.start();114115// Check ClassUnloadRequest requests when event requests are disabled116log.display("\n1) Getting ClassUnloadRequest objects with disabled event requests...");117checkRequests(erManager, 1);118119// Check ClassUnloadRequest requests when event requests are enabled120for (int i=0; i<CLSUNL_NUM; i++) {121clunlRequest[i].enable();122}123log.display("\n2) Getting ClassUnloadRequest objects with enabled event requests...");124checkRequests(erManager, 2);125126// Finish the test127for (int i=0; i<CLSUNL_NUM; i++) {128clunlRequest[i].disable();129}130return quitDebuggee();131}132133private void checkRequests(EventRequestManager erManager, int t_case) {134List reqL = erManager.classUnloadRequests();135if (reqL.size() != CLSUNL_NUM) {136log.complain("TEST CASE #" + t_case + " FAILED: found " + reqL.size()137+ " ClassUnload requests\n\texpected: " + CLSUNL_NUM);138tot_res = FAILED;139return;140}141for (int i=0; i<CLSUNL_NUM; i++) {142ClassUnloadRequest clunlReq =143(ClassUnloadRequest) reqL.get(i);144boolean notFound = true;145for (int j=0; j<CLSUNL_NUM; j++) {146String propKey = (String) clunlReq.getProperty(PROPS[j][0]);147if (propKey != null) {148if (propKey.equals(PROPS[j][1])) {149log.display("Found expected ClassUnload request with the property: ("150+ PROPS[j][0] + "," + propKey + ")");151} else {152log.complain("TEST CASE #" + t_case153+ " FAILED: found ClassUnload request with unexpected property: ("154+ PROPS[j][0] + "," + propKey + ")");155tot_res = FAILED;156}157notFound = false;158break;159}160}161if (notFound) {162log.complain("\nTEST CASE #" + t_case163+ " FAILED: found unexpected ClassUnload request");164tot_res = FAILED;165}166}167}168169private int quitDebuggee() {170if (elThread != null) {171elThread.isConnected = false;172try {173if (elThread.isAlive())174elThread.join();175} catch (InterruptedException e) {176log.complain("TEST INCOMPLETE: caught InterruptedException "177+ e);178tot_res = FAILED;179}180}181182pipe.println(COMMAND_QUIT);183184// Class containing a critical section which may lead to time out of the test185class criticalSection extends Thread {186public void run() {187debuggee.waitFor();188}189}190191criticalSection critSect = new criticalSection();192log.display("\nStarting potential timed out section: waiting "193+ argHandler.getWaitTime() + " minute(s) for debuggee's exit...");194critSect.start();195try {196critSect.join(argHandler.getWaitTime()*60000);197if (critSect.isAlive()) {198log.complain("TEST FAILED: Timeout occured while waiting for debuggee's exit");199return FAILED;200}201} catch (InterruptedException e) {202log.complain("TEST INCOMPLETE: InterruptedException caught while waiting for debuggee's exit");203return FAILED;204}205log.display("Potential timed out section passed\n");206207int debStat = debuggee.getStatus();208if (debStat != (JCK_STATUS_BASE + PASSED)) {209log.complain("TEST FAILED: debuggee's process finished with status: "210+ debStat);211tot_res = FAILED;212} else213log.display("Debuggee's process finished with status: "214+ debStat);215216return tot_res;217}218219class EventListener extends Thread {220public volatile boolean isConnected = true;221222public void run() {223try {224do {225EventSet eventSet = vm.eventQueue().remove(1000);226if (eventSet != null) { // there is not a timeout227EventIterator it = eventSet.eventIterator();228while (it.hasNext()) {229Event event = it.nextEvent();230if (event instanceof VMDeathEvent) {231tot_res = FAILED;232isConnected = false;233log.complain("TEST FAILED: unexpected VMDeathEvent");234} else if (event instanceof VMDisconnectEvent) {235tot_res = FAILED;236isConnected = false;237log.complain("TEST FAILED: unexpected VMDisconnectEvent");238} else239log.display("EventListener: following JDI event occured: "240+ event.toString());241}242if (isConnected) {243eventSet.resume();244}245}246} while (isConnected);247} catch (InterruptedException e) {248tot_res = FAILED;249log.complain("FAILURE in EventListener: caught unexpected "250+ e);251} catch (VMDisconnectedException e) {252tot_res = FAILED;253log.complain("FAILURE in EventListener: caught unexpected "254+ e);255e.printStackTrace();256}257log.display("EventListener: exiting");258}259}260}261262263