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.13 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.63 by jsr166, Fri Jul 3 05:48:30 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.concurrent.locks.*;
11 < import java.util.concurrent.*;
12 < import java.util.*;
13 < import java.io.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.HashSet;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.CyclicBarrier;
16 > import java.util.concurrent.locks.Condition;
17 > import java.util.concurrent.locks.ReentrantLock;
18 >
19 > import junit.framework.AssertionFailedError;
20 > import junit.framework.Test;
21 > 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);
28 >        return new TestSuite(ReentrantLockTest.class);
29      }
30  
31      /**
32 <     * A runnable calling lockInterruptibly
32 >     * A checked runnable calling lockInterruptibly
33       */
34 <    class InterruptibleLockRunnable implements Runnable {
34 >    class InterruptibleLockRunnable extends CheckedRunnable {
35          final ReentrantLock lock;
36 <        InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
37 <        public void run() {
38 <            try {
31 <                lock.lockInterruptibly();
32 <            } catch(InterruptedException success){}
36 >        InterruptibleLockRunnable(ReentrantLock lock) { this.lock = lock; }
37 >        public void realRun() throws InterruptedException {
38 >            lock.lockInterruptibly();
39          }
40      }
41  
36
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 implements Runnable {
46 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
47          final ReentrantLock lock;
48 <        InterruptedLockRunnable(ReentrantLock l) { lock = l; }
49 <        public void run() {
50 <            try {
46 <                lock.lockInterruptibly();
47 <                threadShouldThrow();
48 <            } catch(InterruptedException success){}
48 >        InterruptedLockRunnable(ReentrantLock lock) { this.lock = lock; }
49 >        public void realRun() throws InterruptedException {
50 >            lock.lockInterruptibly();
51          }
52      }
53  
# Line 54 | Line 56 | public class ReentrantLockTest extends J
56       */
57      static class PublicReentrantLock extends ReentrantLock {
58          PublicReentrantLock() { super(); }
59 <        public Collection<Thread> getQueuedThreads() {
60 <            return super.getQueuedThreads();
59 >        PublicReentrantLock(boolean fair) { super(fair); }
60 >        public Thread getOwner() {
61 >            return super.getOwner();
62          }
63 <        public Collection<Thread> getWaitingThreads(Condition c) {
64 <            return super.getWaitingThreads(c);
63 >        public Collection<Thread> getQueuedThreads() {
64 >            return super.getQueuedThreads();
65          }
66 +        public Collection<Thread> getWaitingThreads(Condition c) {
67 +            return super.getWaitingThreads(c);
68 +        }
69 +    }
70  
71 <
71 >    /**
72 >     * Releases write lock, checking that it had a hold count of 1.
73 >     */
74 >    void releaseLock(PublicReentrantLock lock) {
75 >        assertLockedByMoi(lock);
76 >        lock.unlock();
77 >        assertFalse(lock.isHeldByCurrentThread());
78 >        assertNotLocked(lock);
79      }
80  
81      /**
82 <     * Constructor sets given fairness
82 >     * Spin-waits until lock.hasQueuedThread(t) becomes true.
83       */
84 <    public void testConstructor() {
85 <        ReentrantLock rl = new ReentrantLock();
86 <        assertFalse(rl.isFair());
87 <        ReentrantLock r2 = new ReentrantLock(true);
88 <        assertTrue(r2.isFair());
84 >    void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
85 >        long startTime = System.nanoTime();
86 >        while (!lock.hasQueuedThread(t)) {
87 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
88 >                throw new AssertionFailedError("timed out");
89 >            Thread.yield();
90 >        }
91 >        assertTrue(t.isAlive());
92 >        assertNotSame(t, lock.getOwner());
93      }
94  
95      /**
96 <     * locking an unlocked lock succeeds
96 >     * Checks that lock is not locked.
97       */
98 <    public void testLock() {
99 <        ReentrantLock rl = new ReentrantLock();
100 <        rl.lock();
101 <        assertTrue(rl.isLocked());
102 <        rl.unlock();
98 >    void assertNotLocked(PublicReentrantLock lock) {
99 >        assertFalse(lock.isLocked());
100 >        assertFalse(lock.isHeldByCurrentThread());
101 >        assertNull(lock.getOwner());
102 >        assertEquals(0, lock.getHoldCount());
103      }
104  
105      /**
106 <     * locking an unlocked fair lock succeeds
106 >     * Checks that lock is locked by the given thread.
107       */
108 <    public void testFairLock() {
109 <        ReentrantLock rl = new ReentrantLock(true);
110 <        rl.lock();
111 <        assertTrue(rl.isLocked());
112 <        rl.unlock();
108 >    void assertLockedBy(PublicReentrantLock lock, Thread t) {
109 >        assertTrue(lock.isLocked());
110 >        assertSame(t, lock.getOwner());
111 >        assertEquals(t == Thread.currentThread(),
112 >                     lock.isHeldByCurrentThread());
113 >        assertEquals(t == Thread.currentThread(),
114 >                     lock.getHoldCount() > 0);
115      }
116  
117      /**
118 <     * Unlocking an unlocked lock throws IllegalMonitorStateException
118 >     * Checks that lock is locked by the current thread.
119       */
120 <    public void testUnlock_IllegalMonitorStateException() {
121 <        ReentrantLock rl = new ReentrantLock();
122 <        try {
103 <            rl.unlock();
104 <            shouldThrow();
120 >    void assertLockedByMoi(PublicReentrantLock lock) {
121 >        assertLockedBy(lock, Thread.currentThread());
122 >    }
123  
124 <        } catch(IllegalMonitorStateException success){}
124 >    /**
125 >     * Checks that condition c has no waiters.
126 >     */
127 >    void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
128 >        assertHasWaiters(lock, c, new Thread[] {});
129      }
130  
131      /**
132 <     * trylock on an unlocked lock succeeds
132 >     * Checks that condition c has exactly the given waiter threads.
133       */
134 <    public void testTryLock() {
135 <        ReentrantLock rl = new ReentrantLock();
136 <        assertTrue(rl.tryLock());
137 <        assertTrue(rl.isLocked());
138 <        rl.unlock();
134 >    void assertHasWaiters(PublicReentrantLock lock, Condition c,
135 >                          Thread... threads) {
136 >        lock.lock();
137 >        assertEquals(threads.length > 0, lock.hasWaiters(c));
138 >        assertEquals(threads.length, lock.getWaitQueueLength(c));
139 >        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
140 >        assertEquals(threads.length, lock.getWaitingThreads(c).size());
141 >        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
142 >                     new HashSet<Thread>(Arrays.asList(threads)));
143 >        lock.unlock();
144      }
145  
146 +    enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
147  
148      /**
149 <     * hasQueuedThreads reports whether there are waiting threads
149 >     * Awaits condition "indefinitely" using the specified AwaitMethod.
150       */
151 <    public void testhasQueuedThreads() {
152 <        final ReentrantLock lock = new ReentrantLock();
153 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
154 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
155 <        try {
156 <            assertFalse(lock.hasQueuedThreads());
157 <            lock.lock();
158 <            t1.start();
159 <            Thread.sleep(SHORT_DELAY_MS);
160 <            assertTrue(lock.hasQueuedThreads());
161 <            t2.start();
162 <            Thread.sleep(SHORT_DELAY_MS);
163 <            assertTrue(lock.hasQueuedThreads());
164 <            t1.interrupt();
165 <            Thread.sleep(SHORT_DELAY_MS);
166 <            assertTrue(lock.hasQueuedThreads());
167 <            lock.unlock();
168 <            Thread.sleep(SHORT_DELAY_MS);
169 <            assertFalse(lock.hasQueuedThreads());
170 <            t1.join();
171 <            t2.join();
144 <        } catch(Exception e){
145 <            unexpectedException();
151 >    void await(Condition c, AwaitMethod awaitMethod)
152 >            throws InterruptedException {
153 >        long timeoutMillis = 2 * LONG_DELAY_MS;
154 >        switch (awaitMethod) {
155 >        case await:
156 >            c.await();
157 >            break;
158 >        case awaitTimed:
159 >            assertTrue(c.await(timeoutMillis, MILLISECONDS));
160 >            break;
161 >        case awaitNanos:
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 <    }
173 >    }
174  
175      /**
176 <     * getQueueLength reports number of waiting threads
176 >     * Constructor sets given fairness, and is in unlocked state
177 >     */
178 >    public void testConstructor() {
179 >        PublicReentrantLock lock;
180 >
181 >        lock = new PublicReentrantLock();
182 >        assertFalse(lock.isFair());
183 >        assertNotLocked(lock);
184 >
185 >        lock = new PublicReentrantLock(true);
186 >        assertTrue(lock.isFair());
187 >        assertNotLocked(lock);
188 >
189 >        lock = new PublicReentrantLock(false);
190 >        assertFalse(lock.isFair());
191 >        assertNotLocked(lock);
192 >    }
193 >
194 >    /**
195 >     * locking an unlocked lock succeeds
196       */
197 <    public void testGetQueueLength() {
198 <        final ReentrantLock lock = new ReentrantLock();
199 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
200 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
201 <        try {
202 <            assertEquals(0, lock.getQueueLength());
203 <            lock.lock();
204 <            t1.start();
160 <            Thread.sleep(SHORT_DELAY_MS);
161 <            assertEquals(1, lock.getQueueLength());
162 <            t2.start();
163 <            Thread.sleep(SHORT_DELAY_MS);
164 <            assertEquals(2, lock.getQueueLength());
165 <            t1.interrupt();
166 <            Thread.sleep(SHORT_DELAY_MS);
167 <            assertEquals(1, lock.getQueueLength());
168 <            lock.unlock();
169 <            Thread.sleep(SHORT_DELAY_MS);
170 <            assertEquals(0, lock.getQueueLength());
171 <            t1.join();
172 <            t2.join();
173 <        } catch(Exception e){
174 <            unexpectedException();
175 <        }
176 <    }
197 >    public void testLock()      { testLock(false); }
198 >    public void testLock_fair() { testLock(true); }
199 >    public void testLock(boolean fair) {
200 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
201 >        lock.lock();
202 >        assertLockedByMoi(lock);
203 >        releaseLock(lock);
204 >    }
205  
206      /**
207 <     * getQueuedThreads includes waiting threads
207 >     * Unlocking an unlocked lock throws IllegalMonitorStateException
208       */
209 <    public void testGetQueuedThreads() {
210 <        final PublicReentrantLock lock = new PublicReentrantLock();
211 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
212 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
209 >    public void testUnlock_IMSE()      { testUnlock_IMSE(false); }
210 >    public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
211 >    public void testUnlock_IMSE(boolean fair) {
212 >        ReentrantLock lock = new ReentrantLock(fair);
213          try {
186            assertTrue(lock.getQueuedThreads().isEmpty());
187            lock.lock();
188            assertTrue(lock.getQueuedThreads().isEmpty());
189            t1.start();
190            Thread.sleep(SHORT_DELAY_MS);
191            assertTrue(lock.getQueuedThreads().contains(t1));
192            t2.start();
193            Thread.sleep(SHORT_DELAY_MS);
194            assertTrue(lock.getQueuedThreads().contains(t1));
195            assertTrue(lock.getQueuedThreads().contains(t2));
196            t1.interrupt();
197            Thread.sleep(SHORT_DELAY_MS);
198            assertFalse(lock.getQueuedThreads().contains(t1));
199            assertTrue(lock.getQueuedThreads().contains(t2));
214              lock.unlock();
215 <            Thread.sleep(SHORT_DELAY_MS);
216 <            assertTrue(lock.getQueuedThreads().isEmpty());
217 <            t1.join();
204 <            t2.join();
205 <        } catch(Exception e){
206 <            unexpectedException();
207 <        }
208 <    }
209 <
215 >            shouldThrow();
216 >        } catch (IllegalMonitorStateException success) {}
217 >    }
218  
219      /**
220 <     * timed trylock is interruptible.
220 >     * tryLock on an unlocked lock succeeds
221       */
222 <    public void testInterruptedException2() {
223 <        final ReentrantLock lock = new ReentrantLock();
224 <        lock.lock();
225 <        Thread t = new Thread(new Runnable() {
226 <                public void run() {
227 <                    try {
228 <                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
229 <                        threadShouldThrow();
230 <                    } catch(InterruptedException success){}
231 <                }
224 <            });
225 <        try {
226 <            t.start();
227 <            t.interrupt();
228 <        } catch(Exception e){
229 <            unexpectedException();
230 <        }
222 >    public void testTryLock()      { testTryLock(false); }
223 >    public void testTryLock_fair() { testTryLock(true); }
224 >    public void testTryLock(boolean fair) {
225 >        PublicReentrantLock lock = new PublicReentrantLock(fair);
226 >        assertTrue(lock.tryLock());
227 >        assertLockedByMoi(lock);
228 >        assertTrue(lock.tryLock());
229 >        assertLockedByMoi(lock);
230 >        lock.unlock();
231 >        releaseLock(lock);
232      }
233  
234 +    /**
235 +     * hasQueuedThreads reports whether there are waiting threads
236 +     */
237 +    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
238 +    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
239 +    public void testHasQueuedThreads(boolean fair) {
240 +        final PublicReentrantLock lock = new PublicReentrantLock(fair);
241 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
242 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
243 +        assertFalse(lock.hasQueuedThreads());
244 +        lock.lock();
245 +        assertFalse(lock.hasQueuedThreads());
246 +        t1.start();
247 +        waitForQueuedThread(lock, t1);
248 +        assertTrue(lock.hasQueuedThreads());
249 +        t2.start();
250 +        waitForQueuedThread(lock, t2);
251 +        assertTrue(lock.hasQueuedThreads());
252 +        t1.interrupt();
253 +        awaitTermination(t1);
254 +        assertTrue(lock.hasQueuedThreads());
255 +        lock.unlock();
256 +        awaitTermination(t2);
257 +        assertFalse(lock.hasQueuedThreads());
258 +    }
259  
260      /**
261 <     * Trylock on a locked lock fails
261 >     * getQueueLength reports number of waiting threads
262       */
263 <    public void testTryLockWhenLocked() {
264 <        final ReentrantLock lock = new ReentrantLock();
265 <        lock.lock();
266 <        Thread t = new Thread(new Runnable() {
267 <                public void run() {
268 <                    threadAssertFalse(lock.tryLock());
269 <                }
270 <            });
263 >    public void testGetQueueLength()      { testGetQueueLength(false); }
264 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
265 >    public void testGetQueueLength(boolean fair) {
266 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
267 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
268 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
269 >        assertEquals(0, lock.getQueueLength());
270 >        lock.lock();
271 >        t1.start();
272 >        waitForQueuedThread(lock, t1);
273 >        assertEquals(1, lock.getQueueLength());
274 >        t2.start();
275 >        waitForQueuedThread(lock, t2);
276 >        assertEquals(2, lock.getQueueLength());
277 >        t1.interrupt();
278 >        awaitTermination(t1);
279 >        assertEquals(1, lock.getQueueLength());
280 >        lock.unlock();
281 >        awaitTermination(t2);
282 >        assertEquals(0, lock.getQueueLength());
283 >    }
284 >
285 >    /**
286 >     * hasQueuedThread(null) throws NPE
287 >     */
288 >    public void testHasQueuedThreadNPE()      { testHasQueuedThreadNPE(false); }
289 >    public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
290 >    public void testHasQueuedThreadNPE(boolean fair) {
291 >        final ReentrantLock lock = new ReentrantLock(fair);
292          try {
293 <            t.start();
294 <            t.join();
295 <            lock.unlock();
296 <        } catch(Exception e){
250 <            unexpectedException();
251 <        }
252 <    }
293 >            lock.hasQueuedThread(null);
294 >            shouldThrow();
295 >        } catch (NullPointerException success) {}
296 >    }
297  
298      /**
299 <     * Timed trylock on a locked lock times out
299 >     * hasQueuedThread reports whether a thread is queued
300       */
301 <    public void testTryLock_Timeout() {
302 <        final ReentrantLock lock = new ReentrantLock();
303 <        lock.lock();
304 <        Thread t = new Thread(new Runnable() {
305 <                public void run() {
306 <                    try {
307 <                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
308 <                    } catch (Exception ex) {
309 <                        threadUnexpectedException();
310 <                    }
311 <                }
312 <            });
313 <        try {
314 <            t.start();
315 <            t.join();
316 <            lock.unlock();
317 <        } catch(Exception e){
318 <            unexpectedException();
319 <        }
320 <    }
321 <    
301 >    public void testHasQueuedThread()      { testHasQueuedThread(false); }
302 >    public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
303 >    public void testHasQueuedThread(boolean fair) {
304 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
305 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
306 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
307 >        assertFalse(lock.hasQueuedThread(t1));
308 >        assertFalse(lock.hasQueuedThread(t2));
309 >        lock.lock();
310 >        t1.start();
311 >        waitForQueuedThread(lock, t1);
312 >        assertTrue(lock.hasQueuedThread(t1));
313 >        assertFalse(lock.hasQueuedThread(t2));
314 >        t2.start();
315 >        waitForQueuedThread(lock, t2);
316 >        assertTrue(lock.hasQueuedThread(t1));
317 >        assertTrue(lock.hasQueuedThread(t2));
318 >        t1.interrupt();
319 >        awaitTermination(t1);
320 >        assertFalse(lock.hasQueuedThread(t1));
321 >        assertTrue(lock.hasQueuedThread(t2));
322 >        lock.unlock();
323 >        awaitTermination(t2);
324 >        assertFalse(lock.hasQueuedThread(t1));
325 >        assertFalse(lock.hasQueuedThread(t2));
326 >    }
327 >
328      /**
329 <     * getHoldCount returns number of recursive holds
329 >     * getQueuedThreads includes waiting threads
330       */
331 <    public void testGetHoldCount() {
332 <        ReentrantLock lock = new ReentrantLock();
333 <        for(int i = 1; i <= SIZE; i++) {
334 <            lock.lock();
335 <            assertEquals(i,lock.getHoldCount());
336 <        }
337 <        for(int i = SIZE; i > 0; i--) {
338 <            lock.unlock();
339 <            assertEquals(i-1,lock.getHoldCount());
340 <        }
331 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
332 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
333 >    public void testGetQueuedThreads(boolean fair) {
334 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
335 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
336 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
337 >        assertTrue(lock.getQueuedThreads().isEmpty());
338 >        lock.lock();
339 >        assertTrue(lock.getQueuedThreads().isEmpty());
340 >        t1.start();
341 >        waitForQueuedThread(lock, t1);
342 >        assertEquals(1, lock.getQueuedThreads().size());
343 >        assertTrue(lock.getQueuedThreads().contains(t1));
344 >        t2.start();
345 >        waitForQueuedThread(lock, t2);
346 >        assertEquals(2, lock.getQueuedThreads().size());
347 >        assertTrue(lock.getQueuedThreads().contains(t1));
348 >        assertTrue(lock.getQueuedThreads().contains(t2));
349 >        t1.interrupt();
350 >        awaitTermination(t1);
351 >        assertFalse(lock.getQueuedThreads().contains(t1));
352 >        assertTrue(lock.getQueuedThreads().contains(t2));
353 >        assertEquals(1, lock.getQueuedThreads().size());
354 >        lock.unlock();
355 >        awaitTermination(t2);
356 >        assertTrue(lock.getQueuedThreads().isEmpty());
357 >    }
358 >
359 >    /**
360 >     * timed tryLock is interruptible
361 >     */
362 >    public void testTryLock_Interruptible()      { testTryLock_Interruptible(false); }
363 >    public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
364 >    public void testTryLock_Interruptible(boolean fair) {
365 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
366 >        lock.lock();
367 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
368 >            public void realRun() throws InterruptedException {
369 >                lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
370 >            }});
371 >
372 >        waitForQueuedThread(lock, t);
373 >        t.interrupt();
374 >        awaitTermination(t);
375 >        releaseLock(lock);
376 >    }
377 >
378 >    /**
379 >     * tryLock on a locked lock fails
380 >     */
381 >    public void testTryLockWhenLocked()      { testTryLockWhenLocked(false); }
382 >    public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
383 >    public void testTryLockWhenLocked(boolean fair) {
384 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
385 >        lock.lock();
386 >        Thread t = newStartedThread(new CheckedRunnable() {
387 >            public void realRun() {
388 >                assertFalse(lock.tryLock());
389 >            }});
390 >
391 >        awaitTermination(t);
392 >        releaseLock(lock);
393 >    }
394 >
395 >    /**
396 >     * Timed tryLock on a locked lock times out
397 >     */
398 >    public void testTryLock_Timeout()      { testTryLock_Timeout(false); }
399 >    public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
400 >    public void testTryLock_Timeout(boolean fair) {
401 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
402 >        lock.lock();
403 >        Thread t = newStartedThread(new CheckedRunnable() {
404 >            public void realRun() throws InterruptedException {
405 >                long startTime = System.nanoTime();
406 >                long timeoutMillis = 10;
407 >                assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
408 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
409 >            }});
410 >
411 >        awaitTermination(t);
412 >        releaseLock(lock);
413      }
414 <    
293 <  
414 >
415      /**
416 <     * isLocked is true when locked and false when not
416 >     * getHoldCount returns number of recursive holds
417       */
418 <    public void testIsLocked() {
419 <        final ReentrantLock lock = new ReentrantLock();
420 <        lock.lock();
421 <        assertTrue(lock.isLocked());
422 <        lock.unlock();
423 <        assertFalse(lock.isLocked());
424 <        Thread t = new Thread(new Runnable() {
425 <                public void run() {
426 <                    lock.lock();
427 <                    try {
428 <                        Thread.sleep(SMALL_DELAY_MS);
308 <                    }
309 <                    catch(Exception e) {
310 <                        threadUnexpectedException();
311 <                    }
312 <                    lock.unlock();
313 <                }
314 <            });
315 <        try {
316 <            t.start();
317 <            Thread.sleep(SHORT_DELAY_MS);
318 <            assertTrue(lock.isLocked());
319 <            t.join();
320 <            assertFalse(lock.isLocked());
321 <        } catch(Exception e){
322 <            unexpectedException();
418 >    public void testGetHoldCount()      { testGetHoldCount(false); }
419 >    public void testGetHoldCount_fair() { testGetHoldCount(true); }
420 >    public void testGetHoldCount(boolean fair) {
421 >        ReentrantLock lock = new ReentrantLock(fair);
422 >        for (int i = 1; i <= SIZE; i++) {
423 >            lock.lock();
424 >            assertEquals(i, lock.getHoldCount());
425 >        }
426 >        for (int i = SIZE; i > 0; i--) {
427 >            lock.unlock();
428 >            assertEquals(i - 1, lock.getHoldCount());
429          }
430      }
431  
326
432      /**
433 <     * lockInterruptibly is interruptible.
433 >     * isLocked is true when locked and false when not
434       */
435 <    public void testLockInterruptibly1() {
436 <        final ReentrantLock lock = new ReentrantLock();
437 <        lock.lock();
333 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
435 >    public void testIsLocked()      { testIsLocked(false); }
436 >    public void testIsLocked_fair() { testIsLocked(true); }
437 >    public void testIsLocked(boolean fair) {
438          try {
439 <            t.start();
440 <            t.interrupt();
439 >            final ReentrantLock lock = new ReentrantLock(fair);
440 >            assertFalse(lock.isLocked());
441 >            lock.lock();
442 >            assertTrue(lock.isLocked());
443 >            lock.lock();
444 >            assertTrue(lock.isLocked());
445              lock.unlock();
446 <            t.join();
447 <        } catch(Exception e){
448 <            unexpectedException();
449 <        }
450 <    }
446 >            assertTrue(lock.isLocked());
447 >            lock.unlock();
448 >            assertFalse(lock.isLocked());
449 >            final CyclicBarrier barrier = new CyclicBarrier(2);
450 >            Thread t = newStartedThread(new CheckedRunnable() {
451 >                    public void realRun() throws Exception {
452 >                        lock.lock();
453 >                        assertTrue(lock.isLocked());
454 >                        barrier.await();
455 >                        barrier.await();
456 >                        lock.unlock();
457 >                    }});
458 >
459 >            barrier.await();
460 >            assertTrue(lock.isLocked());
461 >            barrier.await();
462 >            awaitTermination(t);
463 >            assertFalse(lock.isLocked());
464 >        } catch (Exception fail) { threadUnexpectedException(fail); }
465 >    }
466  
467      /**
468       * lockInterruptibly succeeds when unlocked, else is interruptible
469       */
470 <    public void testLockInterruptibly2() {
471 <        final ReentrantLock lock = new ReentrantLock();
472 <        try {
473 <            lock.lockInterruptibly();
351 <        } catch(Exception e) {
352 <            unexpectedException();
353 <        }
354 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
470 >    public void testLockInterruptibly()      { testLockInterruptibly(false); }
471 >    public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
472 >    public void testLockInterruptibly(boolean fair) {
473 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
474          try {
475 <            t.start();
476 <            t.interrupt();
477 <            assertTrue(lock.isLocked());
478 <            assertTrue(lock.isHeldByCurrentThread());
479 <            t.join();
480 <        } catch(Exception e){
481 <            unexpectedException();
482 <        }
475 >            lock.lockInterruptibly();
476 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
477 >        assertLockedByMoi(lock);
478 >        Thread t = newStartedThread(new InterruptedLockRunnable(lock));
479 >        waitForQueuedThread(lock, t);
480 >        t.interrupt();
481 >        assertTrue(lock.isLocked());
482 >        assertTrue(lock.isHeldByCurrentThread());
483 >        awaitTermination(t);
484 >        releaseLock(lock);
485      }
486  
487      /**
488       * Calling await without holding lock throws IllegalMonitorStateException
489       */
490 <    public void testAwait_IllegalMonitor() {
491 <        final ReentrantLock lock = new ReentrantLock();
490 >    public void testAwait_IMSE()      { testAwait_IMSE(false); }
491 >    public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
492 >    public void testAwait_IMSE(boolean fair) {
493 >        final ReentrantLock lock = new ReentrantLock(fair);
494          final Condition c = lock.newCondition();
495 <        try {
496 <            c.await();
497 <            shouldThrow();
498 <        }
499 <        catch (IllegalMonitorStateException success) {
500 <        }
501 <        catch (Exception ex) {
502 <            unexpectedException();
495 >        for (AwaitMethod awaitMethod : AwaitMethod.values()) {
496 >            long startTime = System.nanoTime();
497 >            try {
498 >                await(c, awaitMethod);
499 >                shouldThrow();
500 >            } catch (IllegalMonitorStateException success) {
501 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
502 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
503          }
504      }
505  
506      /**
507       * Calling signal without holding lock throws IllegalMonitorStateException
508       */
509 <    public void testSignal_IllegalMonitor() {
510 <        final ReentrantLock lock = new ReentrantLock();
509 >    public void testSignal_IMSE()      { testSignal_IMSE(false); }
510 >    public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
511 >    public void testSignal_IMSE(boolean fair) {
512 >        final ReentrantLock lock = new ReentrantLock(fair);
513          final Condition c = lock.newCondition();
514          try {
515              c.signal();
516              shouldThrow();
517 <        }
393 <        catch (IllegalMonitorStateException success) {
394 <        }
395 <        catch (Exception ex) {
396 <            unexpectedException();
397 <        }
517 >        } catch (IllegalMonitorStateException success) {}
518      }
519  
520      /**
521       * awaitNanos without a signal times out
522       */
523 <    public void testAwaitNanos_Timeout() {
524 <        final ReentrantLock lock = new ReentrantLock();
525 <        final Condition c = lock.newCondition();
523 >    public void testAwaitNanos_Timeout()      { testAwaitNanos_Timeout(false); }
524 >    public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
525 >    public void testAwaitNanos_Timeout(boolean fair) {
526          try {
527 +            final ReentrantLock lock = new ReentrantLock(fair);
528 +            final Condition c = lock.newCondition();
529              lock.lock();
530 <            long t = c.awaitNanos(100);
531 <            assertTrue(t <= 0);
530 >            long startTime = System.nanoTime();
531 >            long timeoutMillis = 10;
532 >            long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
533 >            long nanosRemaining = c.awaitNanos(timeoutNanos);
534 >            assertTrue(nanosRemaining <= 0);
535 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
536              lock.unlock();
537 <        }
412 <        catch (Exception ex) {
413 <            unexpectedException();
414 <        }
537 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
538      }
539  
540      /**
541 <     *  timed await without a signal times out
541 >     * timed await without a signal times out
542       */
543 <    public void testAwait_Timeout() {
544 <        final ReentrantLock lock = new ReentrantLock();
545 <        final Condition c = lock.newCondition();
543 >    public void testAwait_Timeout()      { testAwait_Timeout(false); }
544 >    public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
545 >    public void testAwait_Timeout(boolean fair) {
546          try {
547 +            final ReentrantLock lock = new ReentrantLock(fair);
548 +            final Condition c = lock.newCondition();
549              lock.lock();
550 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
550 >            long startTime = System.nanoTime();
551 >            long timeoutMillis = 10;
552 >            assertFalse(c.await(timeoutMillis, MILLISECONDS));
553 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
554              lock.unlock();
555 <        }
428 <        catch (Exception ex) {
429 <            unexpectedException();
430 <        }
555 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
556      }
557  
558      /**
559       * awaitUntil without a signal times out
560       */
561 <    public void testAwaitUntil_Timeout() {
562 <        final ReentrantLock lock = new ReentrantLock();
563 <        final Condition c = lock.newCondition();
561 >    public void testAwaitUntil_Timeout()      { testAwaitUntil_Timeout(false); }
562 >    public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
563 >    public void testAwaitUntil_Timeout(boolean fair) {
564          try {
565 +            final ReentrantLock lock = new ReentrantLock(fair);
566 +            final Condition c = lock.newCondition();
567              lock.lock();
568 <            java.util.Date d = new java.util.Date();
569 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
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 <        }
445 <        catch (Exception ex) {
446 <            unexpectedException();
447 <        }
574 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
575      }
576  
577      /**
578       * await returns when signalled
579       */
580 <    public void testAwait() {
581 <        final ReentrantLock lock = new ReentrantLock();
580 >    public void testAwait()      { testAwait(false); }
581 >    public void testAwait_fair() { testAwait(true); }
582 >    public void testAwait(boolean fair) {
583 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
584          final Condition c = lock.newCondition();
585 <        Thread t = new Thread(new Runnable() {
586 <                public void run() {
587 <                    try {
588 <                        lock.lock();
589 <                        c.await();
590 <                        lock.unlock();
591 <                    }
592 <                    catch(InterruptedException e) {
593 <                        threadUnexpectedException();
594 <                    }
595 <                }
596 <            });
585 >        final CountDownLatch locked = new CountDownLatch(1);
586 >        Thread t = newStartedThread(new CheckedRunnable() {
587 >            public void realRun() throws InterruptedException {
588 >                lock.lock();
589 >                locked.countDown();
590 >                c.await();
591 >                lock.unlock();
592 >            }});
593 >
594 >        await(locked);
595 >        lock.lock();
596 >        assertHasWaiters(lock, c, t);
597 >        c.signal();
598 >        assertHasNoWaiters(lock, c);
599 >        assertTrue(t.isAlive());
600 >        lock.unlock();
601 >        awaitTermination(t);
602 >    }
603  
604 +    /**
605 +     * hasWaiters throws NPE if null
606 +     */
607 +    public void testHasWaitersNPE()      { testHasWaitersNPE(false); }
608 +    public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
609 +    public void testHasWaitersNPE(boolean fair) {
610 +        final ReentrantLock lock = new ReentrantLock(fair);
611          try {
612 <            t.start();
613 <            Thread.sleep(SHORT_DELAY_MS);
614 <            lock.lock();
615 <            c.signal();
616 <            lock.unlock();
617 <            t.join(SHORT_DELAY_MS);
618 <            assertFalse(t.isAlive());
619 <        }
620 <        catch (Exception ex) {
621 <            unexpectedException();
622 <        }
612 >            lock.hasWaiters(null);
613 >            shouldThrow();
614 >        } catch (NullPointerException success) {}
615 >    }
616 >
617 >    /**
618 >     * getWaitQueueLength throws NPE if null
619 >     */
620 >    public void testGetWaitQueueLengthNPE()      { testGetWaitQueueLengthNPE(false); }
621 >    public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
622 >    public void testGetWaitQueueLengthNPE(boolean fair) {
623 >        final ReentrantLock lock = new ReentrantLock(fair);
624 >        try {
625 >            lock.getWaitQueueLength(null);
626 >            shouldThrow();
627 >        } catch (NullPointerException success) {}
628      }
629  
630      /**
631 <     * hasWaiters throws IAE if not owned
631 >     * getWaitingThreads throws NPE if null
632       */
633 <    public void testHasWaitersIAE() {
634 <        final ReentrantLock lock = new ReentrantLock();
635 <        final Condition c = (lock.newCondition());
636 <        final ReentrantLock lock2 = new ReentrantLock();
633 >    public void testGetWaitingThreadsNPE()      { testGetWaitingThreadsNPE(false); }
634 >    public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
635 >    public void testGetWaitingThreadsNPE(boolean fair) {
636 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
637 >        try {
638 >            lock.getWaitingThreads(null);
639 >            shouldThrow();
640 >        } catch (NullPointerException success) {}
641 >    }
642 >
643 >    /**
644 >     * hasWaiters throws IllegalArgumentException if not owned
645 >     */
646 >    public void testHasWaitersIAE()      { testHasWaitersIAE(false); }
647 >    public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
648 >    public void testHasWaitersIAE(boolean fair) {
649 >        final ReentrantLock lock = new ReentrantLock(fair);
650 >        final Condition c = lock.newCondition();
651 >        final ReentrantLock lock2 = new ReentrantLock(fair);
652          try {
653              lock2.hasWaiters(c);
654              shouldThrow();
655 <        } catch (IllegalArgumentException success) {
494 <        } catch (Exception ex) {
495 <            unexpectedException();
496 <        }
655 >        } catch (IllegalArgumentException success) {}
656      }
657  
658      /**
659 <     * hasWaiters throws IMSE if not locked
659 >     * hasWaiters throws IllegalMonitorStateException if not locked
660       */
661 <    public void testHasWaitersIMSE() {
662 <        final ReentrantLock lock = new ReentrantLock();
663 <        final Condition c = (lock.newCondition());
661 >    public void testHasWaitersIMSE()      { testHasWaitersIMSE(false); }
662 >    public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
663 >    public void testHasWaitersIMSE(boolean fair) {
664 >        final ReentrantLock lock = new ReentrantLock(fair);
665 >        final Condition c = lock.newCondition();
666          try {
667              lock.hasWaiters(c);
668              shouldThrow();
669 <        } catch (IllegalMonitorStateException success) {
509 <        } catch (Exception ex) {
510 <            unexpectedException();
511 <        }
669 >        } catch (IllegalMonitorStateException success) {}
670      }
671  
514
672      /**
673 <     * getWaitQueueLength throws IAE if not owned
673 >     * getWaitQueueLength throws IllegalArgumentException if not owned
674       */
675 <    public void testGetWaitQueueLengthIAE() {
676 <        final ReentrantLock lock = new ReentrantLock();
677 <        final Condition c = (lock.newCondition());
678 <        final ReentrantLock lock2 = new ReentrantLock();
675 >    public void testGetWaitQueueLengthIAE()      { testGetWaitQueueLengthIAE(false); }
676 >    public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
677 >    public void testGetWaitQueueLengthIAE(boolean fair) {
678 >        final ReentrantLock lock = new ReentrantLock(fair);
679 >        final Condition c = lock.newCondition();
680 >        final ReentrantLock lock2 = new ReentrantLock(fair);
681          try {
682              lock2.getWaitQueueLength(c);
683              shouldThrow();
684 <        } catch (IllegalArgumentException success) {
526 <        } catch (Exception ex) {
527 <            unexpectedException();
528 <        }
684 >        } catch (IllegalArgumentException success) {}
685      }
686  
687      /**
688 <     * getWaitQueueLength throws IMSE if not locked
688 >     * getWaitQueueLength throws IllegalMonitorStateException if not locked
689       */
690 <    public void testGetWaitQueueLengthIMSE() {
691 <        final ReentrantLock lock = new ReentrantLock();
692 <        final Condition c = (lock.newCondition());
690 >    public void testGetWaitQueueLengthIMSE()      { testGetWaitQueueLengthIMSE(false); }
691 >    public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
692 >    public void testGetWaitQueueLengthIMSE(boolean fair) {
693 >        final ReentrantLock lock = new ReentrantLock(fair);
694 >        final Condition c = lock.newCondition();
695          try {
696              lock.getWaitQueueLength(c);
697              shouldThrow();
698 <        } catch (IllegalMonitorStateException success) {
541 <        } catch (Exception ex) {
542 <            unexpectedException();
543 <        }
698 >        } catch (IllegalMonitorStateException success) {}
699      }
700  
546
701      /**
702 <     * getWaitingThreads throws IAE if not owned
702 >     * getWaitingThreads throws IllegalArgumentException if not owned
703       */
704 <    public void testGetWaitingThreadsIAE() {
705 <        final PublicReentrantLock lock = new PublicReentrantLock();    
706 <        final Condition c = (lock.newCondition());
707 <        final PublicReentrantLock lock2 = new PublicReentrantLock();    
704 >    public void testGetWaitingThreadsIAE()      { testGetWaitingThreadsIAE(false); }
705 >    public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
706 >    public void testGetWaitingThreadsIAE(boolean fair) {
707 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
708 >        final Condition c = lock.newCondition();
709 >        final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
710          try {
711              lock2.getWaitingThreads(c);
712              shouldThrow();
713 <        } catch (IllegalArgumentException success) {
558 <        } catch (Exception ex) {
559 <            unexpectedException();
560 <        }
713 >        } catch (IllegalArgumentException success) {}
714      }
715  
716      /**
717 <     * getWaitingThreads throws IMSE if not locked
717 >     * getWaitingThreads throws IllegalMonitorStateException if not locked
718       */
719 <    public void testGetWaitingThreadsIMSE() {
720 <        final PublicReentrantLock lock = new PublicReentrantLock();    
721 <        final Condition c = (lock.newCondition());
719 >    public void testGetWaitingThreadsIMSE()      { testGetWaitingThreadsIMSE(false); }
720 >    public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
721 >    public void testGetWaitingThreadsIMSE(boolean fair) {
722 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
723 >        final Condition c = lock.newCondition();
724          try {
725              lock.getWaitingThreads(c);
726              shouldThrow();
727 <        } catch (IllegalMonitorStateException success) {
573 <        } catch (Exception ex) {
574 <            unexpectedException();
575 <        }
727 >        } catch (IllegalMonitorStateException success) {}
728      }
729  
578
579
730      /**
731       * hasWaiters returns true when a thread is waiting, else false
732       */
733 <    public void testHasWaiters() {
734 <        final ReentrantLock lock = new ReentrantLock();
733 >    public void testHasWaiters()      { testHasWaiters(false); }
734 >    public void testHasWaiters_fair() { testHasWaiters(true); }
735 >    public void testHasWaiters(boolean fair) {
736 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
737          final Condition c = lock.newCondition();
738 <        Thread t = new Thread(new Runnable() {
739 <                public void run() {
740 <                    try {
741 <                        lock.lock();
742 <                        threadAssertFalse(lock.hasWaiters(c));
743 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
744 <                        c.await();
745 <                        lock.unlock();
746 <                    }
747 <                    catch(InterruptedException e) {
748 <                        threadUnexpectedException();
749 <                    }
750 <                }
751 <            });
752 <
753 <        try {
754 <            t.start();
755 <            Thread.sleep(SHORT_DELAY_MS);
756 <            lock.lock();
757 <            assertTrue(lock.hasWaiters(c));
758 <            assertEquals(1, lock.getWaitQueueLength(c));
759 <            c.signal();
760 <            lock.unlock();
609 <            Thread.sleep(SHORT_DELAY_MS);
610 <            lock.lock();
611 <            assertFalse(lock.hasWaiters(c));
612 <            assertEquals(0, lock.getWaitQueueLength(c));
613 <            lock.unlock();
614 <            t.join(SHORT_DELAY_MS);
615 <            assertFalse(t.isAlive());
616 <        }
617 <        catch (Exception ex) {
618 <            unexpectedException();
619 <        }
738 >        final CountDownLatch pleaseSignal = new CountDownLatch(1);
739 >        Thread t = newStartedThread(new CheckedRunnable() {
740 >            public void realRun() throws InterruptedException {
741 >                lock.lock();
742 >                assertHasNoWaiters(lock, c);
743 >                assertFalse(lock.hasWaiters(c));
744 >                pleaseSignal.countDown();
745 >                c.await();
746 >                assertHasNoWaiters(lock, c);
747 >                assertFalse(lock.hasWaiters(c));
748 >                lock.unlock();
749 >            }});
750 >
751 >        await(pleaseSignal);
752 >        lock.lock();
753 >        assertHasWaiters(lock, c, t);
754 >        assertTrue(lock.hasWaiters(c));
755 >        c.signal();
756 >        assertHasNoWaiters(lock, c);
757 >        assertFalse(lock.hasWaiters(c));
758 >        lock.unlock();
759 >        awaitTermination(t);
760 >        assertHasNoWaiters(lock, c);
761      }
762  
763      /**
764       * getWaitQueueLength returns number of waiting threads
765       */
766 <    public void testGetWaitQueueLength() {
767 <        final ReentrantLock lock = new ReentrantLock();
766 >    public void testGetWaitQueueLength()      { testGetWaitQueueLength(false); }
767 >    public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
768 >    public void testGetWaitQueueLength(boolean fair) {
769 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
770          final Condition c = lock.newCondition();
771 <        Thread t1 = new Thread(new Runnable() {
772 <                public void run() {
773 <                    try {
774 <                        lock.lock();
775 <                        threadAssertFalse(lock.hasWaiters(c));
776 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
777 <                        c.await();
778 <                        lock.unlock();
779 <                    }
780 <                    catch(InterruptedException e) {
781 <                        threadUnexpectedException();
782 <                    }
783 <                }
784 <            });
785 <
786 <        Thread t2 = new Thread(new Runnable() {
787 <                public void run() {
788 <                    try {
789 <                        lock.lock();
790 <                        threadAssertTrue(lock.hasWaiters(c));
791 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
792 <                        c.await();
793 <                        lock.unlock();
794 <                    }
795 <                    catch(InterruptedException e) {
796 <                        threadUnexpectedException();
797 <                    }
798 <                }
799 <            });
771 >        final CountDownLatch locked1 = new CountDownLatch(1);
772 >        final CountDownLatch locked2 = new CountDownLatch(1);
773 >        Thread t1 = new Thread(new CheckedRunnable() {
774 >            public void realRun() throws InterruptedException {
775 >                lock.lock();
776 >                assertFalse(lock.hasWaiters(c));
777 >                assertEquals(0, lock.getWaitQueueLength(c));
778 >                locked1.countDown();
779 >                c.await();
780 >                lock.unlock();
781 >            }});
782 >
783 >        Thread t2 = new Thread(new CheckedRunnable() {
784 >            public void realRun() throws InterruptedException {
785 >                lock.lock();
786 >                assertTrue(lock.hasWaiters(c));
787 >                assertEquals(1, lock.getWaitQueueLength(c));
788 >                locked2.countDown();
789 >                c.await();
790 >                lock.unlock();
791 >            }});
792 >
793 >        lock.lock();
794 >        assertEquals(0, lock.getWaitQueueLength(c));
795 >        lock.unlock();
796 >
797 >        t1.start();
798 >        await(locked1);
799 >
800 >        lock.lock();
801 >        assertHasWaiters(lock, c, t1);
802 >        assertEquals(1, lock.getWaitQueueLength(c));
803 >        lock.unlock();
804 >
805 >        t2.start();
806 >        await(locked2);
807 >
808 >        lock.lock();
809 >        assertHasWaiters(lock, c, t1, t2);
810 >        assertEquals(2, lock.getWaitQueueLength(c));
811 >        c.signalAll();
812 >        assertHasNoWaiters(lock, c);
813 >        lock.unlock();
814  
815 <        try {
816 <            t1.start();
817 <            Thread.sleep(SHORT_DELAY_MS);
818 <            t2.start();
662 <            Thread.sleep(SHORT_DELAY_MS);
663 <            lock.lock();
664 <            assertTrue(lock.hasWaiters(c));
665 <            assertEquals(2, lock.getWaitQueueLength(c));
666 <            c.signalAll();
667 <            lock.unlock();
668 <            Thread.sleep(SHORT_DELAY_MS);
669 <            lock.lock();
670 <            assertFalse(lock.hasWaiters(c));
671 <            assertEquals(0, lock.getWaitQueueLength(c));
672 <            lock.unlock();
673 <            t1.join(SHORT_DELAY_MS);
674 <            t2.join(SHORT_DELAY_MS);
675 <            assertFalse(t1.isAlive());
676 <            assertFalse(t2.isAlive());
677 <        }
678 <        catch (Exception ex) {
679 <            unexpectedException();
680 <        }
815 >        awaitTermination(t1);
816 >        awaitTermination(t2);
817 >
818 >        assertHasNoWaiters(lock, c);
819      }
820  
821      /**
822       * getWaitingThreads returns only and all waiting threads
823       */
824 <    public void testGetWaitingThreads() {
825 <        final PublicReentrantLock lock = new PublicReentrantLock();    
824 >    public void testGetWaitingThreads()      { testGetWaitingThreads(false); }
825 >    public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
826 >    public void testGetWaitingThreads(boolean fair) {
827 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
828          final Condition c = lock.newCondition();
829 <        Thread t1 = new Thread(new Runnable() {
830 <                public void run() {
831 <                    try {
832 <                        lock.lock();
833 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
834 <                        c.await();
835 <                        lock.unlock();
836 <                    }
837 <                    catch(InterruptedException e) {
838 <                        threadUnexpectedException();
839 <                    }
840 <                }
841 <            });
842 <
843 <        Thread t2 = new Thread(new Runnable() {
844 <                public void run() {
845 <                    try {
846 <                        lock.lock();
847 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
848 <                        c.await();
849 <                        lock.unlock();
850 <                    }
851 <                    catch(InterruptedException e) {
852 <                        threadUnexpectedException();
853 <                    }
854 <                }
855 <            });
856 <
857 <        try {
858 <            lock.lock();
859 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
860 <            lock.unlock();
861 <            t1.start();
862 <            Thread.sleep(SHORT_DELAY_MS);
863 <            t2.start();
864 <            Thread.sleep(SHORT_DELAY_MS);
865 <            lock.lock();
866 <            assertTrue(lock.hasWaiters(c));
867 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
868 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
869 <            c.signalAll();
870 <            lock.unlock();
871 <            Thread.sleep(SHORT_DELAY_MS);
872 <            lock.lock();
873 <            assertFalse(lock.hasWaiters(c));
874 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
875 <            lock.unlock();
876 <            t1.join(SHORT_DELAY_MS);
877 <            t2.join(SHORT_DELAY_MS);
878 <            assertFalse(t1.isAlive());
739 <            assertFalse(t2.isAlive());
740 <        }
741 <        catch (Exception ex) {
742 <            unexpectedException();
743 <        }
829 >        final CountDownLatch locked1 = new CountDownLatch(1);
830 >        final CountDownLatch locked2 = new CountDownLatch(1);
831 >        Thread t1 = new Thread(new CheckedRunnable() {
832 >            public void realRun() throws InterruptedException {
833 >                lock.lock();
834 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
835 >                locked1.countDown();
836 >                c.await();
837 >                lock.unlock();
838 >            }});
839 >
840 >        Thread t2 = new Thread(new CheckedRunnable() {
841 >            public void realRun() throws InterruptedException {
842 >                lock.lock();
843 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
844 >                locked2.countDown();
845 >                c.await();
846 >                lock.unlock();
847 >            }});
848 >
849 >        lock.lock();
850 >        assertTrue(lock.getWaitingThreads(c).isEmpty());
851 >        lock.unlock();
852 >
853 >        t1.start();
854 >        await(locked1);
855 >
856 >        lock.lock();
857 >        assertHasWaiters(lock, c, t1);
858 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
859 >        assertFalse(lock.getWaitingThreads(c).contains(t2));
860 >        assertEquals(1, lock.getWaitingThreads(c).size());
861 >        lock.unlock();
862 >
863 >        t2.start();
864 >        await(locked2);
865 >
866 >        lock.lock();
867 >        assertHasWaiters(lock, c, t1, t2);
868 >        assertTrue(lock.getWaitingThreads(c).contains(t1));
869 >        assertTrue(lock.getWaitingThreads(c).contains(t2));
870 >        assertEquals(2, lock.getWaitingThreads(c).size());
871 >        c.signalAll();
872 >        assertHasNoWaiters(lock, c);
873 >        lock.unlock();
874 >
875 >        awaitTermination(t1);
876 >        awaitTermination(t2);
877 >
878 >        assertHasNoWaiters(lock, c);
879      }
880  
746
747
881      /**
882 <     * awaitUninterruptibly doesn't abort on interrupt
883 <     */
884 <    public void testAwaitUninterruptibly() {
885 <        final ReentrantLock lock = new ReentrantLock();
882 >     * awaitUninterruptibly is uninterruptible
883 >     */
884 >    public void testAwaitUninterruptibly()      { testAwaitUninterruptibly(false); }
885 >    public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
886 >    public void testAwaitUninterruptibly(boolean fair) {
887 >        final ReentrantLock lock = new ReentrantLock(fair);
888          final Condition c = lock.newCondition();
889 <        Thread t = new Thread(new Runnable() {
755 <                public void run() {
756 <                    lock.lock();
757 <                    c.awaitUninterruptibly();
758 <                    lock.unlock();
759 <                }
760 <            });
889 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
890  
891 <        try {
892 <            t.start();
893 <            Thread.sleep(SHORT_DELAY_MS);
894 <            t.interrupt();
895 <            lock.lock();
896 <            c.signal();
897 <            lock.unlock();
898 <            assert(t.isInterrupted());
899 <            t.join(SHORT_DELAY_MS);
900 <            assertFalse(t.isAlive());
901 <        }
902 <        catch (Exception ex) {
903 <            unexpectedException();
904 <        }
891 >        Thread t1 = newStartedThread(new CheckedRunnable() {
892 >            public void realRun() {
893 >                // Interrupt before awaitUninterruptibly
894 >                lock.lock();
895 >                pleaseInterrupt.countDown();
896 >                Thread.currentThread().interrupt();
897 >                c.awaitUninterruptibly();
898 >                assertTrue(Thread.interrupted());
899 >                lock.unlock();
900 >            }});
901 >
902 >        Thread t2 = newStartedThread(new CheckedRunnable() {
903 >            public void realRun() {
904 >                // Interrupt during awaitUninterruptibly
905 >                lock.lock();
906 >                pleaseInterrupt.countDown();
907 >                c.awaitUninterruptibly();
908 >                assertTrue(Thread.interrupted());
909 >                lock.unlock();
910 >            }});
911 >
912 >        await(pleaseInterrupt);
913 >        lock.lock();
914 >        lock.unlock();
915 >        t2.interrupt();
916 >
917 >        assertThreadStaysAlive(t1);
918 >        assertTrue(t2.isAlive());
919 >
920 >        lock.lock();
921 >        c.signalAll();
922 >        lock.unlock();
923 >
924 >        awaitTermination(t1);
925 >        awaitTermination(t2);
926      }
927  
928      /**
929 <     * await is interruptible
930 <     */
931 <    public void testAwait_Interrupt() {
932 <        final ReentrantLock lock = new ReentrantLock();
929 >     * await/awaitNanos/awaitUntil is interruptible
930 >     */
931 >    public void testInterruptible_await()           { testInterruptible(false, AwaitMethod.await); }
932 >    public void testInterruptible_await_fair()      { testInterruptible(true,  AwaitMethod.await); }
933 >    public void testInterruptible_awaitTimed()      { testInterruptible(false, AwaitMethod.awaitTimed); }
934 >    public void testInterruptible_awaitTimed_fair() { testInterruptible(true,  AwaitMethod.awaitTimed); }
935 >    public void testInterruptible_awaitNanos()      { testInterruptible(false, AwaitMethod.awaitNanos); }
936 >    public void testInterruptible_awaitNanos_fair() { testInterruptible(true,  AwaitMethod.awaitNanos); }
937 >    public void testInterruptible_awaitUntil()      { testInterruptible(false, AwaitMethod.awaitUntil); }
938 >    public void testInterruptible_awaitUntil_fair() { testInterruptible(true,  AwaitMethod.awaitUntil); }
939 >    public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
940 >        final PublicReentrantLock lock =
941 >            new PublicReentrantLock(fair);
942          final Condition c = lock.newCondition();
943 <        Thread t = new Thread(new Runnable() {
944 <                public void run() {
945 <                    try {
946 <                        lock.lock();
947 <                        c.await();
948 <                        lock.unlock();
949 <                        threadShouldThrow();
950 <                    }
951 <                    catch(InterruptedException success) {
952 <                    }
953 <                }
954 <            });
955 <
956 <        try {
957 <            t.start();
958 <            Thread.sleep(SHORT_DELAY_MS);
959 <            t.interrupt();
960 <            t.join(SHORT_DELAY_MS);
961 <            assertFalse(t.isAlive());
962 <        }
963 <        catch (Exception ex) {
964 <            unexpectedException();
806 <        }
943 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
944 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
945 >            public void realRun() throws InterruptedException {
946 >                lock.lock();
947 >                assertLockedByMoi(lock);
948 >                assertHasNoWaiters(lock, c);
949 >                pleaseInterrupt.countDown();
950 >                try {
951 >                    await(c, awaitMethod);
952 >                } finally {
953 >                    assertLockedByMoi(lock);
954 >                    assertHasNoWaiters(lock, c);
955 >                    lock.unlock();
956 >                    assertFalse(Thread.interrupted());
957 >                }
958 >            }});
959 >
960 >        await(pleaseInterrupt);
961 >        assertHasWaiters(lock, c, t);
962 >        t.interrupt();
963 >        awaitTermination(t);
964 >        assertNotLocked(lock);
965      }
966  
967      /**
968 <     * awaitNanos is interruptible
968 >     * signalAll wakes up all threads
969       */
970 <    public void testAwaitNanos_Interrupt() {
971 <        final ReentrantLock lock = new ReentrantLock();
970 >    public void testSignalAll_await()           { testSignalAll(false, AwaitMethod.await); }
971 >    public void testSignalAll_await_fair()      { testSignalAll(true,  AwaitMethod.await); }
972 >    public void testSignalAll_awaitTimed()      { testSignalAll(false, AwaitMethod.awaitTimed); }
973 >    public void testSignalAll_awaitTimed_fair() { testSignalAll(true,  AwaitMethod.awaitTimed); }
974 >    public void testSignalAll_awaitNanos()      { testSignalAll(false, AwaitMethod.awaitNanos); }
975 >    public void testSignalAll_awaitNanos_fair() { testSignalAll(true,  AwaitMethod.awaitNanos); }
976 >    public void testSignalAll_awaitUntil()      { testSignalAll(false, AwaitMethod.awaitUntil); }
977 >    public void testSignalAll_awaitUntil_fair() { testSignalAll(true,  AwaitMethod.awaitUntil); }
978 >    public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
979 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
980          final Condition c = lock.newCondition();
981 <        Thread t = new Thread(new Runnable() {
982 <                public void run() {
983 <                    try {
984 <                        lock.lock();
985 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
986 <                        lock.unlock();
987 <                        threadShouldThrow();
988 <                    }
989 <                    catch(InterruptedException success) {
990 <                    }
991 <                }
992 <            });
993 <
994 <        try {
995 <            t.start();
996 <            Thread.sleep(SHORT_DELAY_MS);
997 <            t.interrupt();
998 <            t.join(SHORT_DELAY_MS);
999 <            assertFalse(t.isAlive());
1000 <        }
1001 <        catch (Exception ex) {
836 <            unexpectedException();
837 <        }
981 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
982 >        class Awaiter extends CheckedRunnable {
983 >            public void realRun() throws InterruptedException {
984 >                lock.lock();
985 >                pleaseSignal.countDown();
986 >                await(c, awaitMethod);
987 >                lock.unlock();
988 >            }
989 >        }
990 >
991 >        Thread t1 = newStartedThread(new Awaiter());
992 >        Thread t2 = newStartedThread(new Awaiter());
993 >
994 >        await(pleaseSignal);
995 >        lock.lock();
996 >        assertHasWaiters(lock, c, t1, t2);
997 >        c.signalAll();
998 >        assertHasNoWaiters(lock, c);
999 >        lock.unlock();
1000 >        awaitTermination(t1);
1001 >        awaitTermination(t2);
1002      }
1003  
1004      /**
1005 <     * awaitUntil is interruptible
1006 <     */
1007 <    public void testAwaitUntil_Interrupt() {
1008 <        final ReentrantLock lock = new ReentrantLock();
1005 >     * signal wakes up waiting threads in FIFO order
1006 >     */
1007 >    public void testSignalWakesFifo()      { testSignalWakesFifo(false); }
1008 >    public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1009 >    public void testSignalWakesFifo(boolean fair) {
1010 >        final PublicReentrantLock lock =
1011 >            new PublicReentrantLock(fair);
1012          final Condition c = lock.newCondition();
1013 <        Thread t = new Thread(new Runnable() {
1014 <                public void run() {
1015 <                    try {
1016 <                        lock.lock();
1017 <                        java.util.Date d = new java.util.Date();
1018 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1019 <                        lock.unlock();
1020 <                        threadShouldThrow();
1021 <                    }
1022 <                    catch(InterruptedException success) {
1023 <                    }
1024 <                }
1025 <            });
1026 <
1027 <        try {
1028 <            t.start();
1029 <            Thread.sleep(SHORT_DELAY_MS);
1030 <            t.interrupt();
1031 <            t.join(SHORT_DELAY_MS);
1032 <            assertFalse(t.isAlive());
1033 <        }
1034 <        catch (Exception ex) {
1035 <            unexpectedException();
1036 <        }
1013 >        final CountDownLatch locked1 = new CountDownLatch(1);
1014 >        final CountDownLatch locked2 = new CountDownLatch(1);
1015 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1016 >            public void realRun() throws InterruptedException {
1017 >                lock.lock();
1018 >                locked1.countDown();
1019 >                c.await();
1020 >                lock.unlock();
1021 >            }});
1022 >
1023 >        await(locked1);
1024 >
1025 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1026 >            public void realRun() throws InterruptedException {
1027 >                lock.lock();
1028 >                locked2.countDown();
1029 >                c.await();
1030 >                lock.unlock();
1031 >            }});
1032 >
1033 >        await(locked2);
1034 >
1035 >        lock.lock();
1036 >        assertHasWaiters(lock, c, t1, t2);
1037 >        assertFalse(lock.hasQueuedThreads());
1038 >        c.signal();
1039 >        assertHasWaiters(lock, c, t2);
1040 >        assertTrue(lock.hasQueuedThread(t1));
1041 >        assertFalse(lock.hasQueuedThread(t2));
1042 >        c.signal();
1043 >        assertHasNoWaiters(lock, c);
1044 >        assertTrue(lock.hasQueuedThread(t1));
1045 >        assertTrue(lock.hasQueuedThread(t2));
1046 >        lock.unlock();
1047 >        awaitTermination(t1);
1048 >        awaitTermination(t2);
1049      }
1050  
1051      /**
1052 <     * signalAll wakes up all threads
1053 <     */
1054 <    public void testSignalAll() {
1055 <        final ReentrantLock lock = new ReentrantLock();
1052 >     * await after multiple reentrant locking preserves lock count
1053 >     */
1054 >    public void testAwaitLockCount()      { testAwaitLockCount(false); }
1055 >    public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1056 >    public void testAwaitLockCount(boolean fair) {
1057 >        final PublicReentrantLock lock = new PublicReentrantLock(fair);
1058          final Condition c = lock.newCondition();
1059 <        Thread t1 = new Thread(new Runnable() {
1060 <                public void run() {
1061 <                    try {
1062 <                        lock.lock();
1063 <                        c.await();
1064 <                        lock.unlock();
1065 <                    }
1066 <                    catch(InterruptedException e) {
1067 <                        threadUnexpectedException();
1068 <                    }
1069 <                }
1070 <            });
1071 <
1072 <        Thread t2 = new Thread(new Runnable() {
1073 <                public void run() {
1074 <                    try {
1075 <                        lock.lock();
1076 <                        c.await();
1077 <                        lock.unlock();
1078 <                    }
1079 <                    catch(InterruptedException e) {
1080 <                        threadUnexpectedException();
1081 <                    }
1082 <                }
1083 <            });
1084 <
1085 <        try {
1086 <            t1.start();
1087 <            t2.start();
1088 <            Thread.sleep(SHORT_DELAY_MS);
1089 <            lock.lock();
1090 <            c.signalAll();
1091 <            lock.unlock();
1092 <            t1.join(SHORT_DELAY_MS);
1093 <            t2.join(SHORT_DELAY_MS);
1094 <            assertFalse(t1.isAlive());
914 <            assertFalse(t2.isAlive());
915 <        }
916 <        catch (Exception ex) {
917 <            unexpectedException();
918 <        }
1059 >        final CountDownLatch pleaseSignal = new CountDownLatch(2);
1060 >        Thread t1 = newStartedThread(new CheckedRunnable() {
1061 >            public void realRun() throws InterruptedException {
1062 >                lock.lock();
1063 >                assertLockedByMoi(lock);
1064 >                assertEquals(1, lock.getHoldCount());
1065 >                pleaseSignal.countDown();
1066 >                c.await();
1067 >                assertLockedByMoi(lock);
1068 >                assertEquals(1, lock.getHoldCount());
1069 >                lock.unlock();
1070 >            }});
1071 >
1072 >        Thread t2 = newStartedThread(new CheckedRunnable() {
1073 >            public void realRun() throws InterruptedException {
1074 >                lock.lock();
1075 >                lock.lock();
1076 >                assertLockedByMoi(lock);
1077 >                assertEquals(2, lock.getHoldCount());
1078 >                pleaseSignal.countDown();
1079 >                c.await();
1080 >                assertLockedByMoi(lock);
1081 >                assertEquals(2, lock.getHoldCount());
1082 >                lock.unlock();
1083 >                lock.unlock();
1084 >            }});
1085 >
1086 >        await(pleaseSignal);
1087 >        lock.lock();
1088 >        assertHasWaiters(lock, c, t1, t2);
1089 >        assertEquals(1, lock.getHoldCount());
1090 >        c.signalAll();
1091 >        assertHasNoWaiters(lock, c);
1092 >        lock.unlock();
1093 >        awaitTermination(t1);
1094 >        awaitTermination(t2);
1095      }
1096  
1097      /**
1098       * A serialized lock deserializes as unlocked
1099       */
1100 <    public void testSerialization() {
1101 <        ReentrantLock l = new ReentrantLock();
1102 <        l.lock();
1103 <        l.unlock();
1104 <
1105 <        try {
1106 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1107 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1108 <            out.writeObject(l);
1109 <            out.close();
1110 <
1111 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1112 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1113 <            ReentrantLock r = (ReentrantLock) in.readObject();
1114 <            r.lock();
1115 <            r.unlock();
1116 <        } catch(Exception e){
1117 <            e.printStackTrace();
1118 <            unexpectedException();
1119 <        }
1100 >    public void testSerialization()      { testSerialization(false); }
1101 >    public void testSerialization_fair() { testSerialization(true); }
1102 >    public void testSerialization(boolean fair) {
1103 >        ReentrantLock lock = new ReentrantLock(fair);
1104 >        lock.lock();
1105 >
1106 >        ReentrantLock clone = serialClone(lock);
1107 >        assertEquals(lock.isFair(), clone.isFair());
1108 >        assertTrue(lock.isLocked());
1109 >        assertFalse(clone.isLocked());
1110 >        assertEquals(1, lock.getHoldCount());
1111 >        assertEquals(0, clone.getHoldCount());
1112 >        clone.lock();
1113 >        clone.lock();
1114 >        assertTrue(clone.isLocked());
1115 >        assertEquals(2, clone.getHoldCount());
1116 >        assertEquals(1, lock.getHoldCount());
1117 >        clone.unlock();
1118 >        clone.unlock();
1119 >        assertTrue(lock.isLocked());
1120 >        assertFalse(clone.isLocked());
1121      }
1122  
1123 +    /**
1124 +     * toString indicates current lock state
1125 +     */
1126 +    public void testToString()      { testToString(false); }
1127 +    public void testToString_fair() { testToString(true); }
1128 +    public void testToString(boolean fair) {
1129 +        ReentrantLock lock = new ReentrantLock(fair);
1130 +        assertTrue(lock.toString().contains("Unlocked"));
1131 +        lock.lock();
1132 +        assertTrue(lock.toString().contains("Locked"));
1133 +        lock.unlock();
1134 +        assertTrue(lock.toString().contains("Unlocked"));
1135 +    }
1136   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines