author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
child 42322 | c3474fef4fe4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22952
diff
changeset
|
2 |
* Copyright (c) 2007, 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 |
|
26 |
* @bug 6450200 6450205 6450207 6450211 |
|
27 |
* @summary Test proper handling of tasks that terminate abruptly |
|
28 |
* @author Martin Buchholz |
|
29 |
*/ |
|
30 |
||
34347 | 31 |
import java.security.Permission; |
32 |
import java.util.Arrays; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.Hashtable; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Random; |
|
39 |
import java.util.concurrent.ConcurrentHashMap; |
|
40 |
import java.util.concurrent.CountDownLatch; |
|
41 |
import java.util.concurrent.LinkedBlockingQueue; |
|
42 |
import java.util.concurrent.ThreadFactory; |
|
43 |
import java.util.concurrent.ThreadPoolExecutor; |
|
44 |
import java.util.concurrent.TimeUnit; |
|
45 |
import java.util.concurrent.atomic.AtomicInteger; |
|
46 |
import java.util.concurrent.atomic.AtomicLong; |
|
21605
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
47 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 48 |
|
49 |
public class ThrowingTasks { |
|
7518 | 50 |
static final Random rnd = new Random(); |
2 | 51 |
|
52 |
@SuppressWarnings("serial") |
|
53 |
static class UncaughtExceptions |
|
54 |
extends ConcurrentHashMap<Class<?>, Integer> { |
|
55 |
||
56 |
void inc(Class<?> key) { |
|
57 |
for (;;) { |
|
58 |
Integer i = get(key); |
|
59 |
if (i == null) { |
|
60 |
if (putIfAbsent(key, 1) == null) |
|
61 |
return; |
|
62 |
} else { |
|
63 |
if (replace(key, i, i + 1)) |
|
64 |
return; |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
@SuppressWarnings("serial") |
|
71 |
static class UncaughtExceptionsTable |
|
72 |
extends Hashtable<Class<?>, Integer> { |
|
73 |
||
74 |
synchronized void inc(Class<?> key) { |
|
75 |
Integer i = get(key); |
|
76 |
put(key, (i == null) ? 1 : i + 1); |
|
77 |
} |
|
78 |
} |
|
79 |
||
7518 | 80 |
static final UncaughtExceptions uncaughtExceptions |
2 | 81 |
= new UncaughtExceptions(); |
7518 | 82 |
static final UncaughtExceptionsTable uncaughtExceptionsTable |
2 | 83 |
= new UncaughtExceptionsTable(); |
7518 | 84 |
static final AtomicLong totalUncaughtExceptions |
2 | 85 |
= new AtomicLong(0); |
7518 | 86 |
static final CountDownLatch uncaughtExceptionsLatch |
2 | 87 |
= new CountDownLatch(24); |
88 |
||
7518 | 89 |
static final Thread.UncaughtExceptionHandler handler |
2 | 90 |
= new Thread.UncaughtExceptionHandler() { |
91 |
public void uncaughtException(Thread t, Throwable e) { |
|
92 |
check(! Thread.currentThread().isInterrupted()); |
|
93 |
totalUncaughtExceptions.getAndIncrement(); |
|
94 |
uncaughtExceptions.inc(e.getClass()); |
|
95 |
uncaughtExceptionsTable.inc(e.getClass()); |
|
96 |
uncaughtExceptionsLatch.countDown(); |
|
97 |
}}; |
|
98 |
||
7518 | 99 |
static final ThreadGroup tg = new ThreadGroup("Flaky"); |
2 | 100 |
|
7518 | 101 |
static final ThreadFactory tf = new ThreadFactory() { |
2 | 102 |
public Thread newThread(Runnable r) { |
103 |
Thread t = new Thread(tg, r); |
|
104 |
t.setUncaughtExceptionHandler(handler); |
|
105 |
return t; |
|
106 |
}}; |
|
107 |
||
7518 | 108 |
static final RuntimeException rte = new RuntimeException(); |
109 |
static final Error error = new Error(); |
|
110 |
static final Throwable weird = new Throwable(); |
|
111 |
static final Exception checkedException = new Exception(); |
|
2 | 112 |
|
113 |
static class Thrower implements Runnable { |
|
114 |
Throwable t; |
|
115 |
Thrower(Throwable t) { this.t = t; } |
|
18160
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
116 |
public void run() { |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
117 |
if (t != null) |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
118 |
ThrowingTasks.<RuntimeException>uncheckedThrow(t); |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
119 |
} |
2 | 120 |
} |
121 |
||
7518 | 122 |
static final Thrower noThrower = new Thrower(null); |
123 |
static final Thrower rteThrower = new Thrower(rte); |
|
124 |
static final Thrower errorThrower = new Thrower(error); |
|
125 |
static final Thrower weirdThrower = new Thrower(weird); |
|
126 |
static final Thrower checkedThrower = new Thrower(checkedException); |
|
2 | 127 |
|
7518 | 128 |
static final List<Thrower> throwers = Arrays.asList( |
2 | 129 |
noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower); |
130 |
||
131 |
static class Flaky implements Runnable { |
|
132 |
final Runnable beforeExecute; |
|
133 |
final Runnable execute; |
|
134 |
Flaky(Runnable beforeExecute, |
|
135 |
Runnable execute) { |
|
136 |
this.beforeExecute = beforeExecute; |
|
137 |
this.execute = execute; |
|
138 |
} |
|
139 |
public void run() { execute.run(); } |
|
140 |
} |
|
141 |
||
142 |
static final List<Flaky> flakes = new ArrayList<Flaky>(); |
|
143 |
static { |
|
144 |
for (Thrower x : throwers) |
|
145 |
for (Thrower y : throwers) |
|
146 |
flakes.add(new Flaky(x, y)); |
|
147 |
Collections.shuffle(flakes); |
|
148 |
} |
|
149 |
||
150 |
static final CountDownLatch allStarted = new CountDownLatch(flakes.size()); |
|
151 |
static final CountDownLatch allContinue = new CountDownLatch(1); |
|
152 |
||
153 |
static class PermissiveSecurityManger extends SecurityManager { |
|
154 |
public void checkPermission(Permission p) { /* bien sur, Monsieur */ } |
|
155 |
} |
|
156 |
||
157 |
static void checkTerminated(ThreadPoolExecutor tpe) { |
|
158 |
try { |
|
159 |
check(tpe.getQueue().isEmpty()); |
|
160 |
check(tpe.isShutdown()); |
|
161 |
check(tpe.isTerminated()); |
|
162 |
check(! tpe.isTerminating()); |
|
163 |
equal(tpe.getActiveCount(), 0); |
|
164 |
equal(tpe.getPoolSize(), 0); |
|
165 |
equal(tpe.getTaskCount(), tpe.getCompletedTaskCount()); |
|
34347 | 166 |
check(tpe.awaitTermination(0L, TimeUnit.SECONDS)); |
2 | 167 |
} catch (Throwable t) { unexpected(t); } |
168 |
} |
|
169 |
||
170 |
static class CheckingExecutor extends ThreadPoolExecutor { |
|
21605
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
171 |
private final ReentrantLock lock = new ReentrantLock(); |
2 | 172 |
CheckingExecutor() { |
173 |
super(10, 10, |
|
174 |
1L, TimeUnit.HOURS, |
|
175 |
new LinkedBlockingQueue<Runnable>(), |
|
176 |
tf); |
|
177 |
} |
|
178 |
@Override protected void beforeExecute(Thread t, Runnable r) { |
|
21605
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
179 |
final boolean lessThanCorePoolSize; |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
180 |
// Add a lock to sync allStarted.countDown() and |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
181 |
// allStarted.getCount() < getCorePoolSize() |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
182 |
lock.lock(); |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
183 |
try { |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
184 |
allStarted.countDown(); |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
185 |
lessThanCorePoolSize = allStarted.getCount() < getCorePoolSize(); |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
186 |
} finally { |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
187 |
lock.unlock(); |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
188 |
} |
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
189 |
if (lessThanCorePoolSize) { |
2 | 190 |
try { allContinue.await(); } |
191 |
catch (InterruptedException x) { unexpected(x); } |
|
21605
d32edf24ffcb
8025198: Intermittent test failure: java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
dholmes
parents:
18160
diff
changeset
|
192 |
} |
2 | 193 |
beforeExecuteCount.getAndIncrement(); |
194 |
check(! isTerminated()); |
|
195 |
((Flaky)r).beforeExecute.run(); |
|
196 |
} |
|
197 |
@Override protected void afterExecute(Runnable r, Throwable t) { |
|
198 |
//System.out.println(tg.activeCount()); |
|
199 |
afterExecuteCount.getAndIncrement(); |
|
200 |
check(((Thrower)((Flaky)r).execute).t == t); |
|
201 |
check(! isTerminated()); |
|
202 |
} |
|
203 |
@Override protected void terminated() { |
|
204 |
try { |
|
205 |
terminatedCount.getAndIncrement(); |
|
206 |
if (rnd.nextBoolean()) { |
|
207 |
check(isShutdown()); |
|
208 |
check(isTerminating()); |
|
209 |
check(! isTerminated()); |
|
210 |
check(! awaitTermination(0L, TimeUnit.MINUTES)); |
|
211 |
} |
|
212 |
} catch (Throwable t) { unexpected(t); } |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
static final AtomicInteger beforeExecuteCount = new AtomicInteger(0); |
|
217 |
static final AtomicInteger afterExecuteCount = new AtomicInteger(0); |
|
218 |
static final AtomicInteger terminatedCount = new AtomicInteger(0); |
|
219 |
||
220 |
private static void realMain(String[] args) throws Throwable { |
|
221 |
if (rnd.nextBoolean()) |
|
222 |
System.setSecurityManager(new PermissiveSecurityManger()); |
|
223 |
||
224 |
CheckingExecutor tpe = new CheckingExecutor(); |
|
225 |
||
226 |
for (Runnable task : flakes) |
|
227 |
tpe.execute(task); |
|
228 |
||
229 |
if (rnd.nextBoolean()) { |
|
230 |
allStarted.await(); |
|
231 |
equal(tpe.getTaskCount(), |
|
232 |
(long) flakes.size()); |
|
233 |
equal(tpe.getCompletedTaskCount(), |
|
234 |
(long) flakes.size() - tpe.getCorePoolSize()); |
|
235 |
} |
|
236 |
allContinue.countDown(); |
|
237 |
||
238 |
//System.out.printf("thread count = %d%n", tg.activeCount()); |
|
239 |
uncaughtExceptionsLatch.await(); |
|
240 |
||
241 |
while (tg.activeCount() != tpe.getCorePoolSize() || |
|
242 |
tg.activeCount() != tpe.getCorePoolSize()) |
|
243 |
Thread.sleep(10); |
|
244 |
equal(tg.activeCount(), tpe.getCorePoolSize()); |
|
245 |
||
246 |
tpe.shutdown(); |
|
247 |
||
248 |
check(tpe.awaitTermination(10L, TimeUnit.MINUTES)); |
|
249 |
checkTerminated(tpe); |
|
250 |
||
251 |
//while (tg.activeCount() > 0) Thread.sleep(10); |
|
252 |
//System.out.println(uncaughtExceptions); |
|
253 |
List<Map<Class<?>, Integer>> maps |
|
254 |
= new ArrayList<Map<Class<?>, Integer>>(); |
|
255 |
maps.add(uncaughtExceptions); |
|
256 |
maps.add(uncaughtExceptionsTable); |
|
257 |
for (Map<Class<?>, Integer> map : maps) { |
|
258 |
equal(map.get(Exception.class), throwers.size()); |
|
259 |
equal(map.get(weird.getClass()), throwers.size()); |
|
260 |
equal(map.get(Error.class), throwers.size() + 1 + 2); |
|
261 |
equal(map.get(RuntimeException.class), throwers.size() + 1); |
|
262 |
equal(map.size(), 4); |
|
263 |
} |
|
264 |
equal(totalUncaughtExceptions.get(), 4L*throwers.size() + 4L); |
|
265 |
||
266 |
equal(beforeExecuteCount.get(), flakes.size()); |
|
267 |
equal(afterExecuteCount.get(), throwers.size()); |
|
268 |
equal(tpe.getCompletedTaskCount(), (long) flakes.size()); |
|
269 |
equal(terminatedCount.get(), 1); |
|
270 |
||
271 |
// check for termination operation idempotence |
|
272 |
tpe.shutdown(); |
|
273 |
tpe.shutdownNow(); |
|
274 |
check(tpe.awaitTermination(10L, TimeUnit.MINUTES)); |
|
275 |
checkTerminated(tpe); |
|
276 |
equal(terminatedCount.get(), 1); |
|
277 |
} |
|
278 |
||
279 |
//--------------------- Infrastructure --------------------------- |
|
280 |
static volatile int passed = 0, failed = 0; |
|
281 |
static void pass() {passed++;} |
|
282 |
static void fail() {failed++; Thread.dumpStack();} |
|
283 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
284 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
285 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
286 |
static void equal(Object x, Object y) { |
|
287 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
288 |
else fail(x + " not equal to " + y);} |
|
289 |
public static void main(String[] args) throws Throwable { |
|
290 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
291 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
292 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
18160
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
293 |
@SuppressWarnings("unchecked") static <T extends Throwable> |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
294 |
void uncheckedThrow(Throwable t) throws T { |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
295 |
throw (T)t; // rely on vacuous cast |
da854405dc59
8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents:
7668
diff
changeset
|
296 |
} |
2 | 297 |
} |