Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/EventQueue/remove/remove004.java
41161 views
/*1* Copyright (c) 2001, 2020, 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 jdk.test.lib.Utils;26import nsk.share.*;27import nsk.share.jpda.*;28import nsk.share.jdi.*;2930import com.sun.jdi.*;31import com.sun.jdi.event.*;32import com.sun.jdi.request.*;3334import java.util.*;35import java.io.*;3637/**38* The test for the implementation of an object of the type <BR>39* EventQueue. <BR>40* <BR>41* The test checks up that results of the method <BR>42* <code>com.sun.jdi.EventQueue.remove()</code> <BR>43* complies with its spec. <BR>44* <BR>45* Since method's assertion is <BR>46* "Waits forever for the next available event." <BR>47* in the condition of a limited time for testing, <BR>48* the test case limits time for waiting for the next event <BR>49* to the standard value of WAITTIME set up in the testbase_nsk.<BR>50* <BR>51* The test works as follows. <BR>52* Upon launching debuggee's VM which will be suspended, <BR>53* a debugger waits for the VMStartEvent within a predefined <BR>54* time interval. If no the VMStartEvent received, the test is FAILED.<BR>55* Upon getting the VMStartEvent, it makes the request <BR>56* for debuggee's ClassPrepareEvent with SUSPEND_EVENT_THREAD, <BR>57* resumes the VM, and waits for the event within the predefined<BR>58* time interval. If no the ClassPrepareEvent received, the test is FAILED.<BR>59* Upon getting the ClassPrepareEvent, <BR>60* the debugger sets up the breakpoint with SUSPEND_EVENT_THREAD,<BR>61* the debugger resumes the debuggee and waits for the BreakpointEvent.<BR>62* The debuggee prepares new check and invokes the methodForCommunication<BR>63* to be suspended and to inform the debugger with the event. <BR>64* Upon getting the BreakpointEvent, the debugger performs the check. <BR>65* At the end, the debuggee changes the value of the "instruction" <BR>66* to inform the debugger of checks finished, and both end. <BR>67* <BR>68* The check includes two steps. <BR>69* In first one, second thread waits for any incoming event from the <BR>70* debugger which is sleeping for some time; hence, <BR>71* no events are expected to be received at the debugger end. <BR>72* In second, second thread is interrupted, and the debugger waits for <BR>73* a breakpoint event after the debuggee finishes sleeping. <BR>74*/7576public class remove004 extends JDIBase {7778public static void main (String argv[]) {7980int result = run(argv, System.out);8182System.exit(result + PASS_BASE);83}8485public static int run (String argv[], PrintStream out) {8687int exitCode = new remove004().runThis(argv, out);8889if (exitCode != PASSED) {90System.out.println("TEST FAILED");91}92return testExitCode;93}9495// ************************************************ test parameters9697private String debuggeeName =98"nsk.jdi.EventQueue.remove.remove004a";99100//====================================================== test program101102static Value trueValue;103104private int runThis (String argv[], PrintStream out) {105106argsHandler = new ArgumentHandler(argv);107logHandler = new Log(out, argsHandler);108Binder binder = new Binder(argsHandler, logHandler);109110waitTime = Utils.adjustTimeout(argsHandler.getWaitTime() * 1000);111112try {113log2("launching a debuggee :");114log2(" " + debuggeeName);115if (argsHandler.verbose()) {116debuggee = binder.bindToDebugee(debuggeeName + " -vbs");117} else {118debuggee = binder.bindToDebugee(debuggeeName);119}120if (debuggee == null) {121log3("ERROR: no debuggee launched");122return FAILED;123}124log2("debuggee launched");125} catch ( Exception e ) {126log3("ERROR: Exception : " + e);127log2(" test cancelled");128return FAILED;129}130131debuggee.redirectOutput(logHandler);132133vm = debuggee.VM();134135eventQueue = vm.eventQueue();136if (eventQueue == null) {137log3("ERROR: eventQueue == null : TEST ABORTED");138vm.exit(PASS_BASE);139return FAILED;140}141142log2("invocation of the method runTest()");143switch (runTest()) {144145case 0 : log2("test phase has finished normally");146log2(" waiting for the debuggee to finish ...");147debuggee.waitFor();148149log2("......getting the debuggee's exit status");150int status = debuggee.getStatus();151if (status != PASS_BASE) {152log3("ERROR: debuggee returned UNEXPECTED exit status: " +153status + " != PASS_BASE");154testExitCode = FAILED;155} else {156log2("......debuggee returned expected exit status: " +157status + " == PASS_BASE");158}159break;160161default : log3("ERROR: runTest() returned unexpected value");162163case 1 : log3("test phase has not finished normally: debuggee is still alive");164log2("......forcing: vm.exit();");165testExitCode = FAILED;166try {167vm.exit(PASS_BASE);168} catch ( Exception e ) {169log3("ERROR: Exception : e");170}171break;172173case 2 : log3("test cancelled due to VMDisconnectedException");174log2("......trying: vm.process().destroy();");175testExitCode = FAILED;176try {177Process vmProcess = vm.process();178if (vmProcess != null) {179vmProcess.destroy();180}181} catch ( Exception e ) {182log3("ERROR: Exception : e");183}184break;185}186187return testExitCode;188}189190191/*192* Return value: 0 - normal end of the test193* 1 - ubnormal end of the test194* 2 - VMDisconnectedException while test phase195*/196197private int runTest() {198199try {200testRun();201202log2("waiting for VMDeathEvent");203getEventSet();204if (eventIterator.nextEvent() instanceof VMDeathEvent)205return 0;206207log3("ERROR: last event is not the VMDeathEvent");208return 1;209} catch ( VMDisconnectedException e ) {210log3("ERROR: VMDisconnectedException : " + e);211return 2;212} catch ( Exception e ) {213log3("ERROR: Exception : " + e);214return 1;215}216217}218219private void testRun()220throws JDITestRuntimeException, Exception {221222eventRManager = vm.eventRequestManager();223224ClassPrepareRequest cpRequest = eventRManager.createClassPrepareRequest();225cpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);226cpRequest.addClassFilter(debuggeeName);227228cpRequest.enable();229vm.resume();230getEventSet();231cpRequest.disable();232233ClassPrepareEvent event = (ClassPrepareEvent) eventIterator.next();234debuggeeClass = event.referenceType();235236if (!debuggeeClass.name().equals(debuggeeName))237throw new JDITestRuntimeException("** Unexpected ClassName for ClassPrepareEvent **");238239trueValue = debuggeeClass.getValue(debuggeeClass.fieldByName("BOOLEAN_TRUE_VALUE"));240241log2(" received: ClassPrepareEvent for debuggeeClass");242243String bPointMethod = "methodForCommunication";244String lineForComm = "lineForComm";245BreakpointRequest bpRequest;246247try {248bpRequest = settingBreakpoint(debuggee.threadByNameOrThrow("main"),249debuggeeClass,250bPointMethod, lineForComm, "zero");251} catch ( Exception e ) {252throw e;253}254bpRequest.enable();255256//------------------------------------------------------ testing section257258log1(" TESTING BEGINS");259vm.resume();260261for (int i = 0; ; i++) {262263breakpointForCommunication();264265int instruction = ((IntegerValue)266(debuggeeClass.getValue(debuggeeClass.fieldByName("instruction")))).value();267268if (instruction == 0) {269vm.resume();270break;271}272273log1(" new check: # " + i);274275//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part276277vm.resume();278279Threadremove004 thread2 = new Threadremove004("thread2");280log2(" thread2 is created");281282synchronized (lockingObject) {283synchronized (waitnotifyObj) {284log2(" before: thread2.start()");285thread2.start();286287try {288log2(" before: waitnotifyObj.wait();");289waitnotifyObj.wait();290} catch ( Exception e) {291log3("ERROR: Exception : " + e );292testExitCode = FAILED;293throw new JDITestRuntimeException("** Exception while waiting: notify() **");294}295}296}297log2("mainThread is out of: synchronized (lockingObject)");298299Object waitObj = new Object();300301synchronized (waitObj) {302waitObj.wait(waitTime);303if (!thread2.isAlive()) {304log3("ERROR: thread2 is not alive");305testExitCode = FAILED;306break;307}308309thread2.interrupt();310311for (int i2 = 0; i2 < waitTime; ) {312waitObj.wait(1000);313if (!thread2.isAlive()) {314break;315}316i2 += 1000;317}318if (thread2.isAlive()) {319log3("ERROR: thread2 is still alive");320testExitCode = FAILED;321break;322}323}324325//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~326}327log1(" TESTING ENDS");328return;329}330331// ============================== test's additional methods332333334public static Object waitnotifyObj = new Object();335public static Object lockingObject = new Object();336337class Threadremove004 extends Thread {338339public Threadremove004(String threadName) {340super(threadName);341}342343public void run() {344log2("-----t2: method 'run' enter");345synchronized (waitnotifyObj) {346log2("-----t2: entered into block: synchronized (waitnotifyObj)");347waitnotifyObj.notify();348}349log2("-----t2: exited from block: synchronized (waitnotifyObj)");350synchronized (lockingObject) {351log2("-----t2: entered into block: synchronized (lockingObject)");352}353log2("-----t2: exited from block: synchronized (lockingObject)");354355try {356log2("-----t2: eventSet = eventQueue.remove(); expects: InterruptedException");357eventSet = eventQueue.remove();358throw new JDITestRuntimeException("** return from eventQueue.remove(); **");359} catch (InterruptedException e1) {360log2("-----t2: InterruptedException");361// Signal to debuggee to stop sleeping362try {363((ClassType) debuggeeClass).setValue(debuggeeClass.fieldByName("stopSleeping"),364trueValue);365} catch (InvalidTypeException | ClassNotLoadedException e) {366log3("ERROR: -----t2: Exception : " + e);367testExitCode = FAILED;368}369} catch (Exception e) {370log3("ERROR: -----t2: Exception : " + e);371testExitCode = FAILED;372}373374log2("-----t2: method 'run' exit");375return;376}377}378}379380381