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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines