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; |
188 |
|
return (regex == null) ? null : Pattern.compile(regex); |
189 |
|
} |
190 |
|
|
191 |
+ |
static volatile TestCase currentTestCase; |
192 |
+ |
static { |
193 |
+ |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
194 |
+ |
// avoid spurious reports with enormous runsPerTest |
195 |
+ |
final int timeoutMinutes = Math.max(runsPerTest / 10, 1); |
196 |
+ |
for (TestCase lastTestCase = currentTestCase;;) { |
197 |
+ |
try { MINUTES.sleep(timeoutMinutes); } |
198 |
+ |
catch (InterruptedException unexpected) { break; } |
199 |
+ |
if (lastTestCase == currentTestCase) { |
200 |
+ |
System.err.println |
201 |
+ |
("Looks like we're stuck running test: " |
202 |
+ |
+ lastTestCase); |
203 |
+ |
dumpTestThreads(); |
204 |
+ |
// one stack dump is probably enough; more would be spam |
205 |
+ |
break; |
206 |
+ |
} |
207 |
+ |
lastTestCase = currentTestCase; |
208 |
+ |
}}}; |
209 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
210 |
+ |
thread.setDaemon(true); |
211 |
+ |
thread.start(); |
212 |
+ |
} |
213 |
+ |
|
214 |
|
public void runBare() throws Throwable { |
215 |
+ |
currentTestCase = this; |
216 |
|
if (methodFilter == null |
217 |
|
|| methodFilter.matcher(toString()).find()) |
218 |
|
super.runBare(); |
543 |
|
* the same test have no effect. |
544 |
|
*/ |
545 |
|
public void threadRecordFailure(Throwable t) { |
546 |
< |
threadDump(); |
546 |
> |
System.err.println(t); |
547 |
> |
dumpTestThreads(); |
548 |
|
threadFailure.compareAndSet(null, t); |
549 |
|
} |
550 |
|
|
555 |
|
void tearDownFail(String format, Object... args) { |
556 |
|
String msg = toString() + ": " + String.format(format, args); |
557 |
|
System.err.println(msg); |
558 |
< |
threadDump(); |
558 |
> |
dumpTestThreads(); |
559 |
|
throw new AssertionFailedError(msg); |
560 |
|
} |
561 |
|
|
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
< |
* Finds missing try { ... } finally { joinPool(e); } |
595 |
> |
* Finds missing PoolCleaners |
596 |
|
*/ |
597 |
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
598 |
|
Thread[] survivors = new Thread[7]; |
751 |
|
/** |
752 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
753 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
754 |
< |
* until time elapses. |
754 |
> |
* until time elapses. Ensures that the given time, as measured |
755 |
> |
* by System.nanoTime(), has elapsed. |
756 |
|
*/ |
757 |
|
static void delay(long millis) throws InterruptedException { |
758 |
< |
long startTime = System.nanoTime(); |
759 |
< |
long ns = millis * 1000 * 1000; |
760 |
< |
for (;;) { |
758 |
> |
long nanos = millis * (1000 * 1000); |
759 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
760 |
> |
do { |
761 |
|
if (millis > 0L) |
762 |
|
Thread.sleep(millis); |
763 |
|
else // too short to sleep |
764 |
|
Thread.yield(); |
765 |
< |
long d = ns - (System.nanoTime() - startTime); |
766 |
< |
if (d > 0L) |
767 |
< |
millis = d / (1000 * 1000); |
741 |
< |
else |
742 |
< |
break; |
743 |
< |
} |
765 |
> |
nanos = wakeupTime - System.nanoTime(); |
766 |
> |
millis = nanos / (1000 * 1000); |
767 |
> |
} while (nanos >= 0L); |
768 |
|
} |
769 |
|
|
770 |
|
/** |
771 |
|
* Allows use of try-with-resources with per-test thread pools. |
772 |
|
*/ |
773 |
< |
static class PoolCloser<T extends ExecutorService> |
774 |
< |
implements AutoCloseable { |
775 |
< |
public final T pool; |
752 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
773 |
> |
class PoolCleaner implements AutoCloseable { |
774 |
> |
private final ExecutorService pool; |
775 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
776 |
|
public void close() { joinPool(pool); } |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
781 |
+ |
*/ |
782 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
783 |
+ |
private final Runnable releaser; |
784 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
785 |
+ |
super(pool); |
786 |
+ |
this.releaser = releaser; |
787 |
+ |
} |
788 |
+ |
public void close() { |
789 |
+ |
try { |
790 |
+ |
releaser.run(); |
791 |
+ |
} finally { |
792 |
+ |
super.close(); |
793 |
+ |
} |
794 |
+ |
} |
795 |
+ |
} |
796 |
+ |
|
797 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
798 |
+ |
return new PoolCleaner(pool); |
799 |
+ |
} |
800 |
+ |
|
801 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
802 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
806 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
807 |
+ |
} |
808 |
+ |
|
809 |
+ |
Runnable releaser(final CountDownLatch latch) { |
810 |
+ |
return new Runnable() { public void run() { |
811 |
+ |
do { latch.countDown(); } |
812 |
+ |
while (latch.getCount() > 0); |
813 |
+ |
}}; |
814 |
+ |
} |
815 |
+ |
|
816 |
+ |
/** |
817 |
|
* Waits out termination of a thread pool or fails doing so. |
818 |
|
*/ |
819 |
< |
static void joinPool(ExecutorService pool) { |
819 |
> |
void joinPool(ExecutorService pool) { |
820 |
|
try { |
821 |
|
pool.shutdown(); |
822 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
823 |
< |
fail("ExecutorService " + pool + |
824 |
< |
" did not terminate in a timely manner"); |
822 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
823 |
> |
try { |
824 |
> |
threadFail("ExecutorService " + pool + |
825 |
> |
" did not terminate in a timely manner"); |
826 |
> |
} finally { |
827 |
> |
// last resort, for the benefit of subsequent tests |
828 |
> |
pool.shutdownNow(); |
829 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
830 |
> |
} |
831 |
> |
} |
832 |
|
} catch (SecurityException ok) { |
833 |
|
// Allowed in case test doesn't have privs |
834 |
|
} catch (InterruptedException fail) { |
835 |
< |
fail("Unexpected InterruptedException"); |
835 |
> |
threadFail("Unexpected InterruptedException"); |
836 |
|
} |
837 |
|
} |
838 |
|
|
845 |
|
* necessarily individually slow because they must block. |
846 |
|
*/ |
847 |
|
void testInParallel(Action ... actions) { |
848 |
< |
try (PoolCloser<ExecutorService> poolCloser |
849 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
783 |
< |
ExecutorService pool = poolCloser.pool; |
848 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
849 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
850 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
851 |
|
for (final Action action : actions) |
852 |
|
futures.add(pool.submit(new CheckedRunnable() { |
863 |
|
} |
864 |
|
|
865 |
|
/** |
866 |
< |
* A debugging tool to print all stack traces, as jstack does. |
866 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
867 |
|
* Uninteresting threads are filtered out. |
868 |
|
*/ |
869 |
< |
static void threadDump() { |
869 |
> |
static void dumpTestThreads() { |
870 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
871 |
|
System.err.println("------ stacktrace dump start ------"); |
872 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
879 |
|
if ("Finalizer".equals(name) |
880 |
|
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
881 |
|
continue; |
882 |
+ |
if ("checkForWedgedTest".equals(name)) |
883 |
+ |
continue; |
884 |
|
System.err.print(info); |
885 |
|
} |
886 |
|
System.err.println("------ stacktrace dump end ------"); |
903 |
|
delay(millis); |
904 |
|
assertTrue(thread.isAlive()); |
905 |
|
} catch (InterruptedException fail) { |
906 |
< |
fail("Unexpected InterruptedException"); |
906 |
> |
threadFail("Unexpected InterruptedException"); |
907 |
|
} |
908 |
|
} |
909 |
|
|
925 |
|
for (Thread thread : threads) |
926 |
|
assertTrue(thread.isAlive()); |
927 |
|
} catch (InterruptedException fail) { |
928 |
< |
fail("Unexpected InterruptedException"); |
928 |
> |
threadFail("Unexpected InterruptedException"); |
929 |
|
} |
930 |
|
} |
931 |
|
|
1351 |
|
}}; |
1352 |
|
} |
1353 |
|
|
1354 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1355 |
< |
return new CheckedRunnable() { |
1356 |
< |
public void realRun() throws InterruptedException { |
1357 |
< |
await(latch); |
1358 |
< |
}}; |
1354 |
> |
class LatchAwaiter extends CheckedRunnable { |
1355 |
> |
static final int NEW = 0; |
1356 |
> |
static final int RUNNING = 1; |
1357 |
> |
static final int DONE = 2; |
1358 |
> |
final CountDownLatch latch; |
1359 |
> |
int state = NEW; |
1360 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1361 |
> |
public void realRun() throws InterruptedException { |
1362 |
> |
state = 1; |
1363 |
> |
await(latch); |
1364 |
> |
state = 2; |
1365 |
> |
} |
1366 |
> |
} |
1367 |
> |
|
1368 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1369 |
> |
return new LatchAwaiter(latch); |
1370 |
|
} |
1371 |
|
|
1372 |
|
public void await(CountDownLatch latch) { |