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