34372
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 8143214
|
|
27 |
* @summary Verify that StackWalker works well when one instance of StackWalker
|
|
28 |
* is used by several threads sequentially or concurrently.
|
|
29 |
* @run testng AcrossThreads
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.util.ArrayList;
|
|
33 |
import java.util.EnumSet;
|
|
34 |
import java.util.HashMap;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Map;
|
|
37 |
|
|
38 |
import java.lang.StackWalker.StackFrame;
|
|
39 |
import static java.lang.StackWalker.Option.*;
|
|
40 |
|
|
41 |
import org.testng.annotations.*;
|
|
42 |
import static org.testng.Assert.*;
|
|
43 |
|
|
44 |
public class AcrossThreads {
|
|
45 |
static final StackWalker WALKERS[] = new StackWalker[] {
|
|
46 |
StackWalker.getInstance(RETAIN_CLASS_REFERENCE),
|
|
47 |
StackWalker.getInstance(EnumSet.of(SHOW_REFLECT_FRAMES, RETAIN_CLASS_REFERENCE)),
|
|
48 |
StackWalker.getInstance(EnumSet.of(SHOW_HIDDEN_FRAMES, RETAIN_CLASS_REFERENCE))
|
|
49 |
};
|
|
50 |
|
|
51 |
@DataProvider
|
|
52 |
public StackWalker[][] walkerProvider() {
|
|
53 |
return new StackWalker[][] {
|
|
54 |
new StackWalker[] { WALKERS[0] },
|
|
55 |
new StackWalker[] { WALKERS[1] },
|
|
56 |
new StackWalker[] { WALKERS[2] }
|
|
57 |
};
|
|
58 |
}
|
|
59 |
|
|
60 |
@Test(dataProvider = "walkerProvider")
|
|
61 |
public void test(StackWalker walker) {
|
|
62 |
Thread t1 = new T1(walker);
|
|
63 |
// call methods of one instance of StackWalker sequentially in T1, T2, T3.
|
|
64 |
t1.start();
|
|
65 |
try {
|
|
66 |
t1.join();
|
|
67 |
} catch (InterruptedException e) { }
|
|
68 |
|
|
69 |
List<Thread> threads = new ArrayList<Thread>();
|
|
70 |
for (int i = 0; i < 100; i++) {
|
|
71 |
threads.add(new T1(walker));
|
|
72 |
threads.add(new T2(walker));
|
|
73 |
threads.add(new T3(walker));
|
|
74 |
}
|
|
75 |
// call methods of one instance of StackWalker concurrently in several threads.
|
|
76 |
threads.parallelStream().forEach(t -> {
|
|
77 |
t.setDaemon(true);
|
|
78 |
t.start();
|
|
79 |
});
|
|
80 |
threads.parallelStream().forEach(t -> {
|
|
81 |
try {
|
|
82 |
t.join();
|
|
83 |
} catch (InterruptedException e) { }
|
|
84 |
});
|
|
85 |
}
|
|
86 |
|
|
87 |
interface Consumer {
|
|
88 |
final int LOOPS = 5;
|
|
89 |
|
|
90 |
public void consume();
|
|
91 |
|
|
92 |
default public void assertWalker(StackWalker walker, int n) {
|
|
93 |
if (--n == 0) {
|
|
94 |
Map<String, Integer> methods = new HashMap<String, Integer>();
|
|
95 |
walker.forEach(f -> {
|
|
96 |
Integer i = methods.putIfAbsent(f.getMethodName(), 1);
|
|
97 |
if (i != null) {
|
|
98 |
methods.put(f.getMethodName(), i + 1);
|
|
99 |
}
|
|
100 |
});
|
|
101 |
|
|
102 |
// verify that walker.forEach(...) reaches the specified methods.
|
|
103 |
assertTrue(methods.get("consume") == 1);
|
|
104 |
assertTrue(methods.get("run") == 1);
|
|
105 |
assertTrue(methods.get("assertWalker") == LOOPS);
|
|
106 |
|
|
107 |
// verify that walker.walk(...) reaches the specified methods.
|
|
108 |
assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
|
|
109 |
.filter(mn -> mn.equals("consume"))
|
|
110 |
.count()) == 1);
|
|
111 |
assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
|
|
112 |
.filter(mn -> mn.equals("run"))
|
|
113 |
.count()) == 1);
|
|
114 |
assertTrue(walker.walk(s -> s.map(StackFrame::getMethodName)
|
|
115 |
.filter(mn -> mn.equals("assertWalker"))
|
|
116 |
.count()) == LOOPS);
|
|
117 |
} else {
|
|
118 |
assertWalker(walker, n);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
class T1 extends Thread implements Consumer {
|
|
124 |
final StackWalker walker;
|
|
125 |
|
|
126 |
public T1(StackWalker walker) {
|
|
127 |
this.walker = walker;
|
|
128 |
}
|
|
129 |
|
|
130 |
public void run() {
|
|
131 |
consume();
|
|
132 |
|
|
133 |
Thread t2 = new T2(walker);
|
|
134 |
t2.start();
|
|
135 |
try {
|
|
136 |
t2.join();
|
|
137 |
} catch (InterruptedException e) { }
|
|
138 |
|
|
139 |
consume();
|
|
140 |
}
|
|
141 |
|
|
142 |
public void consume() {
|
|
143 |
assertWalker(walker, LOOPS);
|
|
144 |
|
|
145 |
// verify walker.walk() reaches T1 class through methods run() and consume().
|
|
146 |
assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
|
|
147 |
.count()) == 2);
|
|
148 |
|
|
149 |
assertCallerClass(walker);
|
|
150 |
assertEquals(T1.class, walker.getCallerClass());
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
class T2 extends Thread implements Consumer {
|
|
155 |
final StackWalker walker;
|
|
156 |
|
|
157 |
public T2(StackWalker walker) {
|
|
158 |
this.walker = walker;
|
|
159 |
}
|
|
160 |
|
|
161 |
public void run() {
|
|
162 |
consume();
|
|
163 |
|
|
164 |
Thread t3 = new T3(walker);
|
|
165 |
t3.start();
|
|
166 |
try {
|
|
167 |
t3.join();
|
|
168 |
} catch (InterruptedException e) { }
|
|
169 |
|
|
170 |
consume();
|
|
171 |
}
|
|
172 |
|
|
173 |
public void consume() {
|
|
174 |
assertWalker(walker, LOOPS);
|
|
175 |
|
|
176 |
// verify walker.walk() reaches T2 class through methods run() and consume().
|
|
177 |
assertTrue(walker.walk(s -> s.filter(f -> T2.class == f.getDeclaringClass())
|
|
178 |
.count()) == 2);
|
|
179 |
// verify T1 is not reached, even if call is invoked
|
|
180 |
// from test()->T1.start()->T1.run()->T2
|
|
181 |
assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
|
|
182 |
.count()) == 0);
|
|
183 |
|
|
184 |
assertCallerClass(walker);
|
|
185 |
assertEquals(T2.class, walker.getCallerClass());
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
class T3 extends Thread implements Consumer {
|
|
190 |
final StackWalker walker;
|
|
191 |
|
|
192 |
public T3(StackWalker walker) {
|
|
193 |
this.walker = walker;
|
|
194 |
}
|
|
195 |
|
|
196 |
public void run() {
|
|
197 |
consume();
|
|
198 |
}
|
|
199 |
|
|
200 |
public void consume() {
|
|
201 |
assertWalker(walker, LOOPS);
|
|
202 |
|
|
203 |
// verify walker.walk() reaches T1 class through methods run() and consume().
|
|
204 |
assertTrue(walker.walk(s -> s.filter(f -> T3.class == f.getDeclaringClass())
|
|
205 |
.count()) == 2);
|
|
206 |
// verify T1, T2 is not reached, even if call is invoked
|
|
207 |
// from test() -> T1.start() -> T1.run() -> T2.start() -> T2.run() -> T3
|
|
208 |
assertTrue(walker.walk(s -> s.filter(f -> T2.class == f.getDeclaringClass())
|
|
209 |
.count()) == 0);
|
|
210 |
assertTrue(walker.walk(s -> s.filter(f -> T1.class == f.getDeclaringClass())
|
|
211 |
.count()) == 0);
|
|
212 |
|
|
213 |
assertCallerClass(walker);
|
|
214 |
assertEquals(T3.class, walker.getCallerClass());
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
static void assertCallerClass(StackWalker walker) {
|
|
219 |
// verify walker.getCallerClass() get the expected class.
|
|
220 |
call(walker);
|
|
221 |
}
|
|
222 |
|
|
223 |
static void call(StackWalker walker) {
|
|
224 |
Class<?> c = walker.getCallerClass();
|
|
225 |
assertEquals(c, AcrossThreads.class);
|
|
226 |
}
|
|
227 |
}
|