author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
child 41129 | e54fb9880260 |
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) 2005, 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 6233235 6268386 |
|
27 |
* @summary Test allowsCoreThreadTimeOut |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
28 |
* @library /lib/testlibrary/ |
2 | 29 |
* @author Martin Buchholz |
30 |
*/ |
|
31 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
32 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
33 |
|
34347 | 34 |
import java.util.concurrent.ArrayBlockingQueue; |
35 |
import java.util.concurrent.BlockingQueue; |
|
36 |
import java.util.concurrent.Executors; |
|
37 |
import java.util.concurrent.ThreadFactory; |
|
38 |
import java.util.concurrent.ThreadPoolExecutor; |
|
39 |
import java.util.concurrent.TimeUnit; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
40 |
import jdk.testlibrary.Utils; |
2 | 41 |
|
42 |
public class CoreThreadTimeOut { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
43 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
44 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
45 |
static class IdentifiableThreadFactory implements ThreadFactory { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
46 |
static ThreadFactory defaultThreadFactory |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
47 |
= Executors.defaultThreadFactory(); |
2 | 48 |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
49 |
public Thread newThread(Runnable r) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
50 |
Thread t = defaultThreadFactory.newThread(r); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
51 |
t.setName("CoreThreadTimeOut-" + t.getName()); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
52 |
return t; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
53 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
54 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
55 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
56 |
int countExecutorThreads() { |
2 | 57 |
Thread[] threads = new Thread[Thread.activeCount()+100]; |
58 |
Thread.enumerate(threads); |
|
59 |
int count = 0; |
|
60 |
for (Thread t : threads) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
61 |
if (t != null && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
62 |
t.getName().matches |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
63 |
("CoreThreadTimeOut-pool-[0-9]+-thread-[0-9]+")) |
2 | 64 |
count++; |
65 |
return count; |
|
66 |
} |
|
67 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
68 |
static long millisElapsedSince(long startTime) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
69 |
return (System.nanoTime() - startTime) / (1000L * 1000L); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
70 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
71 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
72 |
void test(String[] args) throws Throwable { |
2 | 73 |
final int threadCount = 10; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
74 |
final int timeoutMillis = 30; |
2 | 75 |
BlockingQueue<Runnable> q |
76 |
= new ArrayBlockingQueue<Runnable>(2*threadCount); |
|
77 |
ThreadPoolExecutor tpe |
|
78 |
= new ThreadPoolExecutor(threadCount, threadCount, |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
79 |
timeoutMillis, TimeUnit.MILLISECONDS, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
80 |
q, new IdentifiableThreadFactory()); |
2 | 81 |
equal(tpe.getCorePoolSize(), threadCount); |
82 |
check(! tpe.allowsCoreThreadTimeOut()); |
|
83 |
tpe.allowCoreThreadTimeOut(true); |
|
84 |
check(tpe.allowsCoreThreadTimeOut()); |
|
85 |
equal(countExecutorThreads(), 0); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
86 |
long t0 = System.nanoTime(); |
2 | 87 |
for (int i = 0; i < threadCount; i++) |
88 |
tpe.submit(new Runnable() { public void run() {}}); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
89 |
int count = countExecutorThreads(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
90 |
if (millisElapsedSince(t0) < timeoutMillis) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
91 |
equal(count, threadCount); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
92 |
while (countExecutorThreads() > 0 && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
93 |
millisElapsedSince(t0) < 10 * 1000); |
2 | 94 |
equal(countExecutorThreads(), 0); |
95 |
tpe.shutdown(); |
|
96 |
check(tpe.allowsCoreThreadTimeOut()); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
97 |
check(tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
2 | 98 |
|
99 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
100 |
if (failed > 0) throw new Exception("Some tests failed"); |
|
101 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
102 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
103 |
//--------------------- Infrastructure --------------------------- |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
104 |
volatile int passed = 0, failed = 0; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
105 |
void pass() {passed++;} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
106 |
void fail() {failed++; Thread.dumpStack();} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
107 |
void fail(String msg) {System.err.println(msg); fail();} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
108 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
109 |
void check(boolean cond) {if (cond) pass(); else fail();} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
110 |
void equal(Object x, Object y) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
111 |
if (x == null ? y == null : x.equals(y)) pass(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
112 |
else fail(x + " not equal to " + y);} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
113 |
public static void main(String[] args) throws Throwable { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
114 |
new CoreThreadTimeOut().instanceMain(args);} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
115 |
public void instanceMain(String[] args) throws Throwable { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
116 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
117 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
118 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
2 | 119 |
} |