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