author | clanger |
Thu, 17 Oct 2019 22:41:36 +0200 | |
changeset 58681 | 5f14a659a8cb |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
44423 | 25 |
* @test |
26 |
* @bug 4401399 |
|
27 |
* @summary Simple basic test of jdi Monitor request and event. |
|
28 |
* @author Swamy Venkataramanappa |
|
2 | 29 |
* |
44423 | 30 |
* @run build TestScaffold VMConnection TargetListener TargetAdapter |
31 |
* @run compile -g MonitorEventTest.java |
|
32 |
* @run driver MonitorEventTest |
|
2 | 33 |
*/ |
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
34 |
import com.sun.jdi.ReferenceType; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
35 |
import com.sun.jdi.ThreadReference; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
36 |
import com.sun.jdi.event.BreakpointEvent; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
37 |
import com.sun.jdi.event.MonitorContendedEnterEvent; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
38 |
import com.sun.jdi.event.MonitorContendedEnteredEvent; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
39 |
import com.sun.jdi.event.MonitorWaitEvent; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
40 |
import com.sun.jdi.event.MonitorWaitedEvent; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
41 |
import com.sun.jdi.request.EventRequest; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
42 |
import com.sun.jdi.request.MonitorContendedEnterRequest; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
43 |
import com.sun.jdi.request.MonitorContendedEnteredRequest; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
44 |
import com.sun.jdi.request.MonitorWaitRequest; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
45 |
import com.sun.jdi.request.MonitorWaitedRequest; |
2 | 46 |
|
47 |
/********** target program **********/ |
|
48 |
||
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
49 |
class MonitorEventTestTarg { |
2 | 50 |
public static Object endingMonitor; |
51 |
public static Object startingMonitor; |
|
52 |
public static final long timeout = 30 * 6000; // milliseconds |
|
53 |
||
54 |
public static volatile boolean aboutEnterLock; |
|
55 |
||
56 |
static void foo() { |
|
57 |
System.out.println("Howdy!"); |
|
58 |
} |
|
59 |
||
60 |
public static void main(String[] args){ |
|
61 |
endingMonitor = new Object(); |
|
62 |
startingMonitor = new Object(); |
|
63 |
||
64 |
myThread t1 = new myThread(); |
|
65 |
foo(); |
|
66 |
aboutEnterLock = false; |
|
67 |
||
68 |
synchronized(endingMonitor) { |
|
69 |
||
70 |
// run thread |
|
71 |
try { |
|
72 |
// start thread |
|
73 |
synchronized (startingMonitor) { |
|
74 |
t1.start(); |
|
75 |
startingMonitor.wait(timeout); |
|
76 |
} |
|
77 |
} catch (InterruptedException e) { |
|
78 |
System.out.println("Interrupted exception " + e); |
|
79 |
} |
|
80 |
||
81 |
Thread.yield(); |
|
82 |
while(!(aboutEnterLock && t1.getState() == Thread.State.BLOCKED)) { |
|
83 |
try { |
|
84 |
Thread.sleep(1000); |
|
85 |
}catch(Exception x){ |
|
86 |
System.out.println(x); |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
try { |
|
91 |
t1.join(timeout); |
|
92 |
} catch (Exception x){ |
|
93 |
System.out.println("Exception while thread.join :" + x); |
|
94 |
} |
|
95 |
System.out.println("Test exiting"); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
class myThread extends Thread { |
|
100 |
public void run() { |
|
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
101 |
synchronized(MonitorEventTestTarg.startingMonitor) { |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
102 |
MonitorEventTestTarg.startingMonitor.notify(); |
2 | 103 |
} |
104 |
||
105 |
// contended enter wait until main thread release monitor |
|
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
106 |
MonitorEventTestTarg.aboutEnterLock = true; |
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
107 |
synchronized (MonitorEventTestTarg.endingMonitor) { |
2 | 108 |
} |
109 |
} |
|
110 |
} |
|
111 |
||
112 |
||
113 |
/********** test program **********/ |
|
114 |
||
115 |
public class MonitorEventTest extends TestScaffold { |
|
116 |
ReferenceType targetClass; |
|
117 |
ThreadReference mainThread; |
|
118 |
MonitorContendedEnterRequest contendedEnterRequest; |
|
119 |
MonitorWaitedRequest monitorWaitedRequest; |
|
120 |
MonitorContendedEnteredRequest contendedEnteredRequest; |
|
121 |
MonitorWaitRequest monitorWaitRequest; |
|
122 |
||
123 |
static int actualWaitCount = 0; |
|
124 |
static int actualWaitedCount = 0; |
|
125 |
static int actualContendedEnterCount = 0; |
|
126 |
static int actualContendedEnteredCount= 0; |
|
127 |
||
128 |
MonitorEventTest (String args[]) { |
|
129 |
super(args); |
|
130 |
} |
|
131 |
||
132 |
public static void main(String[] args) throws Exception { |
|
133 |
new MonitorEventTest(args).startTests(); |
|
134 |
} |
|
135 |
||
136 |
/********** event handlers **********/ |
|
137 |
||
138 |
public void monitorContendedEnter(MonitorContendedEnterEvent event) { |
|
139 |
||
140 |
actualContendedEnterCount++; |
|
141 |
} |
|
142 |
||
143 |
public void monitorContendedEntered(MonitorContendedEnteredEvent event) { |
|
144 |
||
145 |
actualContendedEnteredCount++; |
|
146 |
||
147 |
} |
|
148 |
||
149 |
public void monitorWait(MonitorWaitEvent event) { |
|
150 |
||
151 |
actualWaitCount++; |
|
152 |
||
153 |
} |
|
154 |
public void monitorWaited(MonitorWaitedEvent event) { |
|
155 |
||
156 |
actualWaitedCount++; |
|
157 |
||
158 |
} |
|
159 |
||
160 |
||
161 |
||
162 |
/********** test core **********/ |
|
163 |
||
164 |
protected void runTests() throws Exception { |
|
165 |
/* |
|
166 |
* Get to the top of main() |
|
167 |
* to determine targetClass and mainThread |
|
168 |
*/ |
|
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
169 |
BreakpointEvent bpe = startToMain("MonitorEventTestTarg"); |
2 | 170 |
targetClass = bpe.location().declaringType(); |
171 |
mainThread = bpe.thread(); |
|
172 |
||
173 |
if (vm().canRequestMonitorEvents()) { |
|
174 |
contendedEnterRequest = eventRequestManager().createMonitorContendedEnterRequest(); |
|
175 |
contendedEnterRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); |
|
176 |
contendedEnterRequest.enable(); |
|
177 |
contendedEnteredRequest = eventRequestManager().createMonitorContendedEnteredRequest(); |
|
178 |
contendedEnteredRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); |
|
179 |
contendedEnteredRequest.enable(); |
|
180 |
monitorWaitRequest = eventRequestManager().createMonitorWaitRequest(); |
|
181 |
monitorWaitRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); |
|
182 |
monitorWaitRequest.enable(); |
|
183 |
monitorWaitedRequest = eventRequestManager().createMonitorWaitedRequest(); |
|
184 |
monitorWaitedRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); |
|
185 |
monitorWaitedRequest.enable(); |
|
186 |
} else { |
|
187 |
System.out.println("request monitor events not supported " ); |
|
188 |
} |
|
189 |
||
190 |
||
58681
5f14a659a8cb
8232370: Refactor some com.sun.jdi tests to enable IDE integration
clanger
parents:
47216
diff
changeset
|
191 |
resumeTo("MonitorEventTestTarg", "foo", "()V"); |
2 | 192 |
|
193 |
/* |
|
194 |
* resume until end |
|
195 |
*/ |
|
196 |
listenUntilVMDisconnect(); |
|
197 |
||
198 |
/* |
|
199 |
* At least one of each type event should have recevied by this test. |
|
200 |
*/ |
|
201 |
if (vm().canRequestMonitorEvents()) { |
|
202 |
if (actualContendedEnterCount == 0) { |
|
203 |
failure("Did not receive any contended enter event."); |
|
204 |
} |
|
205 |
if (actualContendedEnteredCount == 0) { |
|
206 |
failure("Did not receive any contended entered event. "); |
|
207 |
} |
|
208 |
if (actualWaitCount == 0) { |
|
209 |
failure("Did not receive any contended monitor wait event"); |
|
210 |
} |
|
211 |
if (actualWaitedCount == 0) { |
|
212 |
failure("Did not receive any contended monitor waited event"); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
||
217 |
/* |
|
218 |
* deal with results of test |
|
219 |
* if anything has called failure("foo") testFailed will be true |
|
220 |
*/ |
|
221 |
if (!testFailed) { |
|
222 |
println("MonitorEventTest: passed"); |
|
223 |
} else { |
|
224 |
throw new Exception("MonitorEventTest: failed"); |
|
225 |
} |
|
226 |
} |
|
227 |
} |