Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventSet/resume/resume001.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.EventSet.resume;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829import com.sun.jdi.*;30import com.sun.jdi.event.*;31import com.sun.jdi.request.*;3233import java.util.*;34import java.io.*;3536import static nsk.share.Consts.TEST_FAILED;3738/**39* The test for the implementation of an object of the type40* EventSet.41*42* The test checks that results of the method43* <code>com.sun.jdi.EventSet.resume()</code>44* complies with its spec.45*46* Test cases include all three possible suspensions, NONE,47* EVENT_THREAD, and ALL, for ClassPrepareEvent sets.48*49* To check up on the method, a debugger,50* upon getting new set for ClassPrepareEvent,51* suspends VM with the method VirtualMachine.suspend(),52* gets the List of debuggee's threads calling VM.allThreads(),53* invokes the method EventSet.resume(), and54* gets another List of debuggee's threads.55* The debugger then compares values of56* each thread's suspendCount from first and second Lists.57*58* The test works as follows.59* - The debugger resumes the debuggee and60* waits for the ClassPrepareEvents.61* - The debuggee creates three threads, running one by one and62* making special clases loaded and prepared to create the Events63* - Upon getting the Events,64* the debugger performs the check required.65* - The debugger informs the debuggee when it completes66* each test case, so it will wait before hitting67* communication breakpoints.68* This prevents the breakpoint SUSPEND_ALL policy69* disrupting the first test case check for70* SUSPEND_NONE, if the debuggee gets ahead of71* the debugger processing.72*/7374public class resume001 extends TestDebuggerType1 {7576public static void main (String argv[]) {77System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);78}7980public static int run (String argv[], PrintStream out) {81debuggeeName = "nsk.jdi.EventSet.resume.resume001a";82return new resume001().runThis(argv, out);83}8485protected void testRun() {8687final int SUSPEND_NONE = EventRequest.SUSPEND_NONE;88final int SUSPEND_THREAD = EventRequest.SUSPEND_EVENT_THREAD;89final int SUSPEND_ALL = EventRequest.SUSPEND_ALL;9091String classNames[] = {92"nsk.jdi.EventSet.resume.TestClass2",93"nsk.jdi.EventSet.resume.TestClass3",94"nsk.jdi.EventSet.resume.TestClass4",95};9697EventRequest eventRequest;9899for (int i = 0; ; i++) {100101if (!shouldRunAfterBreakpoint()) {102vm.resume();103break;104}105106107display(":::::: case: # " + i);108109switch (i) {110111case 0:112eventRequest = settingClassPrepareRequest(classNames[0],113SUSPEND_NONE, "ClassPrepareRequest0");114break;115116case 1:117eventRequest = settingClassPrepareRequest(classNames[1],118SUSPEND_THREAD, "ClassPrepareRequest1");119break;120121case 2:122eventRequest = settingClassPrepareRequest(classNames[2],123SUSPEND_ALL, "ClassPrepareRequest2");124break;125126127default:128throw new Failure("** default case 2 **");129}130131display("......waiting for new ClassPrepareEvent : " + i);132EventSet eventSet = eventHandler.waitForRequestedEventSet(new EventRequest[]{eventRequest}, waitTime, true);133134EventIterator eventIterator = eventSet.eventIterator();135Event newEvent = eventIterator.nextEvent();136137if ( !(newEvent instanceof ClassPrepareEvent)) {138setFailedStatus("ERROR: new event is not ClassPreparedEvent");139} else {140141String property = (String) newEvent.request().getProperty("number");142display(" got new ClassPrepareEvent with propety 'number' == " + property);143144display("......checking up on EventSet.resume()");145display("......--> vm.suspend();");146vm.suspend();147148display(" getting : Map<String, Integer> suspendsCounts1");149150Map<String, Integer> suspendsCounts1 = new HashMap<String, Integer>();151for (ThreadReference threadReference : vm.allThreads()) {152suspendsCounts1.put(threadReference.name(), threadReference.suspendCount());153}154display(suspendsCounts1.toString());155156display(" eventSet.resume;");157eventSet.resume();158159display(" getting : Map<String, Integer> suspendsCounts2");160Map<String, Integer> suspendsCounts2 = new HashMap<String, Integer>();161for (ThreadReference threadReference : vm.allThreads()) {162suspendsCounts2.put(threadReference.name(), threadReference.suspendCount());163}164display(suspendsCounts2.toString());165166display(" getting : int policy = eventSet.suspendPolicy();");167int policy = eventSet.suspendPolicy();168169switch (policy) {170171case SUSPEND_NONE :172display(" case SUSPEND_NONE");173for (String threadName : suspendsCounts1.keySet()) {174display(" checking " + threadName);175if (!suspendsCounts2.containsKey(threadName)) {176complain("ERROR: couldn't get ThreadReference for " + threadName);177testExitCode = TEST_FAILED;178break;179}180int count1 = suspendsCounts1.get(threadName);181int count2 = suspendsCounts2.get(threadName);182if (count1 != count2) {183complain("ERROR: suspendCounts don't match for : " + threadName);184complain("before resuming : " + count1);185complain("after resuming : " + count2);186testExitCode = TEST_FAILED;187break;188}189}190break;191192case SUSPEND_THREAD :193display(" case SUSPEND_THREAD");194for (String threadName : suspendsCounts1.keySet()) {195display("checking " + threadName);196if (!suspendsCounts2.containsKey(threadName)) {197complain("ERROR: couldn't get ThreadReference for " + threadName);198testExitCode = TEST_FAILED;199break;200}201int count1 = suspendsCounts1.get(threadName);202int count2 = suspendsCounts2.get(threadName);203String eventThreadName = ((ClassPrepareEvent)newEvent).thread().name();204int expectedValue = count2 + (eventThreadName.equals(threadName) ? 1 : 0);205if (count1 != expectedValue) {206complain("ERROR: suspendCounts don't match for : " + threadName);207complain("before resuming : " + count1);208complain("after resuming : " + count2);209testExitCode = TEST_FAILED;210break;211}212}213break;214215case SUSPEND_ALL :216217display(" case SUSPEND_ALL");218for (String threadName : suspendsCounts1.keySet()) {219display("checking " + threadName);220221if (!newEvent.request().equals(eventRequest))222break;223if (!suspendsCounts2.containsKey(threadName)) {224complain("ERROR: couldn't get ThreadReference for " + threadName);225testExitCode = TEST_FAILED;226break;227}228int count1 = suspendsCounts1.get(threadName);229int count2 = suspendsCounts2.get(threadName);230if (count1 != count2 + 1) {231complain("ERROR: suspendCounts don't match for : " + threadName);232complain("before resuming : " + count1);233complain("after resuming : " + count2);234testExitCode = TEST_FAILED;235break;236}237}238break;239240default: throw new Failure("** default case 1 **");241}242informDebuggeeTestCase(i);243}244245display("......--> vm.resume()");246vm.resume();247}248return;249}250251private ClassPrepareRequest settingClassPrepareRequest ( String testedClass,252int suspendPolicy,253String property )254throws Failure {255try {256display("......setting up ClassPrepareRequest:");257display(" class: " + testedClass + "; property: " + property);258259ClassPrepareRequest260cpr = eventRManager.createClassPrepareRequest();261cpr.putProperty("number", property);262cpr.addClassFilter(testedClass);263cpr.setSuspendPolicy(suspendPolicy);264265display(" ClassPrepareRequest has been set up");266return cpr;267} catch ( Exception e ) {268throw new Failure("** FAILURE to set up ClassPrepareRequest **");269}270}271/**272* Inform debuggee which thread test the debugger has completed.273* Used for synchronization, so the debuggee does not move too quickly.274* @param testCase index of just completed test275*/276void informDebuggeeTestCase(int testCase) {277try {278((ClassType)debuggeeClass)279.setValue(debuggeeClass.fieldByName("testCase"),280vm.mirrorOf(testCase));281} catch (InvalidTypeException ite) {282throw new Failure("** FAILURE setting testCase **");283} catch (ClassNotLoadedException cnle) {284throw new Failure("** FAILURE notifying debuggee **");285} catch (VMDisconnectedException e) {286throw new Failure("** FAILURE debuggee connection **");287}288}289}290291292