40 |
|
import java.util.concurrent.ExecutionException; |
41 |
|
import java.util.concurrent.Executors; |
42 |
|
import java.util.concurrent.ExecutorService; |
43 |
+ |
import java.util.concurrent.ForkJoinPool; |
44 |
|
import java.util.concurrent.Future; |
45 |
|
import java.util.concurrent.RecursiveAction; |
46 |
|
import java.util.concurrent.RecursiveTask; |
186 |
|
return (regex == null) ? null : Pattern.compile(regex); |
187 |
|
} |
188 |
|
|
189 |
< |
protected void runTest() throws Throwable { |
189 |
> |
public void runBare() throws Throwable { |
190 |
|
if (methodFilter == null |
191 |
< |
|| methodFilter.matcher(toString()).find()) { |
192 |
< |
for (int i = 0; i < runsPerTest; i++) { |
193 |
< |
if (profileTests) |
194 |
< |
runTestProfiled(); |
195 |
< |
else |
196 |
< |
super.runTest(); |
197 |
< |
} |
191 |
> |
|| methodFilter.matcher(toString()).find()) |
192 |
> |
super.runBare(); |
193 |
> |
} |
194 |
> |
|
195 |
> |
protected void runTest() throws Throwable { |
196 |
> |
for (int i = 0; i < runsPerTest; i++) { |
197 |
> |
if (profileTests) |
198 |
> |
runTestProfiled(); |
199 |
> |
else |
200 |
> |
super.runTest(); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
protected void runTestProfiled() throws Throwable { |
205 |
< |
// Warmup run, notably to trigger all needed classloading. |
206 |
< |
super.runTest(); |
203 |
< |
long t0 = System.nanoTime(); |
204 |
< |
try { |
205 |
> |
for (int i = 0; i < 2; i++) { |
206 |
> |
long startTime = System.nanoTime(); |
207 |
|
super.runTest(); |
208 |
< |
} finally { |
209 |
< |
long elapsedMillis = millisElapsedSince(t0); |
210 |
< |
if (elapsedMillis >= profileThreshold) |
208 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
209 |
> |
if (elapsedMillis < profileThreshold) |
210 |
> |
break; |
211 |
> |
// Never report first run of any test; treat it as a |
212 |
> |
// warmup run, notably to trigger all needed classloading, |
213 |
> |
if (i > 0) |
214 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
215 |
|
} |
216 |
|
} |
525 |
|
setDelays(); |
526 |
|
} |
527 |
|
|
528 |
+ |
void tearDownFail(String format, Object... args) { |
529 |
+ |
String msg = toString() + ": " + String.format(format, args); |
530 |
+ |
System.err.println(msg); |
531 |
+ |
printAllStackTraces(); |
532 |
+ |
throw new AssertionFailedError(msg); |
533 |
+ |
} |
534 |
+ |
|
535 |
|
/** |
536 |
|
* Extra checks that get done for all test cases. |
537 |
|
* |
559 |
|
} |
560 |
|
|
561 |
|
if (Thread.interrupted()) |
562 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
562 |
> |
tearDownFail("interrupt status set in main thread"); |
563 |
|
|
564 |
|
checkForkJoinPoolThreadLeaks(); |
565 |
|
} |
568 |
|
* Finds missing try { ... } finally { joinPool(e); } |
569 |
|
*/ |
570 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
571 |
< |
Thread[] survivors = new Thread[5]; |
571 |
> |
Thread[] survivors = new Thread[7]; |
572 |
|
int count = Thread.enumerate(survivors); |
573 |
|
for (int i = 0; i < count; i++) { |
574 |
|
Thread thread = survivors[i]; |
576 |
|
if (name.startsWith("ForkJoinPool-")) { |
577 |
|
// give thread some time to terminate |
578 |
|
thread.join(LONG_DELAY_MS); |
579 |
< |
if (!thread.isAlive()) continue; |
580 |
< |
throw new AssertionFailedError |
581 |
< |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
570 |
< |
toString(), name)); |
579 |
> |
if (thread.isAlive()) |
580 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
581 |
> |
thread); |
582 |
|
} |
583 |
|
} |
584 |
+ |
|
585 |
+ |
if (!ForkJoinPool.commonPool() |
586 |
+ |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
587 |
+ |
tearDownFail("ForkJoin common pool thread stuck"); |
588 |
|
} |
589 |
|
|
590 |
|
/** |
1238 |
|
public static final String TEST_STRING = "a test string"; |
1239 |
|
|
1240 |
|
public static class StringTask implements Callable<String> { |
1241 |
< |
public String call() { return TEST_STRING; } |
1241 |
> |
final String value; |
1242 |
> |
public StringTask() { this(TEST_STRING); } |
1243 |
> |
public StringTask(String value) { this.value = value; } |
1244 |
> |
public String call() { return value; } |
1245 |
|
} |
1246 |
|
|
1247 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1254 |
|
}}; |
1255 |
|
} |
1256 |
|
|
1257 |
+ |
public Runnable countDowner(final CountDownLatch latch) { |
1258 |
+ |
return new CheckedRunnable() { |
1259 |
+ |
public void realRun() throws InterruptedException { |
1260 |
+ |
latch.countDown(); |
1261 |
+ |
}}; |
1262 |
+ |
} |
1263 |
+ |
|
1264 |
|
public Runnable awaiter(final CountDownLatch latch) { |
1265 |
|
return new CheckedRunnable() { |
1266 |
|
public void realRun() throws InterruptedException { |