|
1 /* |
|
2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. 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 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. |
|
22 */ |
|
23 |
|
24 package nsk.jdwp.ThreadReference.Interrupt; |
|
25 |
|
26 import nsk.share.*; |
|
27 import nsk.share.jpda.*; |
|
28 import nsk.share.jdwp.*; |
|
29 |
|
30 import java.io.*; |
|
31 |
|
32 /** |
|
33 * This class represents debuggee part in the test. |
|
34 */ |
|
35 public class interrupt001a { |
|
36 |
|
37 // name for the tested thread |
|
38 public static final String THREAD_NAME = "TestedThreadName"; |
|
39 public static final String FIELD_NAME = "thread"; |
|
40 |
|
41 public static volatile boolean interrupted = false; |
|
42 |
|
43 // notification object to notify debuggee that thread is started |
|
44 private static Object threadStarting = new Object(); |
|
45 // object which thread will wait for before being interruted |
|
46 private static Object threadWaiting = new Object(); |
|
47 |
|
48 // scaffold objects |
|
49 private static volatile ArgumentHandler argumentHandler = null; |
|
50 private static volatile Log log = null; |
|
51 |
|
52 public static void main(String args[]) { |
|
53 interrupt001a _interrupt001a = new interrupt001a(); |
|
54 System.exit(interrupt001.JCK_STATUS_BASE + _interrupt001a.runIt(args, System.err)); |
|
55 } |
|
56 |
|
57 public int runIt(String args[], PrintStream out) { |
|
58 //make log for debugee messages |
|
59 argumentHandler = new ArgumentHandler(args); |
|
60 log = new Log(out, argumentHandler); |
|
61 long timeout = argumentHandler.getWaitTime() * 60 * 1000; // milliseconds |
|
62 |
|
63 // make communication pipe to debugger |
|
64 log.display("Creating pipe"); |
|
65 IOPipe pipe = argumentHandler.createDebugeeIOPipe(log); |
|
66 |
|
67 // load tested class and create tested thread |
|
68 log.display("Creating object of tested class"); |
|
69 TestedClass.thread = new TestedClass(THREAD_NAME); |
|
70 |
|
71 // start the thread and wait for notification from it |
|
72 synchronized (threadStarting) { |
|
73 TestedClass.thread.start(); |
|
74 try { |
|
75 threadStarting.wait(); |
|
76 } catch (InterruptedException e) { |
|
77 log.complain("Interruption while waiting for thread started:\n\t" + e); |
|
78 pipe.println(interrupt001.ERROR); |
|
79 log.display("Debugee FAILED"); |
|
80 return interrupt001.FAILED; |
|
81 } |
|
82 |
|
83 // send debugger signal READY |
|
84 log.display("Sending signal to debugger: " + interrupt001.READY); |
|
85 pipe.println(interrupt001.READY); |
|
86 } |
|
87 |
|
88 // wait for signal RUN from debugeer |
|
89 log.display("Waiting for signal from debugger: " + interrupt001.RUN); |
|
90 String signal = pipe.readln(); |
|
91 log.display("Received signal from debugger: " + signal); |
|
92 |
|
93 // check received signal |
|
94 if (signal == null || !signal.equals(interrupt001.RUN)) { |
|
95 log.complain("Unexpected communication signal from debugee: " + signal |
|
96 + " (expected: " + interrupt001.RUN + ")"); |
|
97 log.display("Debugee FAILED"); |
|
98 return interrupt001.FAILED; |
|
99 } |
|
100 |
|
101 // wait for thread finished in a waittime interval |
|
102 if (TestedClass.thread.isAlive()) { |
|
103 log.display("Waiting for tested thread finished for timeout: " + timeout); |
|
104 try { |
|
105 TestedClass.thread.join(timeout); |
|
106 } catch (InterruptedException e) { |
|
107 log.complain("Interruption while waiting for tested thread finished:\n\t" + e); |
|
108 pipe.println(interrupt001.ERROR); |
|
109 log.display("Debugee FAILED"); |
|
110 return interrupt001.FAILED; |
|
111 } |
|
112 } |
|
113 |
|
114 // test if thread was interrupted by debugger |
|
115 if (interrupt001a.interrupted) { |
|
116 log.display("Sending signal to debugger: " + interrupt001.INTERRUPTED_TRUE); |
|
117 pipe.println(interrupt001.INTERRUPTED_TRUE); |
|
118 } else { |
|
119 log.display("Sending signal to debugger: " + interrupt001.INTERRUPTED_FALSE); |
|
120 pipe.println(interrupt001.INTERRUPTED_FALSE); |
|
121 } |
|
122 |
|
123 // wait for signal QUIT from debugeer |
|
124 log.display("Waiting for signal from debugger: " + interrupt001.QUIT); |
|
125 signal = pipe.readln(); |
|
126 log.display("Received signal from debugger: " + signal); |
|
127 |
|
128 // check received signal |
|
129 if (signal == null || !signal.equals(interrupt001.QUIT)) { |
|
130 log.complain("Unexpected communication signal from debugee: " + signal |
|
131 + " (expected: " + interrupt001.QUIT + ")"); |
|
132 log.display("Debugee FAILED"); |
|
133 return interrupt001.FAILED; |
|
134 } |
|
135 |
|
136 // exit debugee |
|
137 log.display("Debugee PASSED"); |
|
138 return interrupt001.PASSED; |
|
139 } |
|
140 |
|
141 // tested thread class |
|
142 public static class TestedClass extends Thread { |
|
143 |
|
144 // field with the tested Thread value |
|
145 public static volatile TestedClass thread = null; |
|
146 |
|
147 TestedClass(String name) { |
|
148 super(name); |
|
149 } |
|
150 |
|
151 // start the thread and recursive invoke makeFrames() |
|
152 public void run() { |
|
153 log.display("Tested thread started"); |
|
154 |
|
155 synchronized (threadWaiting) { |
|
156 |
|
157 // notify debuggee that thread started |
|
158 synchronized (threadStarting) { |
|
159 threadStarting.notifyAll(); |
|
160 } |
|
161 |
|
162 // wait infinitely for notification object |
|
163 try { |
|
164 threadWaiting.wait(); |
|
165 log.complain("Tested thread NOT interrupted"); |
|
166 } catch (InterruptedException e) { |
|
167 log.display("Tested thread interrupted"); |
|
168 interrupt001a.interrupted = true; |
|
169 } |
|
170 } |
|
171 |
|
172 log.display("Tested thread finished"); |
|
173 } |
|
174 |
|
175 } |
|
176 |
|
177 } |