Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventRequestManager/threadStartRequests/thrstartreq001.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.threadStartRequests;2425import com.sun.jdi.VirtualMachine;26import com.sun.jdi.VMDisconnectedException;27import com.sun.jdi.request.ThreadStartRequest;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.threadStartRequests()</code>41* properly returns all ThreadStartRequest objects when:42* <li>event requests are disabled43* <li>event requests are enabled.<br>44* ThreadStartRequest 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 thrstartreq001 {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.threadStartRequests.thrstartreq001t";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 thrstartreq001().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);88ThreadStartRequest thrstartRequest[] = new ThreadStartRequest[THR_NUM];89String cmd;9091debuggee = binder.bindToDebugee(DEBUGGEE_CLASS);92pipe = debuggee.createIOPipe();93debuggee.redirectStderr(log, "thrstartreq001t.err> ");94vm = debuggee.VM();95EventRequestManager erManager = vm.eventRequestManager();96debuggee.resume();97cmd = pipe.readln();98if (!cmd.equals(COMMAND_READY)) {99log.complain("TEST BUG: unknown debuggee's command: " + cmd);100tot_res = FAILED;101return quitDebuggee();102}103104for (int i=0; i<THR_NUM; i++) {105log.display("Creating ThreadStartRequest #" + i106+ " with the property ("107+ PROPS[i][0] + "," + PROPS[i][1] + ")...");108thrstartRequest[i] = erManager.createThreadStartRequest();109thrstartRequest[i].putProperty(PROPS[i][0], PROPS[i][1]);110}111elThread = new EventListener();112elThread.start();113114// Check ThreadStart requests when event requests are disabled115log.display("\n1) Getting ThreadStartRequest objects with disabled event requests...");116checkRequests(erManager, 1);117118// Check ThreadStart requests when event requests are enabled119for (int i=0; i<THR_NUM; i++) {120thrstartRequest[i].enable();121}122log.display("\n2) Getting ThreadStartRequest objects with enabled event requests...");123checkRequests(erManager, 2);124125// Finish the test126for (int i=0; i<THR_NUM; i++) {127thrstartRequest[i].disable();128}129return quitDebuggee();130}131132private void checkRequests(EventRequestManager erManager, int t_case) {133List reqL = erManager.threadStartRequests();134if (reqL.size() != THR_NUM) {135log.complain("TEST CASE #" + t_case + " FAILED: found " + reqL.size()136+ " ThreadStartRequest objects\n\texpected: " + THR_NUM);137tot_res = FAILED;138return;139}140for (int i=0; i<THR_NUM; i++) {141ThreadStartRequest thrsReq =142(ThreadStartRequest) reqL.get(i);143boolean notFound = true;144for (int j=0; j<THR_NUM; j++) {145String propKey = (String) thrsReq.getProperty(PROPS[j][0]);146if (propKey != null) {147if (propKey.equals(PROPS[j][1])) {148log.display("Found expected ThreadStartRequest object with the property: ("149+ PROPS[j][0] + "," + propKey + ")");150} else {151log.complain("TEST CASE #" + t_case152+ " FAILED: found ThreadStartRequest object with the unexpected property: ("153+ PROPS[j][0] + "," + propKey + ")");154tot_res = FAILED;155}156notFound = false;157break;158}159}160if (notFound) {161log.complain("\nTEST CASE #" + t_case162+ " FAILED: found unexpected ThreadStartRequest object");163tot_res = FAILED;164}165}166}167168private int quitDebuggee() {169if (elThread != null) {170elThread.isConnected = false;171try {172if (elThread.isAlive())173elThread.join();174} catch (InterruptedException e) {175log.complain("TEST INCOMPLETE: caught InterruptedException "176+ e);177tot_res = FAILED;178}179}180181pipe.println(COMMAND_QUIT);182debuggee.waitFor();183int debStat = debuggee.getStatus();184if (debStat != (JCK_STATUS_BASE + PASSED)) {185log.complain("TEST FAILED: debuggee's process finished with status: "186+ debStat);187tot_res = FAILED;188} else189log.display("Debuggee's process finished with status: "190+ debStat);191192return tot_res;193}194195class EventListener extends Thread {196public volatile boolean isConnected = true;197198public void run() {199try {200do {201EventSet eventSet = vm.eventQueue().remove(1000);202if (eventSet != null) { // there is not a timeout203EventIterator it = eventSet.eventIterator();204while (it.hasNext()) {205Event event = it.nextEvent();206if (event instanceof VMDeathEvent) {207tot_res = FAILED;208isConnected = false;209log.complain("TEST FAILED: unexpected VMDeathEvent");210} else if (event instanceof VMDisconnectEvent) {211tot_res = FAILED;212isConnected = false;213log.complain("TEST FAILED: unexpected VMDisconnectEvent");214} else215log.display("EventListener: following JDI event occured: "216+ event.toString());217}218if (isConnected) {219eventSet.resume();220}221}222} while (isConnected);223} catch (InterruptedException e) {224tot_res = FAILED;225log.complain("FAILURE in EventListener: caught unexpected "226+ e);227} catch (VMDisconnectedException e) {228tot_res = FAILED;229log.complain("FAILURE in EventListener: caught unexpected "230+ e);231e.printStackTrace();232}233log.display("EventListener: exiting");234}235}236}237238239