author | dl |
Fri, 23 Sep 2016 13:24:33 -0700 | |
changeset 41131 | 87edc8451f8a |
parent 36233 | f85ed703cf7e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7976
diff
changeset
|
2 |
* Copyright (c) 2006, 2011, 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 6399443 |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
27 |
* @summary Check for auto-shutdown and gc of singleThreadExecutors |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
28 |
* @library /lib/testlibrary/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
29 |
* @run main/othervm/timeout=1000 AutoShutdown |
2 | 30 |
* @author Martin Buchholz |
31 |
*/ |
|
32 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
33 |
import static java.util.concurrent.Executors.defaultThreadFactory; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
34 |
import static java.util.concurrent.Executors.newSingleThreadExecutor; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
35 |
|
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
37 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
38 |
import java.lang.ref.WeakReference; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
39 |
import java.util.Arrays; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
40 |
import java.util.concurrent.ConcurrentLinkedQueue; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
41 |
import java.util.concurrent.CountDownLatch; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
42 |
import java.util.concurrent.Executor; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
43 |
import java.util.concurrent.TimeUnit; |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
44 |
import jdk.testlibrary.Utils; |
2 | 45 |
|
46 |
public class AutoShutdown { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
47 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
48 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
49 |
static void await(CountDownLatch latch) throws InterruptedException { |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
32991
diff
changeset
|
50 |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
51 |
throw new AssertionError("timed out waiting for latch"); |
2 | 52 |
} |
53 |
||
54 |
private static void realMain(String[] args) throws Throwable { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
55 |
final Executor[] executors = { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
56 |
newSingleThreadExecutor(), |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
57 |
newSingleThreadExecutor(defaultThreadFactory()), |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
58 |
// TODO: should these executors also auto-shutdown? |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
59 |
//newFixedThreadPool(1), |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
60 |
//newSingleThreadScheduledExecutor(), |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
61 |
//newSingleThreadScheduledExecutor(defaultThreadFactory()), |
7277
4c9b6e4143ea
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
dl
parents:
5506
diff
changeset
|
62 |
}; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
63 |
final ConcurrentLinkedQueue<WeakReference<Thread>> poolThreads |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
64 |
= new ConcurrentLinkedQueue<>(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
65 |
final CountDownLatch threadStarted |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
66 |
= new CountDownLatch(executors.length); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
67 |
final CountDownLatch pleaseProceed |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
68 |
= new CountDownLatch(1); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
69 |
Runnable task = new Runnable() { public void run() { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
70 |
try { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
71 |
poolThreads.add(new WeakReference<>(Thread.currentThread())); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
72 |
threadStarted.countDown(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
73 |
await(pleaseProceed); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
74 |
} catch (Throwable t) { unexpected(t); } |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
75 |
}}; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
76 |
for (Executor executor : executors) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
77 |
executor.execute(task); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
78 |
await(threadStarted); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
79 |
pleaseProceed.countDown(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
80 |
Arrays.fill(executors, null); // make executors unreachable |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
81 |
boolean done = false; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
82 |
for (long timeout = 1L; !done && timeout <= 128L; timeout *= 2) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
83 |
System.gc(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
84 |
done = true; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
85 |
for (WeakReference<Thread> ref : poolThreads) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
86 |
Thread thread = ref.get(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
87 |
if (thread != null) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
88 |
TimeUnit.SECONDS.timedJoin(thread, timeout); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
89 |
if (thread.isAlive()) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
90 |
done = false; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
91 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
92 |
} |
10119
277fd8d9fcda
7057320: test/java/util/concurrent/Executors/AutoShutdown.java failing intermittently
dl
parents:
9035
diff
changeset
|
93 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
94 |
if (!done) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
10119
diff
changeset
|
95 |
throw new AssertionError("pool threads did not terminate"); |
2 | 96 |
} |
97 |
||
98 |
//--------------------- Infrastructure --------------------------- |
|
99 |
static volatile int passed = 0, failed = 0; |
|
100 |
static void pass() {passed++;} |
|
101 |
static void fail() {failed++; Thread.dumpStack();} |
|
102 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
103 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
104 |
static void equal(Object x, Object y) { |
|
105 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
106 |
else fail(x + " not equal to " + y);} |
|
107 |
public static void main(String[] args) throws Throwable { |
|
108 |
try {realMain(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 |
} |