7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MINUTES; |
11 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
12 |
|
|
13 |
|
import java.io.ByteArrayInputStream; |
16 |
|
import java.io.ObjectOutputStream; |
17 |
|
import java.lang.management.ManagementFactory; |
18 |
|
import java.lang.management.ThreadInfo; |
19 |
+ |
import java.lang.management.ThreadMXBean; |
20 |
|
import java.lang.reflect.Constructor; |
21 |
|
import java.lang.reflect.Method; |
22 |
|
import java.lang.reflect.Modifier; |
23 |
+ |
import java.nio.file.Files; |
24 |
+ |
import java.nio.file.Paths; |
25 |
|
import java.security.CodeSource; |
26 |
|
import java.security.Permission; |
27 |
|
import java.security.PermissionCollection; |
41 |
|
import java.util.concurrent.Callable; |
42 |
|
import java.util.concurrent.CountDownLatch; |
43 |
|
import java.util.concurrent.CyclicBarrier; |
44 |
+ |
import java.util.concurrent.ExecutionException; |
45 |
+ |
import java.util.concurrent.Executors; |
46 |
|
import java.util.concurrent.ExecutorService; |
47 |
+ |
import java.util.concurrent.ForkJoinPool; |
48 |
|
import java.util.concurrent.Future; |
49 |
|
import java.util.concurrent.RecursiveAction; |
50 |
|
import java.util.concurrent.RecursiveTask; |
54 |
|
import java.util.concurrent.ThreadPoolExecutor; |
55 |
|
import java.util.concurrent.TimeoutException; |
56 |
|
import java.util.concurrent.atomic.AtomicReference; |
57 |
+ |
import java.util.regex.Matcher; |
58 |
|
import java.util.regex.Pattern; |
59 |
|
|
60 |
|
import junit.framework.AssertionFailedError; |
73 |
|
* |
74 |
|
* <ol> |
75 |
|
* |
76 |
< |
* <li> All assertions in code running in generated threads must use |
76 |
> |
* <li>All assertions in code running in generated threads must use |
77 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
78 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
79 |
|
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
80 |
|
* particularly recommended) for other code to use these forms too. |
81 |
|
* Only the most typically used JUnit assertion methods are defined |
82 |
< |
* this way, but enough to live with.</li> |
82 |
> |
* this way, but enough to live with. |
83 |
|
* |
84 |
< |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
84 |
> |
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
85 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
86 |
|
* them. These methods are used to clear and check for thread |
87 |
< |
* assertion failures.</li> |
87 |
> |
* assertion failures. |
88 |
|
* |
89 |
|
* <li>All delays and timeouts must use one of the constants {@code |
90 |
|
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
95 |
|
* is always discriminable as larger than SHORT and smaller than |
96 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
97 |
|
* but even so, if there is ever any doubt, they can all be increased |
98 |
< |
* in one spot to rerun tests on slower platforms.</li> |
98 |
> |
* in one spot to rerun tests on slower platforms. |
99 |
|
* |
100 |
< |
* <li> All threads generated must be joined inside each test case |
100 |
> |
* <li>All threads generated must be joined inside each test case |
101 |
|
* method (or {@code fail} to do so) before returning from the |
102 |
|
* method. The {@code joinPool} method can be used to do this when |
103 |
< |
* using Executors.</li> |
103 |
> |
* using Executors. |
104 |
|
* |
105 |
|
* </ol> |
106 |
|
* |
107 |
|
* <p><b>Other notes</b> |
108 |
|
* <ul> |
109 |
|
* |
110 |
< |
* <li> Usually, there is one testcase method per JSR166 method |
110 |
> |
* <li>Usually, there is one testcase method per JSR166 method |
111 |
|
* covering "normal" operation, and then as many exception-testing |
112 |
|
* methods as there are exceptions the method can throw. Sometimes |
113 |
|
* there are multiple tests per JSR166 method when the different |
114 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
115 |
|
* cover multiple methods when they cannot be tested in |
116 |
< |
* isolation.</li> |
116 |
> |
* isolation. |
117 |
|
* |
118 |
< |
* <li> The documentation style for testcases is to provide as javadoc |
118 |
> |
* <li>The documentation style for testcases is to provide as javadoc |
119 |
|
* a simple sentence or two describing the property that the testcase |
120 |
|
* method purports to test. The javadocs do not say anything about how |
121 |
< |
* the property is tested. To find out, read the code.</li> |
121 |
> |
* the property is tested. To find out, read the code. |
122 |
|
* |
123 |
< |
* <li> These tests are "conformance tests", and do not attempt to |
123 |
> |
* <li>These tests are "conformance tests", and do not attempt to |
124 |
|
* test throughput, latency, scalability or other performance factors |
125 |
|
* (see the separate "jtreg" tests for a set intended to check these |
126 |
|
* for the most central aspects of functionality.) So, most tests use |
127 |
|
* the smallest sensible numbers of threads, collection sizes, etc |
128 |
< |
* needed to check basic conformance.</li> |
128 |
> |
* needed to check basic conformance. |
129 |
|
* |
130 |
|
* <li>The test classes currently do not declare inclusion in |
131 |
|
* any particular package to simplify things for people integrating |
132 |
< |
* them in TCK test suites.</li> |
132 |
> |
* them in TCK test suites. |
133 |
|
* |
134 |
< |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
135 |
< |
* runs all JSR166 unit tests.</li> |
134 |
> |
* <li>As a convenience, the {@code main} of this class (JSR166TestCase) |
135 |
> |
* runs all JSR166 unit tests. |
136 |
|
* |
137 |
|
* </ul> |
138 |
|
*/ |
191 |
|
return (regex == null) ? null : Pattern.compile(regex); |
192 |
|
} |
193 |
|
|
194 |
< |
protected void runTest() throws Throwable { |
194 |
> |
// Instrumentation to debug very rare, but very annoying hung test runs. |
195 |
> |
static volatile TestCase currentTestCase; |
196 |
> |
static volatile int currentRun = 0; |
197 |
> |
static { |
198 |
> |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
199 |
> |
// avoid spurious reports with enormous runsPerTest |
200 |
> |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
201 |
> |
for (TestCase lastTestCase = currentTestCase;;) { |
202 |
> |
try { MINUTES.sleep(timeoutMinutes); } |
203 |
> |
catch (InterruptedException unexpected) { break; } |
204 |
> |
if (lastTestCase == currentTestCase) { |
205 |
> |
System.err.printf( |
206 |
> |
"Looks like we're stuck running test: %s (%d/%d)%n", |
207 |
> |
lastTestCase, currentRun, runsPerTest); |
208 |
> |
System.err.println |
209 |
> |
("Looks like we're stuck running test: " |
210 |
> |
+ lastTestCase + " (" + currentRun + "/" + runsPerTest + ")"); |
211 |
> |
System.err.println("availableProcessors=" + |
212 |
> |
Runtime.getRuntime().availableProcessors()); |
213 |
> |
System.err.printf("cpu model = %s%n", cpuModel()); |
214 |
> |
dumpTestThreads(); |
215 |
> |
// one stack dump is probably enough; more would be spam |
216 |
> |
break; |
217 |
> |
} |
218 |
> |
lastTestCase = currentTestCase; |
219 |
> |
}}}; |
220 |
> |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
221 |
> |
thread.setDaemon(true); |
222 |
> |
thread.start(); |
223 |
> |
} |
224 |
> |
|
225 |
> |
public static String cpuModel() { |
226 |
> |
try { |
227 |
> |
Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
228 |
> |
.matcher(new String( |
229 |
> |
Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
230 |
> |
matcher.find(); |
231 |
> |
return matcher.group(1); |
232 |
> |
} catch (Exception ex) { return null; } |
233 |
> |
} |
234 |
> |
|
235 |
> |
public void runBare() throws Throwable { |
236 |
> |
currentTestCase = this; |
237 |
|
if (methodFilter == null |
238 |
< |
|| methodFilter.matcher(toString()).find()) { |
239 |
< |
for (int i = 0; i < runsPerTest; i++) { |
240 |
< |
if (profileTests) |
241 |
< |
runTestProfiled(); |
242 |
< |
else |
243 |
< |
super.runTest(); |
244 |
< |
} |
238 |
> |
|| methodFilter.matcher(toString()).find()) |
239 |
> |
super.runBare(); |
240 |
> |
} |
241 |
> |
|
242 |
> |
protected void runTest() throws Throwable { |
243 |
> |
for (int i = 0; i < runsPerTest; i++) { |
244 |
> |
currentRun = i; |
245 |
> |
if (profileTests) |
246 |
> |
runTestProfiled(); |
247 |
> |
else |
248 |
> |
super.runTest(); |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
protected void runTestProfiled() throws Throwable { |
253 |
< |
// Warmup run, notably to trigger all needed classloading. |
254 |
< |
super.runTest(); |
201 |
< |
long t0 = System.nanoTime(); |
202 |
< |
try { |
253 |
> |
for (int i = 0; i < 2; i++) { |
254 |
> |
long startTime = System.nanoTime(); |
255 |
|
super.runTest(); |
256 |
< |
} finally { |
257 |
< |
long elapsedMillis = millisElapsedSince(t0); |
258 |
< |
if (elapsedMillis >= profileThreshold) |
256 |
> |
long elapsedMillis = millisElapsedSince(startTime); |
257 |
> |
if (elapsedMillis < profileThreshold) |
258 |
> |
break; |
259 |
> |
// Never report first run of any test; treat it as a |
260 |
> |
// warmup run, notably to trigger all needed classloading, |
261 |
> |
if (i > 0) |
262 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
263 |
|
} |
264 |
|
} |
336 |
|
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
337 |
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
338 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
339 |
< |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
339 |
> |
public static boolean atLeastJava9() { |
340 |
> |
return JAVA_CLASS_VERSION >= 53.0 |
341 |
> |
// As of 2015-09, java9 still uses 52.0 class file version |
342 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$"); |
343 |
> |
} |
344 |
> |
public static boolean atLeastJava10() { |
345 |
> |
return JAVA_CLASS_VERSION >= 54.0 |
346 |
> |
|| JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$"); |
347 |
> |
} |
348 |
|
|
349 |
|
/** |
350 |
|
* Collects all JSR166 unit tests as one suite. |
432 |
|
"LongAdderTest", |
433 |
|
"SplittableRandomTest", |
434 |
|
"StampedLockTest", |
435 |
+ |
"SubmissionPublisherTest", |
436 |
|
"ThreadLocalRandom8Test", |
437 |
|
}; |
438 |
|
addNamedTestClasses(suite, java8TestClassNames); |
441 |
|
// Java9+ test classes |
442 |
|
if (atLeastJava9()) { |
443 |
|
String[] java9TestClassNames = { |
444 |
< |
"ThreadPoolExecutor9Test", |
444 |
> |
// Currently empty, but expecting varhandle tests |
445 |
|
}; |
446 |
|
addNamedTestClasses(suite, java9TestClassNames); |
447 |
|
} |
508 |
|
} else { |
509 |
|
return new TestSuite(); |
510 |
|
} |
447 |
– |
|
511 |
|
} |
512 |
|
|
513 |
|
// Delays for timing-dependent tests, in milliseconds. |
565 |
|
* the same test have no effect. |
566 |
|
*/ |
567 |
|
public void threadRecordFailure(Throwable t) { |
568 |
+ |
System.err.println(t); |
569 |
+ |
dumpTestThreads(); |
570 |
|
threadFailure.compareAndSet(null, t); |
571 |
|
} |
572 |
|
|
574 |
|
setDelays(); |
575 |
|
} |
576 |
|
|
577 |
+ |
void tearDownFail(String format, Object... args) { |
578 |
+ |
String msg = toString() + ": " + String.format(format, args); |
579 |
+ |
System.err.println(msg); |
580 |
+ |
dumpTestThreads(); |
581 |
+ |
throw new AssertionFailedError(msg); |
582 |
+ |
} |
583 |
+ |
|
584 |
|
/** |
585 |
|
* Extra checks that get done for all test cases. |
586 |
|
* |
608 |
|
} |
609 |
|
|
610 |
|
if (Thread.interrupted()) |
611 |
< |
throw new AssertionFailedError("interrupt status set in main thread"); |
611 |
> |
tearDownFail("interrupt status set in main thread"); |
612 |
|
|
613 |
|
checkForkJoinPoolThreadLeaks(); |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
617 |
> |
* Finds missing PoolCleaners |
618 |
|
*/ |
619 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
620 |
< |
Thread[] survivors = new Thread[5]; |
620 |
> |
Thread[] survivors = new Thread[7]; |
621 |
|
int count = Thread.enumerate(survivors); |
622 |
|
for (int i = 0; i < count; i++) { |
623 |
|
Thread thread = survivors[i]; |
625 |
|
if (name.startsWith("ForkJoinPool-")) { |
626 |
|
// give thread some time to terminate |
627 |
|
thread.join(LONG_DELAY_MS); |
628 |
< |
if (!thread.isAlive()) continue; |
629 |
< |
throw new AssertionFailedError |
630 |
< |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
559 |
< |
toString(), name)); |
628 |
> |
if (thread.isAlive()) |
629 |
> |
tearDownFail("Found leaked ForkJoinPool thread thread=%s", |
630 |
> |
thread); |
631 |
|
} |
632 |
|
} |
633 |
+ |
|
634 |
+ |
if (!ForkJoinPool.commonPool() |
635 |
+ |
.awaitQuiescence(LONG_DELAY_MS, MILLISECONDS)) |
636 |
+ |
tearDownFail("ForkJoin common pool thread stuck"); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
646 |
|
fail(reason); |
647 |
|
} catch (AssertionFailedError t) { |
648 |
|
threadRecordFailure(t); |
649 |
< |
fail(reason); |
649 |
> |
throw t; |
650 |
|
} |
651 |
|
} |
652 |
|
|
773 |
|
/** |
774 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
775 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
776 |
< |
* until time elapses. |
776 |
> |
* until time elapses. Ensures that the given time, as measured |
777 |
> |
* by System.nanoTime(), has elapsed. |
778 |
|
*/ |
779 |
|
static void delay(long millis) throws InterruptedException { |
780 |
< |
long startTime = System.nanoTime(); |
781 |
< |
long ns = millis * 1000 * 1000; |
782 |
< |
for (;;) { |
780 |
> |
long nanos = millis * (1000 * 1000); |
781 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
782 |
> |
do { |
783 |
|
if (millis > 0L) |
784 |
|
Thread.sleep(millis); |
785 |
|
else // too short to sleep |
786 |
|
Thread.yield(); |
787 |
< |
long d = ns - (System.nanoTime() - startTime); |
788 |
< |
if (d > 0L) |
789 |
< |
millis = d / (1000 * 1000); |
790 |
< |
else |
791 |
< |
break; |
787 |
> |
nanos = wakeupTime - System.nanoTime(); |
788 |
> |
millis = nanos / (1000 * 1000); |
789 |
> |
} while (nanos >= 0L); |
790 |
> |
} |
791 |
> |
|
792 |
> |
/** |
793 |
> |
* Allows use of try-with-resources with per-test thread pools. |
794 |
> |
*/ |
795 |
> |
class PoolCleaner implements AutoCloseable { |
796 |
> |
private final ExecutorService pool; |
797 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
798 |
> |
public void close() { joinPool(pool); } |
799 |
> |
} |
800 |
> |
|
801 |
> |
/** |
802 |
> |
* An extension of PoolCleaner that has an action to release the pool. |
803 |
> |
*/ |
804 |
> |
class PoolCleanerWithReleaser extends PoolCleaner { |
805 |
> |
private final Runnable releaser; |
806 |
> |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
807 |
> |
super(pool); |
808 |
> |
this.releaser = releaser; |
809 |
> |
} |
810 |
> |
public void close() { |
811 |
> |
try { |
812 |
> |
releaser.run(); |
813 |
> |
} finally { |
814 |
> |
super.close(); |
815 |
> |
} |
816 |
|
} |
817 |
|
} |
818 |
|
|
819 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
820 |
+ |
return new PoolCleaner(pool); |
821 |
+ |
} |
822 |
+ |
|
823 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
824 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
825 |
+ |
} |
826 |
+ |
|
827 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
828 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
829 |
+ |
} |
830 |
+ |
|
831 |
+ |
Runnable releaser(final CountDownLatch latch) { |
832 |
+ |
return new Runnable() { public void run() { |
833 |
+ |
do { latch.countDown(); } |
834 |
+ |
while (latch.getCount() > 0); |
835 |
+ |
}}; |
836 |
+ |
} |
837 |
+ |
|
838 |
|
/** |
839 |
|
* Waits out termination of a thread pool or fails doing so. |
840 |
|
*/ |
841 |
< |
void joinPool(ExecutorService exec) { |
841 |
> |
void joinPool(ExecutorService pool) { |
842 |
|
try { |
843 |
< |
exec.shutdown(); |
844 |
< |
if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
845 |
< |
fail("ExecutorService " + exec + |
846 |
< |
" did not terminate in a timely manner"); |
843 |
> |
pool.shutdown(); |
844 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
845 |
> |
try { |
846 |
> |
threadFail("ExecutorService " + pool + |
847 |
> |
" did not terminate in a timely manner"); |
848 |
> |
} finally { |
849 |
> |
// last resort, for the benefit of subsequent tests |
850 |
> |
pool.shutdownNow(); |
851 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
852 |
> |
} |
853 |
> |
} |
854 |
|
} catch (SecurityException ok) { |
855 |
|
// Allowed in case test doesn't have privs |
856 |
|
} catch (InterruptedException fail) { |
857 |
< |
fail("Unexpected InterruptedException"); |
857 |
> |
threadFail("Unexpected InterruptedException"); |
858 |
|
} |
859 |
|
} |
860 |
|
|
861 |
+ |
/** Like Runnable, but with the freedom to throw anything */ |
862 |
+ |
interface Action { public void run() throws Throwable; } |
863 |
+ |
|
864 |
|
/** |
865 |
< |
* A debugging tool to print all stack traces, as jstack does. |
865 |
> |
* Runs all the given actions in parallel, failing if any fail. |
866 |
> |
* Useful for running multiple variants of tests that are |
867 |
> |
* necessarily individually slow because they must block. |
868 |
|
*/ |
869 |
< |
static void printAllStackTraces() { |
870 |
< |
for (ThreadInfo info : |
871 |
< |
ManagementFactory.getThreadMXBean() |
872 |
< |
.dumpAllThreads(true, true)) |
869 |
> |
void testInParallel(Action ... actions) { |
870 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
871 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
872 |
> |
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
873 |
> |
for (final Action action : actions) |
874 |
> |
futures.add(pool.submit(new CheckedRunnable() { |
875 |
> |
public void realRun() throws Throwable { action.run();}})); |
876 |
> |
for (Future<?> future : futures) |
877 |
> |
try { |
878 |
> |
assertNull(future.get(LONG_DELAY_MS, MILLISECONDS)); |
879 |
> |
} catch (ExecutionException ex) { |
880 |
> |
threadUnexpectedException(ex.getCause()); |
881 |
> |
} catch (Exception ex) { |
882 |
> |
threadUnexpectedException(ex); |
883 |
> |
} |
884 |
> |
} |
885 |
> |
} |
886 |
> |
|
887 |
> |
/** |
888 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
889 |
> |
* Uninteresting threads are filtered out. |
890 |
> |
*/ |
891 |
> |
static void dumpTestThreads() { |
892 |
> |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
893 |
> |
System.err.println("------ stacktrace dump start ------"); |
894 |
> |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
895 |
> |
String name = info.getThreadName(); |
896 |
> |
if ("Signal Dispatcher".equals(name)) |
897 |
> |
continue; |
898 |
> |
if ("Reference Handler".equals(name) |
899 |
> |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
900 |
> |
continue; |
901 |
> |
if ("Finalizer".equals(name) |
902 |
> |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
903 |
> |
continue; |
904 |
> |
if ("checkForWedgedTest".equals(name)) |
905 |
> |
continue; |
906 |
|
System.err.print(info); |
907 |
+ |
} |
908 |
+ |
System.err.println("------ stacktrace dump end ------"); |
909 |
|
} |
910 |
|
|
911 |
|
/** |
925 |
|
delay(millis); |
926 |
|
assertTrue(thread.isAlive()); |
927 |
|
} catch (InterruptedException fail) { |
928 |
< |
fail("Unexpected InterruptedException"); |
928 |
> |
threadFail("Unexpected InterruptedException"); |
929 |
|
} |
930 |
|
} |
931 |
|
|
947 |
|
for (Thread thread : threads) |
948 |
|
assertTrue(thread.isAlive()); |
949 |
|
} catch (InterruptedException fail) { |
950 |
< |
fail("Unexpected InterruptedException"); |
950 |
> |
threadFail("Unexpected InterruptedException"); |
951 |
|
} |
952 |
|
} |
953 |
|
|
1225 |
|
} finally { |
1226 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1227 |
|
t.interrupt(); |
1228 |
< |
fail("Test timed out"); |
1228 |
> |
threadFail("Test timed out"); |
1229 |
|
} |
1230 |
|
} |
1231 |
|
} |
1350 |
|
public static final String TEST_STRING = "a test string"; |
1351 |
|
|
1352 |
|
public static class StringTask implements Callable<String> { |
1353 |
< |
public String call() { return TEST_STRING; } |
1353 |
> |
final String value; |
1354 |
> |
public StringTask() { this(TEST_STRING); } |
1355 |
> |
public StringTask(String value) { this.value = value; } |
1356 |
> |
public String call() { return value; } |
1357 |
|
} |
1358 |
|
|
1359 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1366 |
|
}}; |
1367 |
|
} |
1368 |
|
|
1369 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1369 |
> |
public Runnable countDowner(final CountDownLatch latch) { |
1370 |
|
return new CheckedRunnable() { |
1371 |
|
public void realRun() throws InterruptedException { |
1372 |
< |
await(latch); |
1372 |
> |
latch.countDown(); |
1373 |
|
}}; |
1374 |
|
} |
1375 |
|
|
1376 |
+ |
class LatchAwaiter extends CheckedRunnable { |
1377 |
+ |
static final int NEW = 0; |
1378 |
+ |
static final int RUNNING = 1; |
1379 |
+ |
static final int DONE = 2; |
1380 |
+ |
final CountDownLatch latch; |
1381 |
+ |
int state = NEW; |
1382 |
+ |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1383 |
+ |
public void realRun() throws InterruptedException { |
1384 |
+ |
state = 1; |
1385 |
+ |
await(latch); |
1386 |
+ |
state = 2; |
1387 |
+ |
} |
1388 |
+ |
} |
1389 |
+ |
|
1390 |
+ |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1391 |
+ |
return new LatchAwaiter(latch); |
1392 |
+ |
} |
1393 |
+ |
|
1394 |
|
public void await(CountDownLatch latch) { |
1395 |
|
try { |
1396 |
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |