32991
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation.
|
|
7 |
*
|
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
12 |
* accompanied this code).
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License version
|
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 |
*
|
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
19 |
* or visit www.oracle.com if you need additional information or have any
|
|
20 |
* questions.
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* This file is available under and governed by the GNU General Public
|
|
25 |
* License version 2 only, as published by the Free Software Foundation.
|
|
26 |
* However, the following notice accompanied the original version of this
|
|
27 |
* file:
|
|
28 |
*
|
|
29 |
* Written by Martin Buchholz and Doug Lea with assistance from
|
|
30 |
* members of JCP JSR-166 Expert Group and released to the public
|
|
31 |
* domain, as explained at
|
|
32 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
33 |
*/
|
|
34 |
|
|
35 |
/*
|
|
36 |
* @test
|
|
37 |
* @summary Should be able to shutdown a pool when worker creation failed.
|
|
38 |
*/
|
|
39 |
|
34347
|
40 |
import java.util.concurrent.LinkedBlockingQueue;
|
|
41 |
import java.util.concurrent.ThreadFactory;
|
|
42 |
import java.util.concurrent.ThreadPoolExecutor;
|
|
43 |
import java.util.concurrent.TimeUnit;
|
32991
|
44 |
|
|
45 |
public class FlakyThreadFactory {
|
|
46 |
void test(String[] args) throws Throwable {
|
|
47 |
test(NullPointerException.class,
|
|
48 |
new ThreadFactory() {
|
|
49 |
public Thread newThread(Runnable r) {
|
|
50 |
throw new NullPointerException();
|
|
51 |
}});
|
|
52 |
test(OutOfMemoryError.class,
|
|
53 |
new ThreadFactory() {
|
|
54 |
public Thread newThread(Runnable r) {
|
|
55 |
new Thread(null, r, "a natural OOME", 1L << 60);
|
|
56 |
// """On some platforms, the value of the stackSize
|
|
57 |
// parameter may have no effect whatsoever."""
|
|
58 |
throw new OutOfMemoryError("artificial OOME");
|
|
59 |
}});
|
|
60 |
test(null,
|
|
61 |
new ThreadFactory() {
|
|
62 |
public Thread newThread(Runnable r) {
|
|
63 |
return null;
|
|
64 |
}});
|
|
65 |
}
|
|
66 |
|
|
67 |
void test(final Class<?> exceptionClass,
|
|
68 |
final ThreadFactory failingThreadFactory)
|
|
69 |
throws Throwable {
|
|
70 |
ThreadFactory flakyThreadFactory = new ThreadFactory() {
|
|
71 |
int seq = 0;
|
|
72 |
public Thread newThread(Runnable r) {
|
|
73 |
if (seq++ < 4)
|
|
74 |
return new Thread(r);
|
|
75 |
else
|
|
76 |
return failingThreadFactory.newThread(r);
|
|
77 |
}};
|
|
78 |
ThreadPoolExecutor pool =
|
|
79 |
new ThreadPoolExecutor(10, 10,
|
|
80 |
0L, TimeUnit.SECONDS,
|
|
81 |
new LinkedBlockingQueue(),
|
|
82 |
flakyThreadFactory);
|
|
83 |
try {
|
|
84 |
for (int i = 0; i < 8; i++)
|
|
85 |
pool.submit(new Runnable() { public void run() {} });
|
|
86 |
check(exceptionClass == null);
|
|
87 |
} catch (Throwable t) {
|
|
88 |
/* t.printStackTrace(); */
|
|
89 |
check(exceptionClass.isInstance(t));
|
|
90 |
}
|
|
91 |
pool.shutdown();
|
|
92 |
check(pool.awaitTermination(10L, TimeUnit.SECONDS));
|
|
93 |
}
|
|
94 |
|
|
95 |
//--------------------- Infrastructure ---------------------------
|
|
96 |
volatile int passed = 0, failed = 0;
|
|
97 |
void pass() {passed++;}
|
|
98 |
void fail() {failed++; Thread.dumpStack();}
|
|
99 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
100 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
101 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
102 |
void equal(Object x, Object y) {
|
|
103 |
if (x == null ? y == null : x.equals(y)) pass();
|
|
104 |
else fail(x + " not equal to " + y);}
|
|
105 |
public static void main(String[] args) throws Throwable {
|
|
106 |
new FlakyThreadFactory().instanceMain(args);}
|
|
107 |
public void instanceMain(String[] args) throws Throwable {
|
|
108 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
109 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
110 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
111 |
}
|