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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines