author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11832
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, 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 6277663 |
|
27 |
* @summary Test TPE extensibility framework |
|
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.Callable; |
|
36 |
import java.util.concurrent.FutureTask; |
|
37 |
import java.util.concurrent.RunnableFuture; |
|
38 |
import java.util.concurrent.RunnableScheduledFuture; |
|
39 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
40 |
import java.util.concurrent.ThreadPoolExecutor; |
|
41 |
import java.util.concurrent.TimeUnit; |
|
42 |
import java.util.concurrent.atomic.AtomicInteger; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
43 |
import java.util.function.BooleanSupplier; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
44 |
import jdk.testlibrary.Utils; |
2 | 45 |
|
46 |
public class Custom { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
47 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
2 | 48 |
static volatile int passed = 0, failed = 0; |
49 |
static void pass() { passed++; } |
|
50 |
static void fail() { failed++; Thread.dumpStack(); } |
|
51 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
|
52 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
|
53 |
static void equal(Object x, Object y) { |
|
54 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
55 |
else {System.out.println(x + " not equal to " + y); fail(); }} |
|
56 |
||
57 |
private static class CustomTask<V> extends FutureTask<V> { |
|
7518 | 58 |
public static final AtomicInteger births = new AtomicInteger(0); |
2 | 59 |
CustomTask(Callable<V> c) { super(c); births.getAndIncrement(); } |
60 |
CustomTask(Runnable r, V v) { super(r, v); births.getAndIncrement(); } |
|
61 |
} |
|
62 |
||
63 |
private static class CustomTPE extends ThreadPoolExecutor { |
|
64 |
CustomTPE() { |
|
65 |
super(threadCount, threadCount, |
|
66 |
30, TimeUnit.MILLISECONDS, |
|
67 |
new ArrayBlockingQueue<Runnable>(2*threadCount)); |
|
68 |
} |
|
69 |
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
|
70 |
return new CustomTask<V>(c); |
|
71 |
} |
|
72 |
protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { |
|
73 |
return new CustomTask<V>(r, v); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
private static class CustomSTPE extends ScheduledThreadPoolExecutor { |
|
7518 | 78 |
public static final AtomicInteger decorations = new AtomicInteger(0); |
2 | 79 |
CustomSTPE() { |
80 |
super(threadCount); |
|
81 |
} |
|
82 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
83 |
Runnable r, RunnableScheduledFuture<V> task) { |
|
84 |
decorations.getAndIncrement(); |
|
85 |
return task; |
|
86 |
} |
|
87 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
88 |
Callable<V> c, RunnableScheduledFuture<V> task) { |
|
89 |
decorations.getAndIncrement(); |
|
90 |
return task; |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
static int countExecutorThreads() { |
|
95 |
Thread[] threads = new Thread[Thread.activeCount()+100]; |
|
96 |
Thread.enumerate(threads); |
|
97 |
int count = 0; |
|
98 |
for (Thread t : threads) |
|
99 |
if (t != null && t.getName().matches("pool-[0-9]+-thread-[0-9]+")) |
|
100 |
count++; |
|
101 |
return count; |
|
102 |
} |
|
103 |
||
7518 | 104 |
private static final int threadCount = 10; |
2 | 105 |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
106 |
static long millisElapsedSince(long startTime) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
107 |
return (System.nanoTime() - startTime) / (1000L * 1000L); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
108 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
109 |
|
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
110 |
static void spinWaitUntil(BooleanSupplier predicate, long timeoutMillis) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
111 |
long startTime = -1L; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
112 |
while (!predicate.getAsBoolean()) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
113 |
if (startTime == -1L) |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
114 |
startTime = System.nanoTime(); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
115 |
else if (millisElapsedSince(startTime) > timeoutMillis) |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
116 |
throw new AssertionError( |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
117 |
String.format("timed out after %s ms", timeoutMillis)); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
118 |
Thread.yield(); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
119 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
120 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
121 |
|
2 | 122 |
public static void main(String[] args) throws Throwable { |
123 |
CustomTPE tpe = new CustomTPE(); |
|
124 |
equal(tpe.getCorePoolSize(), threadCount); |
|
125 |
equal(countExecutorThreads(), 0); |
|
126 |
for (int i = 0; i < threadCount; i++) |
|
127 |
tpe.submit(new Runnable() { public void run() {}}); |
|
128 |
equal(countExecutorThreads(), threadCount); |
|
129 |
equal(CustomTask.births.get(), threadCount); |
|
130 |
tpe.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
131 |
tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
132 |
spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS); |
2 | 133 |
|
134 |
CustomSTPE stpe = new CustomSTPE(); |
|
135 |
for (int i = 0; i < threadCount; i++) |
|
136 |
stpe.submit(new Runnable() { public void run() {}}); |
|
137 |
equal(CustomSTPE.decorations.get(), threadCount); |
|
138 |
equal(countExecutorThreads(), threadCount); |
|
139 |
stpe.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
140 |
stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
141 |
spinWaitUntil(() -> countExecutorThreads() == 0, LONG_DELAY_MS); |
2 | 142 |
|
143 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
144 |
if (failed > 0) throw new Exception("Some tests failed"); |
|
145 |
} |
|
146 |
} |