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 |
|
|
40 |
import java.util.concurrent.*;
|
|
41 |
|
|
42 |
public class FlakyThreadFactory {
|
|
43 |
void test(String[] args) throws Throwable {
|
|
44 |
test(NullPointerException.class,
|
|
45 |
new ThreadFactory() {
|
|
46 |
public Thread newThread(Runnable r) {
|
|
47 |
throw new NullPointerException();
|
|
48 |
}});
|
|
49 |
test(OutOfMemoryError.class,
|
|
50 |
new ThreadFactory() {
|
|
51 |
public Thread newThread(Runnable r) {
|
|
52 |
new Thread(null, r, "a natural OOME", 1L << 60);
|
|
53 |
// """On some platforms, the value of the stackSize
|
|
54 |
// parameter may have no effect whatsoever."""
|
|
55 |
throw new OutOfMemoryError("artificial OOME");
|
|
56 |
}});
|
|
57 |
test(null,
|
|
58 |
new ThreadFactory() {
|
|
59 |
public Thread newThread(Runnable r) {
|
|
60 |
return null;
|
|
61 |
}});
|
|
62 |
}
|
|
63 |
|
|
64 |
void test(final Class<?> exceptionClass,
|
|
65 |
final ThreadFactory failingThreadFactory)
|
|
66 |
throws Throwable {
|
|
67 |
ThreadFactory flakyThreadFactory = new ThreadFactory() {
|
|
68 |
int seq = 0;
|
|
69 |
public Thread newThread(Runnable r) {
|
|
70 |
if (seq++ < 4)
|
|
71 |
return new Thread(r);
|
|
72 |
else
|
|
73 |
return failingThreadFactory.newThread(r);
|
|
74 |
}};
|
|
75 |
ThreadPoolExecutor pool =
|
|
76 |
new ThreadPoolExecutor(10, 10,
|
|
77 |
0L, TimeUnit.SECONDS,
|
|
78 |
new LinkedBlockingQueue(),
|
|
79 |
flakyThreadFactory);
|
|
80 |
try {
|
|
81 |
for (int i = 0; i < 8; i++)
|
|
82 |
pool.submit(new Runnable() { public void run() {} });
|
|
83 |
check(exceptionClass == null);
|
|
84 |
} catch (Throwable t) {
|
|
85 |
/* t.printStackTrace(); */
|
|
86 |
check(exceptionClass.isInstance(t));
|
|
87 |
}
|
|
88 |
pool.shutdown();
|
|
89 |
check(pool.awaitTermination(10L, TimeUnit.SECONDS));
|
|
90 |
}
|
|
91 |
|
|
92 |
//--------------------- Infrastructure ---------------------------
|
|
93 |
volatile int passed = 0, failed = 0;
|
|
94 |
void pass() {passed++;}
|
|
95 |
void fail() {failed++; Thread.dumpStack();}
|
|
96 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
97 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
98 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
99 |
void equal(Object x, Object y) {
|
|
100 |
if (x == null ? y == null : x.equals(y)) pass();
|
|
101 |
else fail(x + " not equal to " + y);}
|
|
102 |
public static void main(String[] args) throws Throwable {
|
|
103 |
new FlakyThreadFactory().instanceMain(args);}
|
|
104 |
public void instanceMain(String[] args) throws Throwable {
|
|
105 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
106 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
107 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
108 |
}
|