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; |
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; |
191 |
|
return (regex == null) ? null : Pattern.compile(regex); |
192 |
|
} |
193 |
|
|
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 |
202 |
|
try { MINUTES.sleep(timeoutMinutes); } |
203 |
|
catch (InterruptedException unexpected) { break; } |
204 |
|
if (lastTestCase == currentTestCase) { |
205 |
< |
System.err.println |
206 |
< |
("Looks like we're stuck running test: " |
207 |
< |
+ lastTestCase); |
205 |
> |
System.err.printf( |
206 |
> |
"Looks like we're stuck running test: %s (%d/%d)%n", |
207 |
> |
lastTestCase, currentRun, runsPerTest); |
208 |
> |
System.err.println("availableProcessors=" + |
209 |
> |
Runtime.getRuntime().availableProcessors()); |
210 |
> |
System.err.printf("cpu model = %s%n", cpuModel()); |
211 |
|
dumpTestThreads(); |
212 |
+ |
// one stack dump is probably enough; more would be spam |
213 |
+ |
break; |
214 |
|
} |
215 |
|
lastTestCase = currentTestCase; |
216 |
|
}}}; |
219 |
|
thread.start(); |
220 |
|
} |
221 |
|
|
222 |
+ |
public static String cpuModel() { |
223 |
+ |
try { |
224 |
+ |
Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
225 |
+ |
.matcher(new String( |
226 |
+ |
Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
227 |
+ |
matcher.find(); |
228 |
+ |
return matcher.group(1); |
229 |
+ |
} catch (Exception ex) { return null; } |
230 |
+ |
} |
231 |
+ |
|
232 |
|
public void runBare() throws Throwable { |
233 |
|
currentTestCase = this; |
234 |
|
if (methodFilter == null |
238 |
|
|
239 |
|
protected void runTest() throws Throwable { |
240 |
|
for (int i = 0; i < runsPerTest; i++) { |
241 |
+ |
currentRun = i; |
242 |
|
if (profileTests) |
243 |
|
runTestProfiled(); |
244 |
|
else |
795 |
|
public void close() { joinPool(pool); } |
796 |
|
} |
797 |
|
|
798 |
+ |
/** |
799 |
+ |
* An extension of PoolCleaner that has an action to release the pool. |
800 |
+ |
*/ |
801 |
+ |
class PoolCleanerWithReleaser extends PoolCleaner { |
802 |
+ |
private final Runnable releaser; |
803 |
+ |
public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) { |
804 |
+ |
super(pool); |
805 |
+ |
this.releaser = releaser; |
806 |
+ |
} |
807 |
+ |
public void close() { |
808 |
+ |
try { |
809 |
+ |
releaser.run(); |
810 |
+ |
} finally { |
811 |
+ |
super.close(); |
812 |
+ |
} |
813 |
+ |
} |
814 |
+ |
} |
815 |
+ |
|
816 |
|
PoolCleaner cleaner(ExecutorService pool) { |
817 |
|
return new PoolCleaner(pool); |
818 |
|
} |
819 |
|
|
820 |
+ |
PoolCleaner cleaner(ExecutorService pool, Runnable releaser) { |
821 |
+ |
return new PoolCleanerWithReleaser(pool, releaser); |
822 |
+ |
} |
823 |
+ |
|
824 |
+ |
PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) { |
825 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(latch)); |
826 |
+ |
} |
827 |
+ |
|
828 |
+ |
Runnable releaser(final CountDownLatch latch) { |
829 |
+ |
return new Runnable() { public void run() { |
830 |
+ |
do { latch.countDown(); } |
831 |
+ |
while (latch.getCount() > 0); |
832 |
+ |
}}; |
833 |
+ |
} |
834 |
+ |
|
835 |
|
/** |
836 |
|
* Waits out termination of a thread pool or fails doing so. |
837 |
|
*/ |
845 |
|
} finally { |
846 |
|
// last resort, for the benefit of subsequent tests |
847 |
|
pool.shutdownNow(); |
848 |
< |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
848 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
849 |
|
} |
850 |
|
} |
851 |
|
} catch (SecurityException ok) { |
1222 |
|
} finally { |
1223 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1224 |
|
t.interrupt(); |
1225 |
< |
fail("Test timed out"); |
1225 |
> |
threadFail("Test timed out"); |
1226 |
|
} |
1227 |
|
} |
1228 |
|
} |