author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34516 | 3bcd03288484 |
child 40816 | 9756dfcba32e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
34516
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
2 |
* Copyright (c) 2007, 2015, 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 6450200 |
|
27 |
* @summary Test proper handling of pool state changes |
|
34516
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
28 |
* @library /lib/testlibrary/ |
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
29 |
* @build jdk.testlibrary.RandomFactory |
2 | 30 |
* @run main/othervm ConfigChanges |
34516
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
31 |
* @key randomness intermittent |
2 | 32 |
* @author Martin Buchholz |
33 |
*/ |
|
34 |
||
34347 | 35 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
36 |
import static java.util.concurrent.TimeUnit.MINUTES; |
|
37 |
import static java.util.concurrent.TimeUnit.SECONDS; |
|
38 |
||
39 |
import java.security.Permission; |
|
40 |
import java.util.Random; |
|
41 |
import java.util.concurrent.ArrayBlockingQueue; |
|
42 |
import java.util.concurrent.CyclicBarrier; |
|
43 |
import java.util.concurrent.ExecutorService; |
|
44 |
import java.util.concurrent.RejectedExecutionException; |
|
45 |
import java.util.concurrent.ThreadFactory; |
|
46 |
import java.util.concurrent.ThreadPoolExecutor; |
|
47 |
import java.util.concurrent.atomic.AtomicInteger; |
|
34516
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
48 |
import jdk.testlibrary.RandomFactory; |
2 | 49 |
|
50 |
public class ConfigChanges { |
|
7518 | 51 |
static final ThreadGroup tg = new ThreadGroup("pool"); |
2 | 52 |
|
34516
3bcd03288484
8144880: Instrument intermittently failing test ConfigChanges.java
darcy
parents:
34347
diff
changeset
|
53 |
static final Random rnd = RandomFactory.getRandom(); |
2 | 54 |
|
55 |
static void report(ThreadPoolExecutor tpe) { |
|
56 |
try { |
|
57 |
System.out.printf( |
|
58 |
"active=%d submitted=%d completed=%d queued=%d sizes=%d/%d/%d%n", |
|
59 |
tg.activeCount(), |
|
60 |
tpe.getTaskCount(), |
|
61 |
tpe.getCompletedTaskCount(), |
|
62 |
tpe.getQueue().size(), |
|
63 |
tpe.getPoolSize(), |
|
64 |
tpe.getCorePoolSize(), |
|
65 |
tpe.getMaximumPoolSize()); |
|
66 |
} catch (Throwable t) { unexpected(t); } |
|
67 |
} |
|
68 |
||
69 |
static void report(String label, ThreadPoolExecutor tpe) { |
|
70 |
System.out.printf("%10s ", label); |
|
71 |
report(tpe); |
|
72 |
} |
|
73 |
||
74 |
static class PermissiveSecurityManger extends SecurityManager { |
|
75 |
public void checkPermission(Permission p) { /* bien sur, Monsieur */ } |
|
76 |
} |
|
77 |
||
78 |
static void checkShutdown(final ExecutorService es) { |
|
79 |
final Runnable nop = new Runnable() {public void run() {}}; |
|
80 |
try { |
|
81 |
if (new Random().nextBoolean()) { |
|
82 |
check(es.isShutdown()); |
|
83 |
if (es instanceof ThreadPoolExecutor) |
|
84 |
check(((ThreadPoolExecutor) es).isTerminating() |
|
85 |
|| es.isTerminated()); |
|
86 |
THROWS(RejectedExecutionException.class, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
87 |
() -> es.execute(nop)); |
2 | 88 |
} |
89 |
} catch (Throwable t) { unexpected(t); } |
|
90 |
} |
|
91 |
||
92 |
static void checkTerminated(final ThreadPoolExecutor tpe) { |
|
93 |
try { |
|
94 |
checkShutdown(tpe); |
|
95 |
check(tpe.getQueue().isEmpty()); |
|
96 |
check(tpe.isTerminated()); |
|
97 |
check(! tpe.isTerminating()); |
|
98 |
equal(tpe.getActiveCount(), 0); |
|
99 |
equal(tpe.getPoolSize(), 0); |
|
100 |
equal(tpe.getTaskCount(), tpe.getCompletedTaskCount()); |
|
34347 | 101 |
check(tpe.awaitTermination(0L, SECONDS)); |
2 | 102 |
} catch (Throwable t) { unexpected(t); } |
103 |
} |
|
104 |
||
105 |
static Runnable waiter(final CyclicBarrier barrier) { |
|
106 |
return new Runnable() { public void run() { |
|
107 |
try { barrier.await(); barrier.await(); } |
|
108 |
catch (Throwable t) { unexpected(t); }}}; |
|
109 |
} |
|
110 |
||
111 |
static volatile Runnable runnableDuJour; |
|
112 |
||
113 |
private static void realMain(String[] args) throws Throwable { |
|
114 |
if (rnd.nextBoolean()) |
|
115 |
System.setSecurityManager(new PermissiveSecurityManger()); |
|
116 |
||
117 |
final boolean prestart = rnd.nextBoolean(); |
|
118 |
||
119 |
final Thread.UncaughtExceptionHandler handler |
|
120 |
= new Thread.UncaughtExceptionHandler() { |
|
121 |
public void uncaughtException(Thread t, Throwable e) { |
|
122 |
check(! Thread.currentThread().isInterrupted()); |
|
123 |
unexpected(e); |
|
124 |
}}; |
|
125 |
||
126 |
final int n = 3; |
|
127 |
final ThreadPoolExecutor tpe |
|
128 |
= new ThreadPoolExecutor(n, 3*n, |
|
129 |
3L, MINUTES, |
|
130 |
new ArrayBlockingQueue<Runnable>(3*n)); |
|
131 |
tpe.setThreadFactory(new ThreadFactory() { |
|
132 |
public Thread newThread(Runnable r) { |
|
133 |
Thread t = new Thread(tg, r); |
|
134 |
t.setUncaughtExceptionHandler(handler); |
|
135 |
return t; |
|
136 |
}}); |
|
137 |
||
138 |
if (prestart) { |
|
139 |
tpe.prestartAllCoreThreads(); |
|
140 |
equal(tg.activeCount(), n); |
|
141 |
equal(tg.activeCount(), tpe.getCorePoolSize()); |
|
142 |
} |
|
143 |
||
144 |
final Runnable runRunnableDuJour = |
|
145 |
new Runnable() { public void run() { |
|
146 |
// Delay choice of action till last possible moment. |
|
147 |
runnableDuJour.run(); }}; |
|
148 |
final CyclicBarrier pumpedUp = new CyclicBarrier(3*n + 1); |
|
149 |
runnableDuJour = waiter(pumpedUp); |
|
150 |
||
151 |
if (prestart) { |
|
152 |
for (int i = 0; i < 1*n; i++) |
|
153 |
tpe.execute(runRunnableDuJour); |
|
154 |
// Wait for prestarted threads to dequeue their initial tasks. |
|
155 |
while (! tpe.getQueue().isEmpty()) |
|
156 |
Thread.sleep(10); |
|
157 |
for (int i = 0; i < 5*n; i++) |
|
158 |
tpe.execute(runRunnableDuJour); |
|
159 |
} else { |
|
160 |
for (int i = 0; i < 6*n; i++) |
|
161 |
tpe.execute(runRunnableDuJour); |
|
162 |
} |
|
163 |
||
164 |
//report("submitted", tpe); |
|
165 |
pumpedUp.await(); |
|
166 |
equal(tg.activeCount(), 3*n); |
|
167 |
equal(tg.activeCount(), tpe.getMaximumPoolSize()); |
|
168 |
equal(tpe.getCorePoolSize(), n); |
|
169 |
//report("pumped up", tpe); |
|
170 |
equal(tpe.getMaximumPoolSize(), 3*n); |
|
171 |
tpe.setMaximumPoolSize(4*n); |
|
172 |
equal(tpe.getMaximumPoolSize(), 4*n); |
|
173 |
//report("pumped up2", tpe); |
|
174 |
final CyclicBarrier pumpedUp2 = new CyclicBarrier(n + 1); |
|
175 |
runnableDuJour = waiter(pumpedUp2); |
|
176 |
for (int i = 0; i < 1*n; i++) |
|
177 |
tpe.execute(runRunnableDuJour); |
|
178 |
pumpedUp2.await(); |
|
179 |
equal(tg.activeCount(), 4*n); |
|
180 |
equal(tg.activeCount(), tpe.getMaximumPoolSize()); |
|
181 |
equal(tpe.getCompletedTaskCount(), 0L); |
|
182 |
//report("pumped up2", tpe); |
|
183 |
runnableDuJour = new Runnable() { public void run() {}}; |
|
184 |
||
185 |
tpe.setMaximumPoolSize(2*n); |
|
186 |
//report("after set", tpe); |
|
187 |
||
188 |
pumpedUp2.await(); |
|
189 |
pumpedUp.await(); |
|
190 |
||
191 |
// while (tg.activeCount() != n && |
|
192 |
// tg.activeCount() != n) |
|
193 |
// Thread.sleep(10); |
|
194 |
// equal(tg.activeCount(), n); |
|
195 |
// equal(tg.activeCount(), tpe.getCorePoolSize()); |
|
196 |
||
197 |
while (tg.activeCount() != 2*n && |
|
198 |
tg.activeCount() != 2*n) |
|
199 |
Thread.sleep(10); |
|
200 |
equal(tg.activeCount(), 2*n); |
|
201 |
equal(tg.activeCount(), tpe.getMaximumPoolSize()); |
|
202 |
||
203 |
||
204 |
//report("draining", tpe); |
|
205 |
while (tpe.getCompletedTaskCount() < 7*n && |
|
206 |
tpe.getCompletedTaskCount() < 7*n) |
|
207 |
Thread.sleep(10); |
|
208 |
||
209 |
//equal(tg.activeCount(), n); |
|
210 |
//equal(tg.activeCount(), tpe.getCorePoolSize()); |
|
211 |
equal(tg.activeCount(), 2*n); |
|
212 |
equal(tg.activeCount(), tpe.getMaximumPoolSize()); |
|
213 |
||
214 |
equal(tpe.getTaskCount(), 7L*n); |
|
215 |
equal(tpe.getCompletedTaskCount(), 7L*n); |
|
216 |
||
217 |
equal(tpe.getKeepAliveTime(MINUTES), 3L); |
|
218 |
tpe.setKeepAliveTime(7L, MILLISECONDS); |
|
219 |
equal(tpe.getKeepAliveTime(MILLISECONDS), 7L); |
|
220 |
while (tg.activeCount() > n && |
|
221 |
tg.activeCount() > n) |
|
222 |
Thread.sleep(10); |
|
223 |
equal(tg.activeCount(), n); |
|
224 |
||
225 |
//report("idle", tpe); |
|
226 |
check(! tpe.allowsCoreThreadTimeOut()); |
|
227 |
tpe.allowCoreThreadTimeOut(true); |
|
228 |
check(tpe.allowsCoreThreadTimeOut()); |
|
229 |
while (tg.activeCount() > 0 && |
|
230 |
tg.activeCount() > 0) |
|
231 |
Thread.sleep(10); |
|
232 |
equal(tg.activeCount(), 0); |
|
233 |
||
234 |
//report("idle", tpe); |
|
235 |
||
236 |
tpe.shutdown(); |
|
237 |
checkShutdown(tpe); |
|
238 |
check(tpe.awaitTermination(3L, MINUTES)); |
|
239 |
checkTerminated(tpe); |
|
240 |
} |
|
241 |
||
242 |
//--------------------- Infrastructure --------------------------- |
|
243 |
static volatile int passed = 0, failed = 0; |
|
244 |
static void pass() {passed++;} |
|
245 |
static void fail() {failed++; Thread.dumpStack();} |
|
246 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
247 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
248 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
249 |
static void equal(Object x, Object y) { |
|
250 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
251 |
else fail(x + " not equal to " + y);} |
|
252 |
public static void main(String[] args) throws Throwable { |
|
253 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
254 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
255 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
256 |
interface Fun {void f() throws Throwable;} |
2 | 257 |
static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
258 |
for (Fun f : fs) |
|
259 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
260 |
catch (Throwable t) { |
|
261 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
262 |
else unexpected(t);}} |
|
263 |
} |