author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
17482
diff
changeset
|
2 |
* Copyright (c) 2004, 2013, 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 |
/* |
|
25 |
* @test |
|
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
26 |
* @bug 4919105 8004177 |
2 | 27 |
* @summary Generified basic unit test of Thread.getAllStackTraces() |
28 |
* @author Mandy Chung |
|
29 |
*/ |
|
30 |
||
31 |
import java.util.*; |
|
32 |
||
33 |
public class GenerifyStackTraces { |
|
34 |
||
35 |
private static Object go = new Object(); |
|
36 |
private static String[] methodNames = {"run", "A", "B", "C", "Done"}; |
|
37 |
private static int DONE_DEPTH = 5; |
|
38 |
private static boolean testFailed = false; |
|
39 |
||
40 |
private static Thread one; |
|
41 |
private static boolean trace = false; |
|
42 |
public static void main(String[] args) throws Exception { |
|
43 |
if (args.length > 0 && args[0].equals("trace")) { |
|
44 |
trace = true; |
|
45 |
} |
|
46 |
||
47 |
one = new ThreadOne(); |
|
48 |
one.start(); |
|
49 |
||
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
50 |
DumpThread dt = new DumpThread(); |
2 | 51 |
dt.start(); |
52 |
||
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
53 |
try { |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
54 |
one.join(); |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
55 |
} finally { |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
56 |
dt.shutdown(); |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
57 |
} |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
58 |
|
2 | 59 |
if (testFailed) { |
60 |
throw new RuntimeException("Test Failed."); |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
static class DumpThread extends Thread { |
|
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
65 |
private volatile boolean finished = false; |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
66 |
|
2 | 67 |
public void run() { |
68 |
int depth = 2; |
|
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
69 |
while (!finished) { |
2 | 70 |
// At each iterator, wait until ThreadOne blocks |
71 |
// to wait for thread dump. |
|
72 |
// Then dump stack trace and notify ThreadOne to continue. |
|
73 |
try { |
|
74 |
sleep(2000); |
|
75 |
dumpStacks(depth); |
|
76 |
depth++; |
|
77 |
finishDump(); |
|
78 |
} catch (Exception e) { |
|
79 |
e.printStackTrace(); |
|
80 |
testFailed = true; |
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
17482
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
84 |
|
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
85 |
public void shutdown() throws InterruptedException { |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
86 |
finished = true; |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
87 |
this.join(); |
c3efc9e2e227
8004177: test/java/lang/Thread/GenerifyStackTraces.java doesn't clean-up
ewang
parents:
7668
diff
changeset
|
88 |
} |
2 | 89 |
} |
90 |
||
91 |
static class ThreadOne extends Thread { |
|
92 |
public void run() { |
|
93 |
A(); |
|
94 |
} |
|
95 |
private void A() { |
|
96 |
waitForDump(); |
|
97 |
B(); |
|
98 |
} |
|
99 |
private void B() { |
|
100 |
waitForDump(); |
|
101 |
C(); |
|
102 |
} |
|
103 |
private void C() { |
|
104 |
waitForDump(); |
|
105 |
Done(); |
|
106 |
} |
|
107 |
private void Done() { |
|
108 |
waitForDump(); |
|
109 |
||
110 |
// Get stack trace of current thread |
|
111 |
StackTraceElement[] stack = getStackTrace(); |
|
112 |
try { |
|
113 |
checkStack(this, stack, DONE_DEPTH); |
|
114 |
} catch (Exception e) { |
|
115 |
e.printStackTrace(); |
|
116 |
testFailed = true; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
} |
|
121 |
||
122 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
23010
diff
changeset
|
123 |
private static void waitForDump() { |
2 | 124 |
synchronized(go) { |
125 |
try { |
|
126 |
go.wait(); |
|
127 |
} catch (Exception e) { |
|
128 |
throw new RuntimeException("Unexpected exception" + e); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
23010
diff
changeset
|
133 |
private static void finishDump() { |
2 | 134 |
synchronized(go) { |
135 |
try { |
|
136 |
go.notifyAll(); |
|
137 |
} catch (Exception e) { |
|
138 |
throw new RuntimeException("Unexpected exception" + e); |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
public static void dumpStacks(int depth) throws Exception { |
|
144 |
// Get stack trace of another thread |
|
145 |
StackTraceElement[] stack = one.getStackTrace(); |
|
146 |
checkStack(one, stack, depth); |
|
147 |
||
148 |
||
149 |
// Get stack traces of all Threads |
|
150 |
for (Map.Entry<Thread, StackTraceElement[]> entry : |
|
151 |
Thread.getAllStackTraces().entrySet()) { |
|
152 |
Thread t = entry.getKey(); |
|
153 |
stack = entry.getValue(); |
|
154 |
if (t == null || stack == null) { |
|
155 |
throw new RuntimeException("Null thread or stacktrace returned"); |
|
156 |
} |
|
157 |
if (t == one) { |
|
158 |
checkStack(t, stack, depth); |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
private static void checkStack(Thread t, StackTraceElement[] stack, |
|
164 |
int depth) throws Exception { |
|
165 |
if (trace) { |
|
166 |
printStack(t, stack); |
|
167 |
} |
|
168 |
int frame = stack.length - 1; |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
169 |
for (int i = 0; i < depth && frame >= 0; i++) { |
2 | 170 |
if (! stack[frame].getMethodName().equals(methodNames[i])) { |
171 |
throw new RuntimeException("Expected " + methodNames[i] + |
|
172 |
" in frame " + frame + " but got " + |
|
173 |
stack[frame].getMethodName()); |
|
174 |
} |
|
175 |
frame--; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
private static void printStack(Thread t, StackTraceElement[] stack) { |
|
180 |
System.out.println(t + |
|
181 |
" stack: (length = " + stack.length + ")"); |
|
182 |
if (t != null) { |
|
183 |
for (int j = 0; j < stack.length; j++) { |
|
184 |
System.out.println(stack[j]); |
|
185 |
} |
|
186 |
System.out.println(); |
|
187 |
} |
|
188 |
} |
|
189 |
} |