Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadDeathRequests/thrdeathreq001.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.threadDeathRequests;2425import com.sun.jdi.VirtualMachine;26import com.sun.jdi.VMDisconnectedException;27import com.sun.jdi.request.ThreadDeathRequest;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.threadDeathRequests()</code>41* properly returns all ThreadDeathRequest objects when:42* <li>event requests are disabled43* <li>event requests are enabled.<br>44* ThreadDeathRequest objects are distinguished by the different45* <code>EventRequest</code>'s properties.<br>46* JDI EventHandler was added as workaround for the bug 4430096.47* This prevents the target VM from potential hangup.48*/49public class thrdeathreq001 {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.threadDeathRequests.thrdeathreq001t";55static final String COMMAND_READY = "ready";56static final String COMMAND_QUIT = "quit";5758static final int THR_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 Log log;70private IOPipe pipe;71private Debugee debuggee;72private VirtualMachine vm;73private EventListener elThread;74private volatile int tot_res = PASSED;7576public static void main (String argv[]) {77System.exit(run(argv,System.out) + JCK_STATUS_BASE);78}7980public static int run(String argv[], PrintStream out) {81return new thrdeathreq001().runIt(argv, out);82}8384private int runIt(String args[], PrintStream out) {85ArgumentHandler argHandler = new ArgumentHandler(args);86log = new Log(out, argHandler);87Binder binder = new Binder(argHandler, log);88ThreadDeathRequest thrdeathRequest[] =89new ThreadDeathRequest[THR_NUM];90String cmd;9192debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);93pipe = debuggee.createIOPipe();94debuggee.redirectStderr(log, "thrdeathreq001t.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<THR_NUM; i++) {106log.display("Creating ThreadDeathRequest #" + i107+ " with the property ("108+ PROPS[i][0] + "," + PROPS[i][1] + ")...");109thrdeathRequest[i] = erManager.createThreadDeathRequest();110thrdeathRequest[i].putProperty(PROPS[i][0], PROPS[i][1]);111}112elThread = new EventListener();113elThread.start();114115// Check ThreadDeath requests when event requests are disabled116log.display("\n1) Getting ThreadDeathRequest objects with disabled event requests...");117checkRequests(erManager, 1);118119// Check ThreadDeath requests when event requests are enabled120for (int i=0; i<THR_NUM; i++) {121thrdeathRequest[i].enable();122}123log.display("\n2) Getting ThreadDeathRequest objects with enabled event requests...");124checkRequests(erManager, 2);125126// Finish the test127for (int i=0; i<THR_NUM; i++) {128thrdeathRequest[i].disable();129}130return quitDebuggee();131}132133private void checkRequests(EventRequestManager erManager, int t_case) {134List reqL = erManager.threadDeathRequests();135if (reqL.size() != THR_NUM) {136log.complain("TEST CASE #" + t_case + " FAILED: found " + reqL.size()137+ " ThreadDeathRequest objects\n\texpected: " + THR_NUM);138tot_res = FAILED;139return;140}141for (int i=0; i<THR_NUM; i++) {142ThreadDeathRequest thrdReq =143(ThreadDeathRequest) reqL.get(i);144boolean notFound = true;145for (int j=0; j<THR_NUM; j++) {146String propKey = (String) thrdReq.getProperty(PROPS[j][0]);147if (propKey != null) {148if (propKey.equals(PROPS[j][1])) {149log.display("Found expected ThreadDeathRequest object with the property: ("150+ PROPS[j][0] + "," + propKey + ")");151} else {152log.complain("TEST CASE #" + t_case153+ " FAILED: found ThreadDeathRequest object with the 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 ThreadDeathRequest object");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);183debuggee.waitFor();184int debStat = debuggee.getStatus();185if (debStat != (JCK_STATUS_BASE + PASSED)) {186log.complain("TEST FAILED: debuggee's process finished with status: "187+ debStat);188tot_res = FAILED;189} else190log.display("Debuggee's process finished with status: "191+ debStat);192193return tot_res;194}195196class EventListener extends Thread {197public volatile boolean isConnected = true;198199public void run() {200try {201do {202EventSet eventSet = vm.eventQueue().remove(1000);203if (eventSet != null) { // there is not a timeout204EventIterator it = eventSet.eventIterator();205while (it.hasNext()) {206Event event = it.nextEvent();207if (event instanceof VMDeathEvent) {208tot_res = FAILED;209isConnected = false;210log.complain("TEST FAILED: unexpected VMDeathEvent");211} else if (event instanceof VMDisconnectEvent) {212tot_res = FAILED;213isConnected = false;214log.complain("TEST FAILED: unexpected VMDisconnectEvent");215} else216log.display("EventListener: following JDI event occured: "217+ event.toString());218}219if (isConnected) {220eventSet.resume();221}222}223} while (isConnected);224} catch (InterruptedException e) {225tot_res = FAILED;226log.complain("FAILURE in EventListener: caught unexpected "227+ e);228} catch (VMDisconnectedException e) {229tot_res = FAILED;230log.complain("FAILURE in EventListener: caught unexpected "231+ e);232e.printStackTrace();233}234log.display("EventListener: exiting");235}236}237}238239240