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 |
+ |
} |
205 |
+ |
lastTestCase = currentTestCase; |
206 |
+ |
}}}; |
207 |
+ |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
208 |
+ |
thread.setDaemon(true); |
209 |
+ |
thread.start(); |
210 |
+ |
} |
211 |
+ |
|
212 |
|
public void runBare() throws Throwable { |
213 |
+ |
currentTestCase = this; |
214 |
|
if (methodFilter == null |
215 |
|
|| methodFilter.matcher(toString()).find()) |
216 |
|
super.runBare(); |
484 |
|
} else { |
485 |
|
return new TestSuite(); |
486 |
|
} |
464 |
– |
|
487 |
|
} |
488 |
|
|
489 |
|
// Delays for timing-dependent tests, in milliseconds. |
541 |
|
* the same test have no effect. |
542 |
|
*/ |
543 |
|
public void threadRecordFailure(Throwable t) { |
544 |
+ |
System.err.println(t); |
545 |
+ |
dumpTestThreads(); |
546 |
|
threadFailure.compareAndSet(null, t); |
547 |
|
} |
548 |
|
|
553 |
|
void tearDownFail(String format, Object... args) { |
554 |
|
String msg = toString() + ": " + String.format(format, args); |
555 |
|
System.err.println(msg); |
556 |
< |
printAllStackTraces(); |
556 |
> |
dumpTestThreads(); |
557 |
|
throw new AssertionFailedError(msg); |
558 |
|
} |
559 |
|
|
622 |
|
fail(reason); |
623 |
|
} catch (AssertionFailedError t) { |
624 |
|
threadRecordFailure(t); |
625 |
< |
fail(reason); |
625 |
> |
throw t; |
626 |
|
} |
627 |
|
} |
628 |
|
|
749 |
|
/** |
750 |
|
* Delays, via Thread.sleep, for the given millisecond delay, but |
751 |
|
* if the sleep is shorter than specified, may re-sleep or yield |
752 |
< |
* until time elapses. |
752 |
> |
* until time elapses. Ensures that the given time, as measured |
753 |
> |
* by System.nanoTime(), has elapsed. |
754 |
|
*/ |
755 |
|
static void delay(long millis) throws InterruptedException { |
756 |
< |
long startTime = System.nanoTime(); |
757 |
< |
long ns = millis * 1000 * 1000; |
758 |
< |
for (;;) { |
756 |
> |
long nanos = millis * (1000 * 1000); |
757 |
> |
final long wakeupTime = System.nanoTime() + nanos; |
758 |
> |
do { |
759 |
|
if (millis > 0L) |
760 |
|
Thread.sleep(millis); |
761 |
|
else // too short to sleep |
762 |
|
Thread.yield(); |
763 |
< |
long d = ns - (System.nanoTime() - startTime); |
764 |
< |
if (d > 0L) |
765 |
< |
millis = d / (1000 * 1000); |
741 |
< |
else |
742 |
< |
break; |
743 |
< |
} |
763 |
> |
nanos = wakeupTime - System.nanoTime(); |
764 |
> |
millis = nanos / (1000 * 1000); |
765 |
> |
} while (nanos >= 0L); |
766 |
|
} |
767 |
|
|
768 |
|
/** |
769 |
|
* Allows use of try-with-resources with per-test thread pools. |
770 |
|
*/ |
771 |
< |
static class PoolCloser<T extends ExecutorService> |
772 |
< |
implements AutoCloseable { |
773 |
< |
public final T pool; |
752 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
771 |
> |
class PoolCleaner implements AutoCloseable { |
772 |
> |
private final ExecutorService pool; |
773 |
> |
public PoolCleaner(ExecutorService pool) { this.pool = pool; } |
774 |
|
public void close() { joinPool(pool); } |
775 |
|
} |
776 |
|
|
777 |
+ |
PoolCleaner cleaner(ExecutorService pool) { |
778 |
+ |
return new PoolCleaner(pool); |
779 |
+ |
} |
780 |
+ |
|
781 |
|
/** |
782 |
|
* Waits out termination of a thread pool or fails doing so. |
783 |
|
*/ |
784 |
< |
static void joinPool(ExecutorService pool) { |
784 |
> |
void joinPool(ExecutorService pool) { |
785 |
|
try { |
786 |
|
pool.shutdown(); |
787 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
788 |
< |
fail("ExecutorService " + pool + |
789 |
< |
" did not terminate in a timely manner"); |
787 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
788 |
> |
try { |
789 |
> |
threadFail("ExecutorService " + pool + |
790 |
> |
" did not terminate in a timely manner"); |
791 |
> |
} finally { |
792 |
> |
// last resort, for the benefit of subsequent tests |
793 |
> |
pool.shutdownNow(); |
794 |
> |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
795 |
> |
} |
796 |
> |
} |
797 |
|
} catch (SecurityException ok) { |
798 |
|
// Allowed in case test doesn't have privs |
799 |
|
} catch (InterruptedException fail) { |
800 |
< |
fail("Unexpected InterruptedException"); |
800 |
> |
threadFail("Unexpected InterruptedException"); |
801 |
|
} |
802 |
|
} |
803 |
|
|
810 |
|
* necessarily individually slow because they must block. |
811 |
|
*/ |
812 |
|
void testInParallel(Action ... actions) { |
813 |
< |
try (PoolCloser<ExecutorService> poolCloser |
814 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
783 |
< |
ExecutorService pool = poolCloser.pool; |
813 |
> |
ExecutorService pool = Executors.newCachedThreadPool(); |
814 |
> |
try (PoolCleaner cleaner = cleaner(pool)) { |
815 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
816 |
|
for (final Action action : actions) |
817 |
|
futures.add(pool.submit(new CheckedRunnable() { |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
< |
* A debugging tool to print all stack traces, as jstack does. |
831 |
> |
* A debugging tool to print stack traces of most threads, as jstack does. |
832 |
|
* Uninteresting threads are filtered out. |
833 |
|
*/ |
834 |
< |
static void printAllStackTraces() { |
834 |
> |
static void dumpTestThreads() { |
835 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
836 |
|
System.err.println("------ stacktrace dump start ------"); |
837 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
844 |
|
if ("Finalizer".equals(name) |
845 |
|
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
846 |
|
continue; |
847 |
+ |
if ("checkForWedgedTest".equals(name)) |
848 |
+ |
continue; |
849 |
|
System.err.print(info); |
850 |
|
} |
851 |
|
System.err.println("------ stacktrace dump end ------"); |
868 |
|
delay(millis); |
869 |
|
assertTrue(thread.isAlive()); |
870 |
|
} catch (InterruptedException fail) { |
871 |
< |
fail("Unexpected InterruptedException"); |
871 |
> |
threadFail("Unexpected InterruptedException"); |
872 |
|
} |
873 |
|
} |
874 |
|
|
890 |
|
for (Thread thread : threads) |
891 |
|
assertTrue(thread.isAlive()); |
892 |
|
} catch (InterruptedException fail) { |
893 |
< |
fail("Unexpected InterruptedException"); |
893 |
> |
threadFail("Unexpected InterruptedException"); |
894 |
|
} |
895 |
|
} |
896 |
|
|
1316 |
|
}}; |
1317 |
|
} |
1318 |
|
|
1319 |
< |
public Runnable awaiter(final CountDownLatch latch) { |
1320 |
< |
return new CheckedRunnable() { |
1321 |
< |
public void realRun() throws InterruptedException { |
1322 |
< |
await(latch); |
1323 |
< |
}}; |
1319 |
> |
class LatchAwaiter extends CheckedRunnable { |
1320 |
> |
static final int NEW = 0; |
1321 |
> |
static final int RUNNING = 1; |
1322 |
> |
static final int DONE = 2; |
1323 |
> |
final CountDownLatch latch; |
1324 |
> |
int state = NEW; |
1325 |
> |
LatchAwaiter(CountDownLatch latch) { this.latch = latch; } |
1326 |
> |
public void realRun() throws InterruptedException { |
1327 |
> |
state = 1; |
1328 |
> |
await(latch); |
1329 |
> |
state = 2; |
1330 |
> |
} |
1331 |
> |
} |
1332 |
> |
|
1333 |
> |
public LatchAwaiter awaiter(CountDownLatch latch) { |
1334 |
> |
return new LatchAwaiter(latch); |
1335 |
|
} |
1336 |
|
|
1337 |
|
public void await(CountDownLatch latch) { |