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(10); } |
197 |
> |
try { MINUTES.sleep(timeoutMinutes); } |
198 |
|
catch (InterruptedException unexpected) { break; } |
199 |
|
if (lastTestCase == currentTestCase) { |
200 |
|
System.err.println |
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 |
|
} |
770 |
|
/** |
771 |
|
* Allows use of try-with-resources with per-test thread pools. |
772 |
|
*/ |
773 |
< |
static class PoolCloser<T extends ExecutorService> |
773 |
> |
class PoolCleaner<T extends ExecutorService> |
774 |
|
implements AutoCloseable { |
775 |
|
public final T pool; |
776 |
< |
public PoolCloser(T pool) { this.pool = pool; } |
776 |
> |
public PoolCleaner(T pool) { this.pool = pool; } |
777 |
|
public void close() { joinPool(pool); } |
778 |
|
} |
779 |
|
|
780 |
+ |
<T extends ExecutorService> PoolCleaner<T> cleaner(T pool) { |
781 |
+ |
return new PoolCleaner<T>(pool); |
782 |
+ |
} |
783 |
+ |
|
784 |
|
/** |
785 |
|
* Waits out termination of a thread pool or fails doing so. |
786 |
|
*/ |
787 |
< |
static void joinPool(ExecutorService pool) { |
787 |
> |
void joinPool(ExecutorService pool) { |
788 |
|
try { |
789 |
|
pool.shutdown(); |
790 |
< |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) |
791 |
< |
fail("ExecutorService " + pool + |
792 |
< |
" did not terminate in a timely manner"); |
790 |
> |
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) { |
791 |
> |
try { |
792 |
> |
threadFail("ExecutorService " + pool + |
793 |
> |
" did not terminate in a timely manner"); |
794 |
> |
} finally { |
795 |
> |
// last resort, for the benefit of subsequent tests |
796 |
> |
pool.shutdownNow(); |
797 |
> |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
798 |
> |
} |
799 |
> |
} |
800 |
|
} catch (SecurityException ok) { |
801 |
|
// Allowed in case test doesn't have privs |
802 |
|
} catch (InterruptedException fail) { |
803 |
< |
fail("Unexpected InterruptedException"); |
803 |
> |
threadFail("Unexpected InterruptedException"); |
804 |
|
} |
805 |
|
} |
806 |
|
|
813 |
|
* necessarily individually slow because they must block. |
814 |
|
*/ |
815 |
|
void testInParallel(Action ... actions) { |
816 |
< |
try (PoolCloser<ExecutorService> poolCloser |
817 |
< |
= new PoolCloser<>(Executors.newCachedThreadPool())) { |
818 |
< |
ExecutorService pool = poolCloser.pool; |
816 |
> |
try (PoolCleaner<ExecutorService> cleaner |
817 |
> |
= cleaner(Executors.newCachedThreadPool())) { |
818 |
> |
ExecutorService pool = cleaner.pool; |
819 |
|
ArrayList<Future<?>> futures = new ArrayList<>(actions.length); |
820 |
|
for (final Action action : actions) |
821 |
|
futures.add(pool.submit(new CheckedRunnable() { |
872 |
|
delay(millis); |
873 |
|
assertTrue(thread.isAlive()); |
874 |
|
} catch (InterruptedException fail) { |
875 |
< |
fail("Unexpected InterruptedException"); |
875 |
> |
threadFail("Unexpected InterruptedException"); |
876 |
|
} |
877 |
|
} |
878 |
|
|
894 |
|
for (Thread thread : threads) |
895 |
|
assertTrue(thread.isAlive()); |
896 |
|
} catch (InterruptedException fail) { |
897 |
< |
fail("Unexpected InterruptedException"); |
897 |
> |
threadFail("Unexpected InterruptedException"); |
898 |
|
} |
899 |
|
} |
900 |
|
|