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

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.54 by jsr166, Wed Dec 31 19:05:43 2014 UTC vs.
Revision 1.63 by jsr166, Fri Jul 3 05:48:30 2015 UTC

# Line 22 | Line 22 | import junit.framework.TestSuite;
22  
23   public class ReentrantLockTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28          return new TestSuite(ReentrantLockTest.class);
29      }
30  
31      /**
32 <     * A runnable calling lockInterruptibly
32 >     * A checked runnable calling lockInterruptibly
33       */
34      class InterruptibleLockRunnable extends CheckedRunnable {
35          final ReentrantLock lock;
36 <        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
36 >        InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
37          public void realRun() throws InterruptedException {
38              lock.lockInterruptibly();
39          }
40      }
41  
42      /**
43 <     * A runnable calling lockInterruptibly that expects to be
43 >     * A checked runnable calling lockInterruptibly that expects to be
44       * interrupted
45       */
46      class InterruptedLockRunnable extends CheckedInterruptedRunnable {
47          final ReentrantLock lock;
48 <        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
48 >        InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
49          public void realRun() throws InterruptedException {
50              lock.lockInterruptibly();
51          }
# Line 146 | Line 146 | public class ReentrantLockTest extends J
146      enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147  
148      /**
149 <     * Awaits condition using the specified AwaitMethod.
149 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
150       */
151      void await(Condition c, AwaitMethod awaitMethod)
152              throws InterruptedException {
# Line 159 | Line 159 | public class ReentrantLockTest extends J
159              assertTrue(c.await(timeoutMillis, MILLISECONDS));
160              break;
161          case awaitNanos:
162 <            long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
163 <            long nanosRemaining = c.awaitNanos(nanosTimeout);
164 <            assertTrue(nanosRemaining > 0);
162 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
163 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
164 >            assertTrue(nanosRemaining > timeoutNanos / 2);
165 >            assertTrue(nanosRemaining <= timeoutNanos);
166              break;
167          case awaitUntil:
168              assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
169              break;
170 +        default:
171 +            throw new AssertionError();
172          }
173      }
174  
# Line 422 | Line 425 | public class ReentrantLockTest extends J
425          }
426          for (int i = SIZE; i > 0; i--) {
427              lock.unlock();
428 <            assertEquals(i-1, lock.getHoldCount());
428 >            assertEquals(i - 1, lock.getHoldCount());
429          }
430      }
431  
# Line 458 | Line 461 | public class ReentrantLockTest extends J
461              barrier.await();
462              awaitTermination(t);
463              assertFalse(lock.isLocked());
464 <        } catch (Exception e) {
462 <            threadUnexpectedException(e);
463 <        }
464 >        } catch (Exception fail) { threadUnexpectedException(fail); }
465      }
466  
467      /**
# Line 472 | Line 473 | public class ReentrantLockTest extends J
473          final PublicReentrantLock lock = new PublicReentrantLock(fair);
474          try {
475              lock.lockInterruptibly();
476 <        } catch (InterruptedException ie) {
476 <            threadUnexpectedException(ie);
477 <        }
476 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
477          assertLockedByMoi(lock);
478          Thread t = newStartedThread(new InterruptedLockRunnable(lock));
479          waitForQueuedThread(lock, t);
# Line 535 | Line 534 | public class ReentrantLockTest extends J
534              assertTrue(nanosRemaining <= 0);
535              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536              lock.unlock();
537 <        } catch (InterruptedException e) {
539 <            threadUnexpectedException(e);
540 <        }
537 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
538      }
539  
540      /**
# Line 555 | Line 552 | public class ReentrantLockTest extends J
552              assertFalse(c.await(timeoutMillis, MILLISECONDS));
553              assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
554              lock.unlock();
555 <        } catch (InterruptedException e) {
559 <            threadUnexpectedException(e);
560 <        }
555 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
556      }
557  
558      /**
# Line 570 | Line 565 | public class ReentrantLockTest extends J
565              final ReentrantLock lock = new ReentrantLock(fair);
566              final Condition c = lock.newCondition();
567              lock.lock();
568 <            long startTime = System.nanoTime();
569 <            long timeoutMillis = 10;
570 <            java.util.Date d = new java.util.Date();
571 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
572 <            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
568 >            // We shouldn't assume that nanoTime and currentTimeMillis
569 >            // use the same time source, so don't use nanoTime here.
570 >            java.util.Date delayedDate = delayedDate(timeoutMillis());
571 >            assertFalse(c.awaitUntil(delayedDate));
572 >            assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
573              lock.unlock();
574 <        } catch (InterruptedException e) {
580 <            threadUnexpectedException(e);
581 <        }
574 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
575      }
576  
577      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines