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.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); |
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 |
|
}}}; |
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 |
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 |
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 |
|
*/ |
848 |
|
} finally { |
849 |
|
// last resort, for the benefit of subsequent tests |
850 |
|
pool.shutdownNow(); |
851 |
< |
pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS); |
851 |
> |
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS); |
852 |
|
} |
853 |
|
} |
854 |
|
} catch (SecurityException ok) { |
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 |
|
} |