author | dl |
Mon, 12 Sep 2016 13:14:49 -0700 | |
changeset 40817 | 4f5fb115676d |
parent 36233 | f85ed703cf7e |
child 42338 | a60f280f803c |
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 with assistance from members of JCP |
|
30 |
* JSR-166 Expert Group and released to the public domain, as |
|
31 |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
/* |
|
35 |
* @test |
|
36 |
* @bug 8022642 8065320 8129861 |
|
37 |
* @summary Ensure relative sanity when zero core threads |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
38 |
* @library /lib/testlibrary/ |
32991 | 39 |
*/ |
40 |
||
34347 | 41 |
import static java.util.concurrent.TimeUnit.HOURS; |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
42 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
34347 | 43 |
|
44 |
import java.lang.reflect.Field; |
|
45 |
import java.util.concurrent.BlockingQueue; |
|
46 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
47 |
import java.util.concurrent.locks.Condition; |
|
48 |
import java.util.concurrent.locks.ReentrantLock; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
49 |
import java.util.function.BooleanSupplier; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
50 |
import jdk.testlibrary.Utils; |
32991 | 51 |
|
52 |
public class ZeroCoreThreads { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
53 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
54 |
|
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
55 |
static long millisElapsedSince(long startTime) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
56 |
return (System.nanoTime() - startTime) / (1000L * 1000L); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
57 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
58 |
|
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
59 |
static void spinWaitUntil(BooleanSupplier predicate, long timeoutMillis) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
60 |
long startTime = -1L; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
61 |
while (!predicate.getAsBoolean()) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
62 |
if (startTime == -1L) |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
63 |
startTime = System.nanoTime(); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
64 |
else if (millisElapsedSince(startTime) > timeoutMillis) |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
65 |
throw new AssertionError( |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
66 |
String.format("timed out after %s ms", timeoutMillis)); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
67 |
Thread.yield(); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
68 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
69 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
70 |
|
32991 | 71 |
static boolean hasWaiters(ReentrantLock lock, Condition condition) { |
72 |
lock.lock(); |
|
73 |
try { |
|
74 |
return lock.hasWaiters(condition); |
|
75 |
} finally { |
|
76 |
lock.unlock(); |
|
77 |
} |
|
78 |
} |
|
79 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
80 |
static void awaitHasWaiters(ReentrantLock lock, Condition condition, |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
81 |
long timeoutMillis) { |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
82 |
spinWaitUntil(() -> hasWaiters(lock, condition), timeoutMillis); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
83 |
} |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
84 |
|
32991 | 85 |
static <T> T getField(Object x, String fieldName) { |
86 |
try { |
|
87 |
Field field = x.getClass().getDeclaredField(fieldName); |
|
88 |
field.setAccessible(true); |
|
89 |
return (T) field.get(x); |
|
90 |
} catch (ReflectiveOperationException ex) { |
|
91 |
throw new AssertionError(ex); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
void test(String[] args) throws Throwable { |
|
96 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(0); |
|
97 |
try { |
|
98 |
test(p); |
|
99 |
} finally { |
|
100 |
p.shutdownNow(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
101 |
check(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
32991 | 102 |
} |
103 |
} |
|
104 |
||
105 |
void test(ScheduledThreadPoolExecutor p) throws Throwable { |
|
106 |
Runnable dummy = new Runnable() { public void run() { |
|
107 |
throw new AssertionError("shouldn't get here"); }}; |
|
108 |
BlockingQueue q = p.getQueue(); |
|
109 |
ReentrantLock lock = getField(q, "lock"); |
|
110 |
Condition available = getField(q, "available"); |
|
111 |
||
112 |
equal(0, p.getPoolSize()); |
|
113 |
equal(0, p.getLargestPoolSize()); |
|
114 |
equal(0L, p.getTaskCount()); |
|
115 |
equal(0L, p.getCompletedTaskCount()); |
|
116 |
p.schedule(dummy, 1L, HOURS); |
|
117 |
// Ensure one pool thread actually waits in timed queue poll |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
118 |
awaitHasWaiters(lock, available, LONG_DELAY_MS); |
32991 | 119 |
equal(1, p.getPoolSize()); |
120 |
equal(1, p.getLargestPoolSize()); |
|
121 |
equal(1L, p.getTaskCount()); |
|
122 |
equal(0L, p.getCompletedTaskCount()); |
|
123 |
} |
|
124 |
||
125 |
//--------------------- Infrastructure --------------------------- |
|
126 |
volatile int passed = 0, failed = 0; |
|
127 |
void pass() {passed++;} |
|
128 |
void fail() {failed++; Thread.dumpStack();} |
|
129 |
void fail(String msg) {System.err.println(msg); fail();} |
|
130 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
131 |
void check(boolean cond) {if (cond) pass(); else fail();} |
|
132 |
void equal(Object x, Object y) { |
|
133 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
134 |
else fail(x + " not equal to " + y);} |
|
135 |
public static void main(String[] args) throws Throwable { |
|
136 |
new ZeroCoreThreads().instanceMain(args);} |
|
137 |
void instanceMain(String[] args) throws Throwable { |
|
138 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
|
139 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
140 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
141 |
} |