ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/concurrent/ThreadPoolExecutor/FlakyThreadFactory.java
Revision: 1.5
Committed: Sat Oct 21 04:13:12 2017 UTC (6 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +8 -6 lines
Log Message:
suppress errorprone warnings

File Contents

# Content
1 /*
2 * Written by Martin Buchholz and Doug Lea with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 /*
9 * @test
10 * @summary Should be able to shutdown a pool when worker creation failed.
11 * @library /lib/testlibrary/
12 */
13
14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15
16 import java.util.concurrent.LinkedBlockingQueue;
17 import java.util.concurrent.ThreadFactory;
18 import java.util.concurrent.ThreadPoolExecutor;
19 import java.util.concurrent.TimeUnit;
20 import jdk.testlibrary.Utils;
21
22 public class FlakyThreadFactory {
23 static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
24
25 void test(String[] args) throws Throwable {
26 test(NullPointerException.class,
27 new ThreadFactory() {
28 public Thread newThread(Runnable r) {
29 throw new NullPointerException();
30 }});
31 test(OutOfMemoryError.class,
32 new ThreadFactory() {
33 @SuppressWarnings("DeadThread")
34 public Thread newThread(Runnable r) {
35 // We expect this to throw OOME, but ...
36 new Thread(null, r, "a natural OOME", 1L << 60);
37 // """On some platforms, the value of the stackSize
38 // parameter may have no effect whatsoever."""
39 throw new OutOfMemoryError("artificial OOME");
40 }});
41 test(null,
42 new ThreadFactory() {
43 public Thread newThread(Runnable r) {
44 return null;
45 }});
46 }
47
48 void test(final Class<?> exceptionClass,
49 final ThreadFactory failingThreadFactory)
50 throws Throwable {
51 ThreadFactory flakyThreadFactory = new ThreadFactory() {
52 int seq = 0;
53 public Thread newThread(Runnable r) {
54 if (seq++ < 4)
55 return new Thread(r);
56 else
57 return failingThreadFactory.newThread(r);
58 }};
59 ThreadPoolExecutor pool =
60 new ThreadPoolExecutor(10, 10,
61 0L, TimeUnit.SECONDS,
62 new LinkedBlockingQueue(),
63 flakyThreadFactory);
64 try {
65 for (int i = 0; i < 8; i++)
66 pool.submit(new Runnable() { public void run() {} });
67 check(exceptionClass == null);
68 } catch (Throwable t) {
69 /* t.printStackTrace(); */
70 check(exceptionClass.isInstance(t));
71 }
72 pool.shutdown();
73 check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
74 }
75
76 //--------------------- Infrastructure ---------------------------
77 volatile int passed = 0, failed = 0;
78 void pass() {passed++;}
79 void fail() {failed++; Thread.dumpStack();}
80 void fail(String msg) {System.err.println(msg); fail();}
81 void unexpected(Throwable t) {failed++; t.printStackTrace();}
82 void check(boolean cond) {if (cond) pass(); else fail();}
83 void equal(Object x, Object y) {
84 if (x == null ? y == null : x.equals(y)) pass();
85 else fail(x + " not equal to " + y);}
86 public static void main(String[] args) throws Throwable {
87 new FlakyThreadFactory().instanceMain(args);}
88 public void instanceMain(String[] args) throws Throwable {
89 try {test(args);} catch (Throwable t) {unexpected(t);}
90 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
91 if (failed > 0) throw new AssertionError("Some tests failed");}
92 }