2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 4401399
|
|
27 |
* @summary Simple basic test of jdi Monitor request and event.
|
|
28 |
*
|
|
29 |
* @author Swamy Venkataramanappa
|
|
30 |
*
|
|
31 |
* @run build TestScaffold VMConnection TargetListener TargetAdapter
|
|
32 |
* @run compile -g MonitorEventTest.java
|
|
33 |
* @run main MonitorEventTest
|
|
34 |
*/
|
|
35 |
import com.sun.jdi.*;
|
|
36 |
import com.sun.jdi.event.*;
|
|
37 |
import com.sun.jdi.request.*;
|
|
38 |
|
|
39 |
import java.util.*;
|
|
40 |
|
|
41 |
/********** target program **********/
|
|
42 |
|
|
43 |
class MonitorTestTarg {
|
|
44 |
public static Object endingMonitor;
|
|
45 |
public static Object startingMonitor;
|
|
46 |
public static final long timeout = 30 * 6000; // milliseconds
|
|
47 |
|
|
48 |
public static volatile boolean aboutEnterLock;
|
|
49 |
|
|
50 |
static void foo() {
|
|
51 |
System.out.println("Howdy!");
|
|
52 |
}
|
|
53 |
|
|
54 |
public static void main(String[] args){
|
|
55 |
endingMonitor = new Object();
|
|
56 |
startingMonitor = new Object();
|
|
57 |
|
|
58 |
myThread t1 = new myThread();
|
|
59 |
foo();
|
|
60 |
aboutEnterLock = false;
|
|
61 |
|
|
62 |
synchronized(endingMonitor) {
|
|
63 |
|
|
64 |
// run thread
|
|
65 |
try {
|
|
66 |
// start thread
|
|
67 |
synchronized (startingMonitor) {
|
|
68 |
t1.start();
|
|
69 |
startingMonitor.wait(timeout);
|
|
70 |
}
|
|
71 |
} catch (InterruptedException e) {
|
|
72 |
System.out.println("Interrupted exception " + e);
|
|
73 |
}
|
|
74 |
|
|
75 |
Thread.yield();
|
|
76 |
while(!(aboutEnterLock && t1.getState() == Thread.State.BLOCKED)) {
|
|
77 |
try {
|
|
78 |
Thread.sleep(1000);
|
|
79 |
}catch(Exception x){
|
|
80 |
System.out.println(x);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
}
|
|
84 |
try {
|
|
85 |
t1.join(timeout);
|
|
86 |
} catch (Exception x){
|
|
87 |
System.out.println("Exception while thread.join :" + x);
|
|
88 |
}
|
|
89 |
System.out.println("Test exiting");
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
class myThread extends Thread {
|
|
94 |
public void run() {
|
|
95 |
synchronized(MonitorTestTarg.startingMonitor) {
|
|
96 |
MonitorTestTarg.startingMonitor.notify();
|
|
97 |
}
|
|
98 |
|
|
99 |
// contended enter wait until main thread release monitor
|
|
100 |
MonitorTestTarg.aboutEnterLock = true;
|
|
101 |
synchronized (MonitorTestTarg.endingMonitor) {
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
|
|
107 |
/********** test program **********/
|
|
108 |
|
|
109 |
public class MonitorEventTest extends TestScaffold {
|
|
110 |
ReferenceType targetClass;
|
|
111 |
ThreadReference mainThread;
|
|
112 |
List monitors;
|
|
113 |
MonitorContendedEnterRequest contendedEnterRequest;
|
|
114 |
MonitorWaitedRequest monitorWaitedRequest;
|
|
115 |
MonitorContendedEnteredRequest contendedEnteredRequest;
|
|
116 |
MonitorWaitRequest monitorWaitRequest;
|
|
117 |
|
|
118 |
static int actualWaitCount = 0;
|
|
119 |
static int actualWaitedCount = 0;
|
|
120 |
static int actualContendedEnterCount = 0;
|
|
121 |
static int actualContendedEnteredCount= 0;
|
|
122 |
|
|
123 |
MonitorEventTest (String args[]) {
|
|
124 |
super(args);
|
|
125 |
}
|
|
126 |
|
|
127 |
public static void main(String[] args) throws Exception {
|
|
128 |
new MonitorEventTest(args).startTests();
|
|
129 |
}
|
|
130 |
|
|
131 |
/********** event handlers **********/
|
|
132 |
|
|
133 |
public void monitorContendedEnter(MonitorContendedEnterEvent event) {
|
|
134 |
|
|
135 |
actualContendedEnterCount++;
|
|
136 |
}
|
|
137 |
|
|
138 |
public void monitorContendedEntered(MonitorContendedEnteredEvent event) {
|
|
139 |
|
|
140 |
actualContendedEnteredCount++;
|
|
141 |
|
|
142 |
}
|
|
143 |
|
|
144 |
public void monitorWait(MonitorWaitEvent event) {
|
|
145 |
|
|
146 |
actualWaitCount++;
|
|
147 |
|
|
148 |
}
|
|
149 |
public void monitorWaited(MonitorWaitedEvent event) {
|
|
150 |
|
|
151 |
actualWaitedCount++;
|
|
152 |
|
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
/********** test core **********/
|
|
158 |
|
|
159 |
protected void runTests() throws Exception {
|
|
160 |
/*
|
|
161 |
* Get to the top of main()
|
|
162 |
* to determine targetClass and mainThread
|
|
163 |
*/
|
|
164 |
BreakpointEvent bpe = startToMain("MonitorTestTarg");
|
|
165 |
targetClass = bpe.location().declaringType();
|
|
166 |
mainThread = bpe.thread();
|
|
167 |
|
|
168 |
int initialSize = mainThread.frames().size();
|
|
169 |
if (vm().canRequestMonitorEvents()) {
|
|
170 |
contendedEnterRequest = eventRequestManager().createMonitorContendedEnterRequest();
|
|
171 |
contendedEnterRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
|
|
172 |
contendedEnterRequest.enable();
|
|
173 |
contendedEnteredRequest = eventRequestManager().createMonitorContendedEnteredRequest();
|
|
174 |
contendedEnteredRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
|
|
175 |
contendedEnteredRequest.enable();
|
|
176 |
monitorWaitRequest = eventRequestManager().createMonitorWaitRequest();
|
|
177 |
monitorWaitRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
|
|
178 |
monitorWaitRequest.enable();
|
|
179 |
monitorWaitedRequest = eventRequestManager().createMonitorWaitedRequest();
|
|
180 |
monitorWaitedRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
|
|
181 |
monitorWaitedRequest.enable();
|
|
182 |
} else {
|
|
183 |
System.out.println("request monitor events not supported " );
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
resumeTo("MonitorTestTarg", "foo", "()V");
|
|
188 |
|
|
189 |
/*
|
|
190 |
* resume until end
|
|
191 |
*/
|
|
192 |
listenUntilVMDisconnect();
|
|
193 |
|
|
194 |
/*
|
|
195 |
* At least one of each type event should have recevied by this test.
|
|
196 |
*/
|
|
197 |
if (vm().canRequestMonitorEvents()) {
|
|
198 |
if (actualContendedEnterCount == 0) {
|
|
199 |
failure("Did not receive any contended enter event.");
|
|
200 |
}
|
|
201 |
if (actualContendedEnteredCount == 0) {
|
|
202 |
failure("Did not receive any contended entered event. ");
|
|
203 |
}
|
|
204 |
if (actualWaitCount == 0) {
|
|
205 |
failure("Did not receive any contended monitor wait event");
|
|
206 |
}
|
|
207 |
if (actualWaitedCount == 0) {
|
|
208 |
failure("Did not receive any contended monitor waited event");
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
|
|
213 |
/*
|
|
214 |
* deal with results of test
|
|
215 |
* if anything has called failure("foo") testFailed will be true
|
|
216 |
*/
|
|
217 |
if (!testFailed) {
|
|
218 |
println("MonitorEventTest: passed");
|
|
219 |
} else {
|
|
220 |
throw new Exception("MonitorEventTest: failed");
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|