ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.218 by jsr166, Sun Jan 29 20:19:00 2017 UTC vs.
Revision 1.220 by jsr166, Sat Mar 11 17:33:32 2017 UTC

# Line 52 | Line 52 | import java.lang.management.ThreadMXBean
52   import java.lang.reflect.Constructor;
53   import java.lang.reflect.Method;
54   import java.lang.reflect.Modifier;
55 import java.nio.file.Files;
56 import java.nio.file.Paths;
55   import java.security.CodeSource;
56   import java.security.Permission;
57   import java.security.PermissionCollection;
# Line 91 | Line 89 | import java.util.concurrent.ThreadPoolEx
89   import java.util.concurrent.TimeoutException;
90   import java.util.concurrent.atomic.AtomicBoolean;
91   import java.util.concurrent.atomic.AtomicReference;
94 import java.util.regex.Matcher;
92   import java.util.regex.Pattern;
93  
94   import junit.framework.AssertionFailedError;
# Line 301 | Line 298 | public class JSR166TestCase extends Test
298  
299   //     public static String cpuModel() {
300   //         try {
301 < //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
301 > //             java.util.regex.Matcher matcher
302 > //               = Pattern.compile("model name\\s*: (.*)")
303   //                 .matcher(new String(
304 < //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
304 > //                     java.nio.file.Files.readAllBytes(
305 > //                         java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8"));
306   //             matcher.find();
307   //             return matcher.group(1);
308   //         } catch (Exception ex) { return null; }
# Line 1299 | Line 1298 | public class JSR166TestCase extends Test
1298                  startTime = System.nanoTime();
1299              else if (millisElapsedSince(startTime) > timeoutMillis) {
1300                  threadAssertTrue(thread.isAlive());
1301 <                return;
1301 >                fail("timed out waiting for thread to enter wait state");
1302              }
1303              Thread.yield();
1304          }
1305      }
1306  
1307      /**
1308 <     * Waits up to LONG_DELAY_MS for the given thread to enter a wait
1309 <     * state: BLOCKED, WAITING, or TIMED_WAITING.
1308 >     * Spin-waits up to the specified number of milliseconds for the given
1309 >     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
1310 >     * and additionally satisfy the given condition.
1311 >     */
1312 >    void waitForThreadToEnterWaitState(
1313 >        Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) {
1314 >        long startTime = 0L;
1315 >        for (;;) {
1316 >            Thread.State s = thread.getState();
1317 >            if (s == Thread.State.BLOCKED ||
1318 >                s == Thread.State.WAITING ||
1319 >                s == Thread.State.TIMED_WAITING) {
1320 >                try {
1321 >                    if (waitingForGodot.call())
1322 >                        return;
1323 >                } catch (Throwable fail) { threadUnexpectedException(fail); }
1324 >            }
1325 >            else if (s == Thread.State.TERMINATED)
1326 >                fail("Unexpected thread termination");
1327 >            else if (startTime == 0L)
1328 >                startTime = System.nanoTime();
1329 >            else if (millisElapsedSince(startTime) > timeoutMillis) {
1330 >                threadAssertTrue(thread.isAlive());
1331 >                fail("timed out waiting for thread to enter wait state");
1332 >            }
1333 >            Thread.yield();
1334 >        }
1335 >    }
1336 >
1337 >    /**
1338 >     * Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
1339 >     * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
1340       */
1341      void waitForThreadToEnterWaitState(Thread thread) {
1342          waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
1343      }
1344  
1345      /**
1346 +     * Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to
1347 +     * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
1348 +     * and additionally satisfy the given condition.
1349 +     */
1350 +    void waitForThreadToEnterWaitState(
1351 +        Thread thread, Callable<Boolean> waitingForGodot) {
1352 +        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot);
1353 +    }
1354 +
1355 +    /**
1356       * Returns the number of milliseconds since time given by
1357       * startNanoTime, which must have been previously returned from a
1358       * call to {@link System#nanoTime()}.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines