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

Comparing jsr166/src/test/tck/SemaphoreTest.java (file contents):
Revision 1.10 by dl, Fri Jan 2 00:38:47 2004 UTC vs.
Revision 1.28 by jsr166, Tue May 24 23:43: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.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class SemaphoreTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(SemaphoreTest.class);
20 >        return new TestSuite(SemaphoreTest.class);
21      }
22  
23      /**
24       * Subclass to expose protected methods
25       */
26      static class PublicSemaphore extends Semaphore {
27 <        PublicSemaphore(int p, boolean f) { super(p, f); }
28 <        public Collection<Thread> getQueuedThreads() {
29 <            return super.getQueuedThreads();
27 >        PublicSemaphore(int permits) { super(permits); }
28 >        PublicSemaphore(int permits, boolean fair) { super(permits, fair); }
29 >        public Collection<Thread> getQueuedThreads() {
30 >            return super.getQueuedThreads();
31          }
32 <        public void reducePermits(int p) {
33 <            super.reducePermits(p);
32 >        public boolean hasQueuedThread(Thread t) {
33 >            return super.getQueuedThreads().contains(t);
34 >        }
35 >        public void reducePermits(int reduction) {
36 >            super.reducePermits(reduction);
37          }
38      }
39  
40      /**
41       * A runnable calling acquire
42       */
43 <    class InterruptibleLockRunnable implements Runnable {
43 >    class InterruptibleLockRunnable extends CheckedRunnable {
44          final Semaphore lock;
45 <        InterruptibleLockRunnable(Semaphore l) { lock = l; }
46 <        public void run() {
45 >        InterruptibleLockRunnable(Semaphore s) { lock = s; }
46 >        public void realRun() {
47              try {
48                  lock.acquire();
49 <            } catch(InterruptedException success){}
49 >            }
50 >            catch (InterruptedException ignored) {}
51          }
52      }
53  
48
54      /**
55 <     * A runnable calling acquire that expects to be
51 <     * interrupted
55 >     * A runnable calling acquire that expects to be interrupted
56       */
57 <    class InterruptedLockRunnable implements Runnable {
57 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
58          final Semaphore lock;
59 <        InterruptedLockRunnable(Semaphore l) { lock = l; }
60 <        public void run() {
61 <            try {
62 <                lock.acquire();
63 <                threadShouldThrow();
64 <            } catch(InterruptedException success){}
59 >        InterruptedLockRunnable(Semaphore s) { lock = s; }
60 >        public void realRun() throws InterruptedException {
61 >            lock.acquire();
62 >        }
63 >    }
64 >
65 >    /**
66 >     * Spin-waits until s.hasQueuedThread(t) becomes true.
67 >     */
68 >    void waitForQueuedThread(PublicSemaphore s, Thread t) {
69 >        long startTime = System.nanoTime();
70 >        while (!s.hasQueuedThread(t)) {
71 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
72 >                throw new AssertionFailedError("timed out");
73 >            Thread.yield();
74 >        }
75 >        assert(s.hasQueuedThreads());
76 >        assertTrue(t.isAlive());
77 >    }
78 >
79 >    /**
80 >     * Spin-waits until s.hasQueuedThreads() becomes true.
81 >     */
82 >    void waitForQueuedThreads(Semaphore s) {
83 >        long startTime = System.nanoTime();
84 >        while (!s.hasQueuedThreads()) {
85 >            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
86 >                throw new AssertionFailedError("timed out");
87 >            Thread.yield();
88 >        }
89 >    }
90 >
91 >    enum AcquireMethod {
92 >        acquire() {
93 >            void acquire(Semaphore s) throws InterruptedException {
94 >                s.acquire();
95 >            }
96 >        },
97 >        acquireN() {
98 >            void acquire(Semaphore s, int permits) throws InterruptedException {
99 >                s.acquire(permits);
100 >            }
101 >        },
102 >        acquireUninterruptibly() {
103 >            void acquire(Semaphore s) {
104 >                s.acquireUninterruptibly();
105 >            }
106 >        },
107 >        acquireUninterruptiblyN() {
108 >            void acquire(Semaphore s, int permits) {
109 >                s.acquireUninterruptibly(permits);
110 >            }
111 >        },
112 >        tryAcquire() {
113 >            void acquire(Semaphore s) {
114 >                assertTrue(s.tryAcquire());
115 >            }
116 >        },
117 >        tryAcquireN() {
118 >            void acquire(Semaphore s, int permits) {
119 >                assertTrue(s.tryAcquire(permits));
120 >            }
121 >        },
122 >        tryAcquireTimed() {
123 >            void acquire(Semaphore s) throws InterruptedException {
124 >                assertTrue(s.tryAcquire(2 * LONG_DELAY_MS, MILLISECONDS));
125 >            }
126 >        },
127 >        tryAcquireTimedN {
128 >            void acquire(Semaphore s, int permits) throws InterruptedException {
129 >                assertTrue(s.tryAcquire(permits, 2 * LONG_DELAY_MS, MILLISECONDS));
130 >            }
131 >        };
132 >
133 >        // Intentionally meta-circular
134 >
135 >        /** Acquires 1 permit. */
136 >        void acquire(Semaphore s) throws InterruptedException {
137 >            acquire(s, 1);
138 >        }
139 >        /** Acquires the given number of permits. */
140 >        void acquire(Semaphore s, int permits) throws InterruptedException {
141 >            for (int i = 0; i < permits; i++)
142 >                acquire(s);
143          }
144      }
145  
146      /**
147       * Zero, negative, and positive initial values are allowed in constructor
148       */
149 <    public void testConstructor() {
150 <        Semaphore s0 = new Semaphore(0, false);
151 <        assertEquals(0, s0.availablePermits());
152 <        assertFalse(s0.isFair());
153 <        Semaphore s1 = new Semaphore(-1, false);
154 <        assertEquals(-1, s1.availablePermits());
155 <        Semaphore s2 = new Semaphore(-1, false);
156 <        assertEquals(-1, s2.availablePermits());
149 >    public void testConstructor()      { testConstructor(false); }
150 >    public void testConstructor_fair() { testConstructor(true); }
151 >    public void testConstructor(boolean fair) {
152 >        for (int permits : new int[] { -42, -1, 0, 1, 42 }) {
153 >            Semaphore s = new Semaphore(permits, fair);
154 >            assertEquals(permits, s.availablePermits());
155 >            assertEquals(fair, s.isFair());
156 >        }
157 >    }
158 >
159 >    /**
160 >     * Constructor without fairness argument behaves as nonfair
161 >     */
162 >    public void testConstructorDefaultsToNonFair() {
163 >        for (int permits : new int[] { -42, -1, 0, 1, 42 }) {
164 >            Semaphore s = new Semaphore(permits);
165 >            assertEquals(permits, s.availablePermits());
166 >            assertFalse(s.isFair());
167 >        }
168      }
169  
170      /**
171       * tryAcquire succeeds when sufficient permits, else fails
172       */
173 <    public void testTryAcquireInSameThread() {
174 <        Semaphore s = new Semaphore(2, false);
173 >    public void testTryAcquireInSameThread()      { testTryAcquireInSameThread(false); }
174 >    public void testTryAcquireInSameThread_fair() { testTryAcquireInSameThread(true); }
175 >    public void testTryAcquireInSameThread(boolean fair) {
176 >        Semaphore s = new Semaphore(2, fair);
177          assertEquals(2, s.availablePermits());
178          assertTrue(s.tryAcquire());
179          assertTrue(s.tryAcquire());
180          assertEquals(0, s.availablePermits());
181          assertFalse(s.tryAcquire());
182 +        assertFalse(s.tryAcquire());
183 +        assertEquals(0, s.availablePermits());
184      }
185  
186      /**
187 <     * Acquire and release of semaphore succeed if initially available
91 <     */
92 <    public void testAcquireReleaseInSameThread() {
93 <        Semaphore s = new Semaphore(1, false);
94 <        try {
95 <            s.acquire();
96 <            s.release();
97 <            s.acquire();
98 <            s.release();
99 <            s.acquire();
100 <            s.release();
101 <            s.acquire();
102 <            s.release();
103 <            s.acquire();
104 <            s.release();
105 <            assertEquals(1, s.availablePermits());
106 <        } catch( InterruptedException e){
107 <            unexpectedException();
108 <        }
109 <    }
110 <
111 <    /**
112 <     * Uninterruptible acquire and release of semaphore succeed if
113 <     * initially available
187 >     * timed tryAcquire times out
188       */
189 <    public void testAcquireUninterruptiblyReleaseInSameThread() {
190 <        Semaphore s = new Semaphore(1, false);
191 <        try {
192 <            s.acquireUninterruptibly();
193 <            s.release();
194 <            s.acquireUninterruptibly();
195 <            s.release();
196 <            s.acquireUninterruptibly();
197 <            s.release();
198 <            s.acquireUninterruptibly();
199 <            s.release();
200 <            s.acquireUninterruptibly();
201 <            s.release();
202 <            assertEquals(1, s.availablePermits());
203 <        } finally {
204 <        }
205 <    }
189 >    public void testTryAcquire_timeout()      { testTryAcquire_timeout(false); }
190 >    public void testTryAcquire_timeout_fair() { testTryAcquire_timeout(true); }
191 >    public void testTryAcquire_timeout(boolean fair) {
192 >        Semaphore s = new Semaphore(0, fair);
193 >        long startTime = System.nanoTime();
194 >        try { assertFalse(s.tryAcquire(timeoutMillis(), MILLISECONDS)); }
195 >        catch (InterruptedException e) { threadUnexpectedException(e); }
196 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
197 >    }
198 >
199 >    /**
200 >     * timed tryAcquire(N) times out
201 >     */
202 >    public void testTryAcquireN_timeout()      { testTryAcquireN_timeout(false); }
203 >    public void testTryAcquireN_timeout_fair() { testTryAcquireN_timeout(true); }
204 >    public void testTryAcquireN_timeout(boolean fair) {
205 >        Semaphore s = new Semaphore(2, fair);
206 >        long startTime = System.nanoTime();
207 >        try { assertFalse(s.tryAcquire(3, timeoutMillis(), MILLISECONDS)); }
208 >        catch (InterruptedException e) { threadUnexpectedException(e); }
209 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
210 >    }
211 >
212 >    /**
213 >     * acquire(), acquire(N), timed tryAcquired, timed tryAcquire(N)
214 >     * are interruptible
215 >     */
216 >    public void testInterruptible_acquire()               { testInterruptible(false, AcquireMethod.acquire); }
217 >    public void testInterruptible_acquire_fair()          { testInterruptible(true,  AcquireMethod.acquire); }
218 >    public void testInterruptible_acquireN()              { testInterruptible(false, AcquireMethod.acquireN); }
219 >    public void testInterruptible_acquireN_fair()         { testInterruptible(true,  AcquireMethod.acquireN); }
220 >    public void testInterruptible_tryAcquireTimed()       { testInterruptible(false, AcquireMethod.tryAcquireTimed); }
221 >    public void testInterruptible_tryAcquireTimed_fair()  { testInterruptible(true,  AcquireMethod.tryAcquireTimed); }
222 >    public void testInterruptible_tryAcquireTimedN()      { testInterruptible(false, AcquireMethod.tryAcquireTimedN); }
223 >    public void testInterruptible_tryAcquireTimedN_fair() { testInterruptible(true,  AcquireMethod.tryAcquireTimedN); }
224 >    public void testInterruptible(boolean fair, final AcquireMethod acquirer) {
225 >        final PublicSemaphore s = new PublicSemaphore(0, fair);
226 >        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
227 >        Thread t = newStartedThread(new CheckedRunnable() {
228 >            public void realRun() {
229 >                // Interrupt before acquire
230 >                Thread.currentThread().interrupt();
231 >                try {
232 >                    acquirer.acquire(s);
233 >                    shouldThrow();
234 >                } catch (InterruptedException success) {}
235 >
236 >                // Interrupt during acquire
237 >                try {
238 >                    acquirer.acquire(s);
239 >                    shouldThrow();
240 >                } catch (InterruptedException success) {}
241 >
242 >                // Interrupt before acquire(N)
243 >                Thread.currentThread().interrupt();
244 >                try {
245 >                    acquirer.acquire(s, 3);
246 >                    shouldThrow();
247 >                } catch (InterruptedException success) {}
248 >
249 >                anotherInterruptPlease.release();
250 >
251 >                // Interrupt during acquire(N)
252 >                try {
253 >                    acquirer.acquire(s, 3);
254 >                    shouldThrow();
255 >                } catch (InterruptedException success) {}
256 >            }});
257 >
258 >        waitForQueuedThread(s, t);
259 >        t.interrupt();
260 >        try {
261 >            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
262 >        } catch (InterruptedException e) { threadUnexpectedException(e); }
263 >        waitForQueuedThread(s, t);
264 >        t.interrupt();
265 >        awaitTermination(t);
266 >    }
267 >
268 >    /**
269 >     * acquireUninterruptibly(), acquireUninterruptibly(N) are
270 >     * uninterruptible
271 >     */
272 >    public void testUninterruptible_acquireUninterruptibly()       { testUninterruptible(false, AcquireMethod.acquireUninterruptibly); }
273 >    public void testUninterruptible_acquireUninterruptibly_fair()  { testUninterruptible(true,  AcquireMethod.acquireUninterruptibly); }
274 >    public void testUninterruptible_acquireUninterruptiblyN()      { testUninterruptible(false, AcquireMethod.acquireUninterruptiblyN); }
275 >    public void testUninterruptible_acquireUninterruptiblyN_fair() { testUninterruptible(true,  AcquireMethod.acquireUninterruptiblyN); }
276 >    public void testUninterruptible(boolean fair, final AcquireMethod acquirer) {
277 >        final PublicSemaphore s = new PublicSemaphore(0, fair);
278 >        final Semaphore anotherInterruptPlease = new Semaphore(0, fair);
279 >        Thread t = newStartedThread(new CheckedRunnable() {
280 >            public void realRun() throws InterruptedException {
281 >                // Interrupt before acquire
282 >                Thread.currentThread().interrupt();
283 >                acquirer.acquire(s);
284 >                assertTrue(Thread.interrupted());
285 >
286 >                anotherInterruptPlease.release();
287 >
288 >                // Interrupt during acquire
289 >                acquirer.acquire(s);
290 >                assertTrue(Thread.interrupted());
291 >            }});
292 >
293 >        waitForQueuedThread(s, t);
294 >        assertThreadStaysAlive(t);
295 >        s.release();
296 >
297 >        try {
298 >            assertTrue(anotherInterruptPlease.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
299 >        } catch (InterruptedException e) { threadUnexpectedException(e); }
300 >        waitForQueuedThread(s, t);
301 >        t.interrupt();
302 >        assertThreadStaysAlive(t);
303 >        s.release();
304  
305 <    /**
134 <     * Timed Acquire and release of semaphore succeed if
135 <     * initially available
136 <     */
137 <    public void testTimedAcquireReleaseInSameThread() {
138 <        Semaphore s = new Semaphore(1, false);
139 <        try {
140 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
141 <            s.release();
142 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
143 <            s.release();
144 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
145 <            s.release();
146 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
147 <            s.release();
148 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
149 <            s.release();
150 <            assertEquals(1, s.availablePermits());
151 <        } catch( InterruptedException e){
152 <            unexpectedException();
153 <        }
305 >        awaitTermination(t);
306      }
307  
308      /**
309 <     * A release in one thread enables an acquire in another thread
309 >     * hasQueuedThreads reports whether there are waiting threads
310       */
311 <    public void testAcquireReleaseInDifferentThreads() {
312 <        final Semaphore s = new Semaphore(0, false);
313 <        Thread t = new Thread(new Runnable() {
314 <                public void run() {
315 <                    try {
316 <                        s.acquire();
317 <                        s.release();
318 <                        s.release();
319 <                        s.acquire();
320 <                    } catch(InterruptedException ie){
321 <                        threadUnexpectedException();
322 <                    }
323 <                }
324 <            });
325 <        try {
326 <            t.start();
327 <            Thread.sleep(SHORT_DELAY_MS);
328 <            s.release();
177 <            s.release();
178 <            s.acquire();
179 <            s.acquire();
180 <            s.release();
181 <            t.join();
182 <        } catch( InterruptedException e){
183 <            unexpectedException();
184 <        }
311 >    public void testHasQueuedThreads()      { testHasQueuedThreads(false); }
312 >    public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
313 >    public void testHasQueuedThreads(boolean fair) {
314 >        final PublicSemaphore lock = new PublicSemaphore(1, fair);
315 >        assertFalse(lock.hasQueuedThreads());
316 >        lock.acquireUninterruptibly();
317 >        Thread t1 = newStartedThread(new InterruptedLockRunnable(lock));
318 >        waitForQueuedThread(lock, t1);
319 >        assertTrue(lock.hasQueuedThreads());
320 >        Thread t2 = newStartedThread(new InterruptibleLockRunnable(lock));
321 >        waitForQueuedThread(lock, t2);
322 >        assertTrue(lock.hasQueuedThreads());
323 >        t1.interrupt();
324 >        awaitTermination(t1);
325 >        assertTrue(lock.hasQueuedThreads());
326 >        lock.release();
327 >        awaitTermination(t2);
328 >        assertFalse(lock.hasQueuedThreads());
329      }
330  
331      /**
332 <     * A release in one thread enables an uninterruptible acquire in another thread
332 >     * getQueueLength reports number of waiting threads
333       */
334 <    public void testUninterruptibleAcquireReleaseInDifferentThreads() {
335 <        final Semaphore s = new Semaphore(0, false);
336 <        Thread t = new Thread(new Runnable() {
337 <                public void run() {
338 <                    s.acquireUninterruptibly();
339 <                    s.release();
340 <                    s.release();
341 <                    s.acquireUninterruptibly();
342 <                }
343 <            });
344 <        try {
345 <            t.start();
346 <            Thread.sleep(SHORT_DELAY_MS);
347 <            s.release();
348 <            s.release();
349 <            s.acquireUninterruptibly();
350 <            s.acquireUninterruptibly();
351 <            s.release();
208 <            t.join();
209 <        } catch( InterruptedException e){
210 <            unexpectedException();
211 <        }
334 >    public void testGetQueueLength()      { testGetQueueLength(false); }
335 >    public void testGetQueueLength_fair() { testGetQueueLength(true); }
336 >    public void testGetQueueLength(boolean fair) {
337 >        final PublicSemaphore lock = new PublicSemaphore(1, fair);
338 >        assertEquals(0, lock.getQueueLength());
339 >        lock.acquireUninterruptibly();
340 >        Thread t1 = newStartedThread(new InterruptedLockRunnable(lock));
341 >        waitForQueuedThread(lock, t1);
342 >        assertEquals(1, lock.getQueueLength());
343 >        Thread t2 = newStartedThread(new InterruptibleLockRunnable(lock));
344 >        waitForQueuedThread(lock, t2);
345 >        assertEquals(2, lock.getQueueLength());
346 >        t1.interrupt();
347 >        awaitTermination(t1);
348 >        assertEquals(1, lock.getQueueLength());
349 >        lock.release();
350 >        awaitTermination(t2);
351 >        assertEquals(0, lock.getQueueLength());
352      }
353  
214
354      /**
355 <     *  A release in one thread enables a timed acquire in another thread
355 >     * getQueuedThreads includes waiting threads
356       */
357 <    public void testTimedAcquireReleaseInDifferentThreads() {
358 <        final Semaphore s = new Semaphore(1, false);
359 <        Thread t = new Thread(new Runnable() {
360 <                public void run() {
361 <                    try {
362 <                        s.release();
363 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
364 <                        s.release();
365 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
366 <
367 <                    } catch(InterruptedException ie){
368 <                        threadUnexpectedException();
369 <                    }
370 <                }
371 <            });
372 <        try {
373 <            t.start();
374 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
375 <            s.release();
376 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
377 <            s.release();
239 <            s.release();
240 <            t.join();
241 <        } catch( InterruptedException e){
242 <            unexpectedException();
243 <        }
357 >    public void testGetQueuedThreads()      { testGetQueuedThreads(false); }
358 >    public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
359 >    public void testGetQueuedThreads(boolean fair) {
360 >        final PublicSemaphore lock = new PublicSemaphore(1, fair);
361 >        assertTrue(lock.getQueuedThreads().isEmpty());
362 >        lock.acquireUninterruptibly();
363 >        assertTrue(lock.getQueuedThreads().isEmpty());
364 >        Thread t1 = newStartedThread(new InterruptedLockRunnable(lock));
365 >        waitForQueuedThread(lock, t1);
366 >        assertTrue(lock.getQueuedThreads().contains(t1));
367 >        Thread t2 = newStartedThread(new InterruptibleLockRunnable(lock));
368 >        waitForQueuedThread(lock, t2);
369 >        assertTrue(lock.getQueuedThreads().contains(t1));
370 >        assertTrue(lock.getQueuedThreads().contains(t2));
371 >        t1.interrupt();
372 >        awaitTermination(t1);
373 >        assertFalse(lock.getQueuedThreads().contains(t1));
374 >        assertTrue(lock.getQueuedThreads().contains(t2));
375 >        lock.release();
376 >        awaitTermination(t2);
377 >        assertTrue(lock.getQueuedThreads().isEmpty());
378      }
379  
380      /**
381 <     * A waiting acquire blocks interruptibly
382 <     */
383 <    public void testAcquire_InterruptedException() {
384 <        final Semaphore s = new Semaphore(0, false);
385 <        Thread t = new Thread(new Runnable() {
386 <                public void run() {
387 <                    try {
388 <                        s.acquire();
389 <                        threadShouldThrow();
390 <                    } catch(InterruptedException success){}
391 <                }
392 <            });
393 <        t.start();
260 <        try {
261 <            Thread.sleep(SHORT_DELAY_MS);
262 <            t.interrupt();
263 <            t.join();
264 <        } catch(InterruptedException e){
265 <            unexpectedException();
266 <        }
267 <    }
268 <    
269 <    /**
270 <     *  A waiting timed acquire blocks interruptibly
271 <     */
272 <    public void testTryAcquire_InterruptedException() {
273 <        final Semaphore s = new Semaphore(0, false);
274 <        Thread t = new Thread(new Runnable() {
275 <                public void run() {
276 <                    try {
277 <                        s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
278 <                        threadShouldThrow();
279 <                    } catch(InterruptedException success){
280 <                    }
281 <                }
282 <            });
283 <        t.start();
284 <        try {
285 <            Thread.sleep(SHORT_DELAY_MS);
286 <            t.interrupt();
287 <            t.join();
288 <        } catch(InterruptedException e){
289 <            unexpectedException();
290 <        }
381 >     * drainPermits reports and removes given number of permits
382 >     */
383 >    public void testDrainPermits()      { testDrainPermits(false); }
384 >    public void testDrainPermits_fair() { testDrainPermits(true); }
385 >    public void testDrainPermits(boolean fair) {
386 >        Semaphore s = new Semaphore(0, fair);
387 >        assertEquals(0, s.availablePermits());
388 >        assertEquals(0, s.drainPermits());
389 >        s.release(10);
390 >        assertEquals(10, s.availablePermits());
391 >        assertEquals(10, s.drainPermits());
392 >        assertEquals(0, s.availablePermits());
393 >        assertEquals(0, s.drainPermits());
394      }
395  
396      /**
397 <     * hasQueuedThreads reports whether there are waiting threads
295 <     */
296 <    public void testHasQueuedThreads() {
297 <        final Semaphore lock = new Semaphore(1, false);
298 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
299 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
300 <        try {
301 <            assertFalse(lock.hasQueuedThreads());
302 <            lock.acquireUninterruptibly();
303 <            t1.start();
304 <            Thread.sleep(SHORT_DELAY_MS);
305 <            assertTrue(lock.hasQueuedThreads());
306 <            t2.start();
307 <            Thread.sleep(SHORT_DELAY_MS);
308 <            assertTrue(lock.hasQueuedThreads());
309 <            t1.interrupt();
310 <            Thread.sleep(SHORT_DELAY_MS);
311 <            assertTrue(lock.hasQueuedThreads());
312 <            lock.release();
313 <            Thread.sleep(SHORT_DELAY_MS);
314 <            assertFalse(lock.hasQueuedThreads());
315 <            t1.join();
316 <            t2.join();
317 <        } catch(Exception e){
318 <            unexpectedException();
319 <        }
320 <    }
321 <
322 <    /**
323 <     * getQueueLength reports number of waiting threads
397 >     * release(-N) throws IllegalArgumentException
398       */
399 <    public void testGetQueueLength() {
400 <        final Semaphore lock = new Semaphore(1, false);
401 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
402 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
399 >    public void testReleaseIAE()      { testReleaseIAE(false); }
400 >    public void testReleaseIAE_fair() { testReleaseIAE(true); }
401 >    public void testReleaseIAE(boolean fair) {
402 >        Semaphore s = new Semaphore(10, fair);
403          try {
404 <            assertEquals(0, lock.getQueueLength());
405 <            lock.acquireUninterruptibly();
406 <            t1.start();
407 <            Thread.sleep(SHORT_DELAY_MS);
334 <            assertEquals(1, lock.getQueueLength());
335 <            t2.start();
336 <            Thread.sleep(SHORT_DELAY_MS);
337 <            assertEquals(2, lock.getQueueLength());
338 <            t1.interrupt();
339 <            Thread.sleep(SHORT_DELAY_MS);
340 <            assertEquals(1, lock.getQueueLength());
341 <            lock.release();
342 <            Thread.sleep(SHORT_DELAY_MS);
343 <            assertEquals(0, lock.getQueueLength());
344 <            t1.join();
345 <            t2.join();
346 <        } catch(Exception e){
347 <            unexpectedException();
348 <        }
349 <    }
404 >            s.release(-1);
405 >            shouldThrow();
406 >        } catch (IllegalArgumentException success) {}
407 >    }
408  
409      /**
410 <     * getQueuedThreads includes waiting threads
410 >     * reducePermits(-N) throws IllegalArgumentException
411       */
412 <    public void testGetQueuedThreads() {
413 <        final PublicSemaphore lock = new PublicSemaphore(1, false);
414 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
415 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
412 >    public void testReducePermitsIAE()      { testReducePermitsIAE(false); }
413 >    public void testReducePermitsIAE_fair() { testReducePermitsIAE(true); }
414 >    public void testReducePermitsIAE(boolean fair) {
415 >        PublicSemaphore s = new PublicSemaphore(10, fair);
416          try {
417 <            assertTrue(lock.getQueuedThreads().isEmpty());
418 <            lock.acquireUninterruptibly();
419 <            assertTrue(lock.getQueuedThreads().isEmpty());
420 <            t1.start();
363 <            Thread.sleep(SHORT_DELAY_MS);
364 <            assertTrue(lock.getQueuedThreads().contains(t1));
365 <            t2.start();
366 <            Thread.sleep(SHORT_DELAY_MS);
367 <            assertTrue(lock.getQueuedThreads().contains(t1));
368 <            assertTrue(lock.getQueuedThreads().contains(t2));
369 <            t1.interrupt();
370 <            Thread.sleep(SHORT_DELAY_MS);
371 <            assertFalse(lock.getQueuedThreads().contains(t1));
372 <            assertTrue(lock.getQueuedThreads().contains(t2));
373 <            lock.release();
374 <            Thread.sleep(SHORT_DELAY_MS);
375 <            assertTrue(lock.getQueuedThreads().isEmpty());
376 <            t1.join();
377 <            t2.join();
378 <        } catch(Exception e){
379 <            unexpectedException();
380 <        }
381 <    }
382 <
417 >            s.reducePermits(-1);
418 >            shouldThrow();
419 >        } catch (IllegalArgumentException success) {}
420 >    }
421  
422      /**
423       * reducePermits reduces number of permits
424       */
425 <    public void testReducePermits() {
426 <        PublicSemaphore s = new PublicSemaphore(10, false);
425 >    public void testReducePermits()      { testReducePermits(false); }
426 >    public void testReducePermits_fair() { testReducePermits(true); }
427 >    public void testReducePermits(boolean fair) {
428 >        PublicSemaphore s = new PublicSemaphore(10, fair);
429 >        assertEquals(10, s.availablePermits());
430 >        s.reducePermits(0);
431          assertEquals(10, s.availablePermits());
432          s.reducePermits(1);
433          assertEquals(9, s.availablePermits());
434          s.reducePermits(10);
435          assertEquals(-1, s.availablePermits());
436 +        s.reducePermits(10);
437 +        assertEquals(-11, s.availablePermits());
438 +        s.reducePermits(0);
439 +        assertEquals(-11, s.availablePermits());
440      }
441  
442      /**
443 <     * a deserialized serialized semaphore has same number of permits
443 >     * a reserialized semaphore has same number of permits and
444 >     * fairness, but no queued threads
445       */
446 <    public void testSerialization() {
447 <        Semaphore l = new Semaphore(3, false);
446 >    public void testSerialization()      { testSerialization(false); }
447 >    public void testSerialization_fair() { testSerialization(true); }
448 >    public void testSerialization(boolean fair) {
449          try {
450 <            l.acquire();
451 <            l.release();
452 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
453 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
406 <            out.writeObject(l);
407 <            out.close();
408 <
409 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
410 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
411 <            Semaphore r = (Semaphore) in.readObject();
412 <            assertEquals(3, r.availablePermits());
413 <            assertFalse(r.isFair());
414 <            r.acquire();
415 <            r.release();
416 <        } catch(Exception e){
417 <            unexpectedException();
418 <        }
419 <    }
450 >            Semaphore s = new Semaphore(3, fair);
451 >            s.acquire();
452 >            s.acquire();
453 >            s.release();
454  
455 +            Semaphore clone = serialClone(s);
456 +            assertEquals(fair, s.isFair());
457 +            assertEquals(fair, clone.isFair());
458 +            assertEquals(2, s.availablePermits());
459 +            assertEquals(2, clone.availablePermits());
460 +            clone.acquire();
461 +            clone.acquire();
462 +            clone.release();
463 +            assertEquals(2, s.availablePermits());
464 +            assertEquals(1, clone.availablePermits());
465  
466 <    /**
467 <     * Zero, negative, and positive initial values are allowed in constructor
468 <     */
469 <    public void testConstructor_fair() {
470 <        Semaphore s0 = new Semaphore(0, true);
471 <        assertEquals(0, s0.availablePermits());
472 <        assertTrue(s0.isFair());
473 <        Semaphore s1 = new Semaphore(-1, true);
474 <        assertEquals(-1, s1.availablePermits());
475 <        Semaphore s2 = new Semaphore(-1, true);
476 <        assertEquals(-1, s2.availablePermits());
477 <    }
478 <
479 <    /**
480 <     * tryAcquire succeeds when sufficient permits, else fails
437 <     */
438 <    public void testTryAcquireInSameThread_fair() {
439 <        Semaphore s = new Semaphore(2, true);
440 <        assertEquals(2, s.availablePermits());
441 <        assertTrue(s.tryAcquire());
442 <        assertTrue(s.tryAcquire());
443 <        assertEquals(0, s.availablePermits());
444 <        assertFalse(s.tryAcquire());
466 >            s = new Semaphore(0, fair);
467 >            Thread t = newStartedThread(new InterruptibleLockRunnable(s));
468 >            waitForQueuedThreads(s);
469 >            clone = serialClone(s);
470 >            assertEquals(fair, s.isFair());
471 >            assertEquals(fair, clone.isFair());
472 >            assertEquals(0, s.availablePermits());
473 >            assertEquals(0, clone.availablePermits());
474 >            assertTrue(s.hasQueuedThreads());
475 >            assertFalse(clone.hasQueuedThreads());
476 >            s.release();
477 >            awaitTermination(t);
478 >            assertFalse(s.hasQueuedThreads());
479 >            assertFalse(clone.hasQueuedThreads());
480 >        } catch (InterruptedException e) { threadUnexpectedException(e); }
481      }
482  
483      /**
484       * tryAcquire(n) succeeds when sufficient permits, else fails
485       */
486 <    public void testTryAcquireNInSameThread_fair() {
487 <        Semaphore s = new Semaphore(2, true);
486 >    public void testTryAcquireNInSameThread()      { testTryAcquireNInSameThread(false); }
487 >    public void testTryAcquireNInSameThread_fair() { testTryAcquireNInSameThread(true); }
488 >    public void testTryAcquireNInSameThread(boolean fair) {
489 >        Semaphore s = new Semaphore(2, fair);
490 >        assertEquals(2, s.availablePermits());
491 >        assertFalse(s.tryAcquire(3));
492          assertEquals(2, s.availablePermits());
493          assertTrue(s.tryAcquire(2));
494          assertEquals(0, s.availablePermits());
495 <        assertFalse(s.tryAcquire());
496 <    }
497 <
458 <    /**
459 <     * Acquire and release of semaphore succeed if initially available
460 <     */
461 <    public void testAcquireReleaseInSameThread_fair() {
462 <        Semaphore s = new Semaphore(1, true);
463 <        try {
464 <            s.acquire();
465 <            s.release();
466 <            s.acquire();
467 <            s.release();
468 <            s.acquire();
469 <            s.release();
470 <            s.acquire();
471 <            s.release();
472 <            s.acquire();
473 <            s.release();
474 <            assertEquals(1, s.availablePermits());
475 <        } catch( InterruptedException e){
476 <            unexpectedException();
477 <        }
478 <    }
479 <
480 <    /**
481 <     * Acquire(n) and release(n) of semaphore succeed if initially available
482 <     */
483 <    public void testAcquireReleaseNInSameThread_fair() {
484 <        Semaphore s = new Semaphore(1, true);
485 <        try {
486 <            s.release(1);
487 <            s.acquire(1);
488 <            s.release(2);
489 <            s.acquire(2);
490 <            s.release(3);
491 <            s.acquire(3);
492 <            s.release(4);
493 <            s.acquire(4);
494 <            s.release(5);
495 <            s.acquire(5);
496 <            assertEquals(1, s.availablePermits());
497 <        } catch( InterruptedException e){
498 <            unexpectedException();
499 <        }
500 <    }
501 <
502 <    /**
503 <     * Acquire(n) and release(n) of semaphore succeed if initially available
504 <     */
505 <    public void testAcquireUninterruptiblyReleaseNInSameThread_fair() {
506 <        Semaphore s = new Semaphore(1, true);
507 <        try {
508 <            s.release(1);
509 <            s.acquireUninterruptibly(1);
510 <            s.release(2);
511 <            s.acquireUninterruptibly(2);
512 <            s.release(3);
513 <            s.acquireUninterruptibly(3);
514 <            s.release(4);
515 <            s.acquireUninterruptibly(4);
516 <            s.release(5);
517 <            s.acquireUninterruptibly(5);
518 <            assertEquals(1, s.availablePermits());
519 <        } finally {
520 <        }
521 <    }
522 <
523 <    /**
524 <     * release(n) in one thread enables timed acquire(n) in another thread
525 <     */
526 <    public void testTimedAcquireReleaseNInSameThread_fair() {
527 <        Semaphore s = new Semaphore(1, true);
528 <        try {
529 <            s.release(1);
530 <            assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
531 <            s.release(2);
532 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
533 <            s.release(3);
534 <            assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
535 <            s.release(4);
536 <            assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
537 <            s.release(5);
538 <            assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
539 <            assertEquals(1, s.availablePermits());
540 <        } catch( InterruptedException e){
541 <            unexpectedException();
542 <        }
495 >        assertFalse(s.tryAcquire(1));
496 >        assertFalse(s.tryAcquire(2));
497 >        assertEquals(0, s.availablePermits());
498      }
499  
500      /**
501 <     * release in one thread enables timed acquire in another thread
501 >     * acquire succeeds if permits available
502       */
503 <    public void testTimedAcquireReleaseInSameThread_fair() {
504 <        Semaphore s = new Semaphore(1, true);
505 <        try {
506 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
507 <            s.release();
508 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
509 <            s.release();
510 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
511 <            s.release();
512 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
513 <            s.release();
514 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
515 <            s.release();
503 >    public void testReleaseAcquireSameThread_acquire()       { testReleaseAcquireSameThread(false, AcquireMethod.acquire); }
504 >    public void testReleaseAcquireSameThread_acquire_fair()  { testReleaseAcquireSameThread(true, AcquireMethod.acquire); }
505 >    public void testReleaseAcquireSameThread_acquireN()      { testReleaseAcquireSameThread(false, AcquireMethod.acquireN); }
506 >    public void testReleaseAcquireSameThread_acquireN_fair() { testReleaseAcquireSameThread(true, AcquireMethod.acquireN); }
507 >    public void testReleaseAcquireSameThread_acquireUninterruptibly()       { testReleaseAcquireSameThread(false, AcquireMethod.acquireUninterruptibly); }
508 >    public void testReleaseAcquireSameThread_acquireUninterruptibly_fair()  { testReleaseAcquireSameThread(true, AcquireMethod.acquireUninterruptibly); }
509 >    public void testReleaseAcquireSameThread_acquireUninterruptiblyN()      { testReleaseAcquireSameThread(false, AcquireMethod.acquireUninterruptibly); }
510 >    public void testReleaseAcquireSameThread_acquireUninterruptiblyN_fair() { testReleaseAcquireSameThread(true, AcquireMethod.acquireUninterruptibly); }
511 >    public void testReleaseAcquireSameThread_tryAcquire()       { testReleaseAcquireSameThread(false, AcquireMethod.tryAcquire); }
512 >    public void testReleaseAcquireSameThread_tryAcquire_fair()  { testReleaseAcquireSameThread(true, AcquireMethod.tryAcquire); }
513 >    public void testReleaseAcquireSameThread_tryAcquireN()      { testReleaseAcquireSameThread(false, AcquireMethod.tryAcquireN); }
514 >    public void testReleaseAcquireSameThread_tryAcquireN_fair() { testReleaseAcquireSameThread(true, AcquireMethod.tryAcquireN); }
515 >    public void testReleaseAcquireSameThread_tryAcquireTimed()       { testReleaseAcquireSameThread(false, AcquireMethod.tryAcquireTimed); }
516 >    public void testReleaseAcquireSameThread_tryAcquireTimed_fair()  { testReleaseAcquireSameThread(true, AcquireMethod.tryAcquireTimed); }
517 >    public void testReleaseAcquireSameThread_tryAcquireTimedN()      { testReleaseAcquireSameThread(false, AcquireMethod.tryAcquireTimedN); }
518 >    public void testReleaseAcquireSameThread_tryAcquireTimedN_fair() { testReleaseAcquireSameThread(true, AcquireMethod.tryAcquireTimedN); }
519 >    public void testReleaseAcquireSameThread(boolean fair,
520 >                                             final AcquireMethod acquirer) {
521 >        Semaphore s = new Semaphore(1, fair);
522 >        for (int i = 1; i < 6; i++) {
523 >            s.release(i);
524 >            assertEquals(1 + i, s.availablePermits());
525 >            try {
526 >                acquirer.acquire(s, i);
527 >            } catch (InterruptedException e) { threadUnexpectedException(e); }
528              assertEquals(1, s.availablePermits());
562        } catch( InterruptedException e){
563            unexpectedException();
564        }
565    }
566
567    /**
568     * A release in one thread enables an acquire in another thread
569     */
570    public void testAcquireReleaseInDifferentThreads_fair() {
571        final Semaphore s = new Semaphore(0, true);
572        Thread t = new Thread(new Runnable() {
573                public void run() {
574                    try {
575                        s.acquire();
576                        s.acquire();
577                        s.acquire();
578                        s.acquire();
579                    } catch(InterruptedException ie){
580                        threadUnexpectedException();
581                    }
582                }
583            });
584        try {
585            t.start();
586            Thread.sleep(SHORT_DELAY_MS);
587            s.release();
588            s.release();
589            s.release();
590            s.release();
591            s.release();
592            s.release();
593            t.join();
594            assertEquals(2, s.availablePermits());
595        } catch( InterruptedException e){
596            unexpectedException();
529          }
530      }
531  
532      /**
533 <     * release(n) in one thread enables acquire(n) in another thread
533 >     * release in one thread enables acquire in another thread
534       */
535 <    public void testAcquireReleaseNInDifferentThreads_fair() {
536 <        final Semaphore s = new Semaphore(0, true);
537 <        Thread t = new Thread(new Runnable() {
538 <                public void run() {
539 <                    try {
540 <                        s.acquire();
541 <                        s.release();
542 <                        s.release();
543 <                        s.acquire();
544 <                    } catch(InterruptedException ie){
545 <                        threadUnexpectedException();
546 <                    }
547 <                }
548 <            });
549 <        try {
550 <            t.start();
551 <            Thread.sleep(SHORT_DELAY_MS);
552 <            s.release();
553 <            s.release();
554 <            s.acquire();
555 <            s.acquire();
556 <            s.release();
557 <            t.join();
558 <        } catch( InterruptedException e){
559 <            unexpectedException();
560 <        }
561 <    }
562 <
563 <
564 <
565 <    /**
566 <     * release in one thread enables timed acquire in another thread
567 <     */
568 <    public void testTimedAcquireReleaseInDifferentThreads_fair() {
569 <        final Semaphore s = new Semaphore(1, true);
638 <        Thread t = new Thread(new Runnable() {
639 <                public void run() {
640 <                    try {
641 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
642 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
643 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
644 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
645 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
646 <
647 <                    } catch(InterruptedException ie){
648 <                        threadUnexpectedException();
649 <                    }
650 <                }
651 <            });
652 <        t.start();
653 <        try {
654 <            s.release();
655 <            s.release();
656 <            s.release();
657 <            s.release();
658 <            s.release();
659 <            t.join();
660 <        } catch( InterruptedException e){
661 <            unexpectedException();
662 <        }
663 <    }
664 <
665 <    /**
666 <     * release(n) in one thread enables timed acquire(n) in another thread
667 <     */
668 <    public void testTimedAcquireReleaseNInDifferentThreads_fair() {
669 <        final Semaphore s = new Semaphore(2, true);
670 <        Thread t = new Thread(new Runnable() {
671 <                public void run() {
672 <                    try {
673 <                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
674 <                        s.release(2);
675 <                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
676 <                        s.release(2);
677 <                    } catch(InterruptedException ie){
678 <                        threadUnexpectedException();
679 <                    }
680 <                }
681 <            });
682 <        t.start();
683 <        try {
684 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
685 <            s.release(2);
686 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
687 <            s.release(2);
688 <            t.join();
689 <        } catch( InterruptedException e){
690 <            unexpectedException();
691 <        }
692 <    }
693 <
694 <    /**
695 <     * A waiting acquire blocks interruptibly
696 <     */
697 <    public void testAcquire_InterruptedException_fair() {
698 <        final Semaphore s = new Semaphore(0, true);
699 <        Thread t = new Thread(new Runnable() {
700 <                public void run() {
701 <                    try {
702 <                        s.acquire();
703 <                        threadShouldThrow();
704 <                    } catch(InterruptedException success){}
705 <                }
706 <            });
707 <        t.start();
708 <        try {
709 <            Thread.sleep(SHORT_DELAY_MS);
710 <            t.interrupt();
711 <            t.join();
712 <        } catch(InterruptedException e){
713 <            unexpectedException();
714 <        }
715 <    }
716 <
717 <    /**
718 <     * A waiting acquire(n) blocks interruptibly
719 <     */
720 <    public void testAcquireN_InterruptedException_fair() {
721 <        final Semaphore s = new Semaphore(2, true);
722 <        Thread t = new Thread(new Runnable() {
723 <                public void run() {
724 <                    try {
725 <                        s.acquire(3);
726 <                        threadShouldThrow();
727 <                    } catch(InterruptedException success){}
728 <                }
729 <            });
730 <        t.start();
731 <        try {
732 <            Thread.sleep(SHORT_DELAY_MS);
733 <            t.interrupt();
734 <            t.join();
735 <        } catch(InterruptedException e){
736 <            unexpectedException();
737 <        }
738 <    }
739 <    
740 <    /**
741 <     *  A waiting tryAcquire blocks interruptibly
742 <     */
743 <    public void testTryAcquire_InterruptedException_fair() {
744 <        final Semaphore s = new Semaphore(0, true);
745 <        Thread t = new Thread(new Runnable() {
746 <                public void run() {
747 <                    try {
748 <                        s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
749 <                        threadShouldThrow();
750 <                    } catch(InterruptedException success){
751 <                    }
752 <                }
753 <            });
754 <        t.start();
755 <        try {
756 <            Thread.sleep(SHORT_DELAY_MS);
757 <            t.interrupt();
758 <            t.join();
759 <        } catch(InterruptedException e){
760 <            unexpectedException();
761 <        }
762 <    }
763 <
764 <    /**
765 <     *  A waiting tryAcquire(n) blocks interruptibly
766 <     */
767 <    public void testTryAcquireN_InterruptedException_fair() {
768 <        final Semaphore s = new Semaphore(1, true);
769 <        Thread t = new Thread(new Runnable() {
770 <                public void run() {
771 <                    try {
772 <                        s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
773 <                        threadShouldThrow();
774 <                    } catch(InterruptedException success){
775 <                    }
776 <                }
777 <            });
778 <        t.start();
779 <        try {
780 <            Thread.sleep(SHORT_DELAY_MS);
781 <            t.interrupt();
782 <            t.join();
783 <        } catch(InterruptedException e){
784 <            unexpectedException();
535 >    public void testReleaseAcquireDifferentThreads_acquire()       { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquire); }
536 >    public void testReleaseAcquireDifferentThreads_acquire_fair()  { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquire); }
537 >    public void testReleaseAcquireDifferentThreads_acquireN()      { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireN); }
538 >    public void testReleaseAcquireDifferentThreads_acquireN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireN); }
539 >    public void testReleaseAcquireDifferentThreads_acquireUninterruptibly()       { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireUninterruptibly); }
540 >    public void testReleaseAcquireDifferentThreads_acquireUninterruptibly_fair()  { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireUninterruptibly); }
541 >    public void testReleaseAcquireDifferentThreads_acquireUninterruptiblyN()      { testReleaseAcquireDifferentThreads(false, AcquireMethod.acquireUninterruptibly); }
542 >    public void testReleaseAcquireDifferentThreads_acquireUninterruptiblyN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.acquireUninterruptibly); }
543 >    public void testReleaseAcquireDifferentThreads_tryAcquireTimed()       { testReleaseAcquireDifferentThreads(false, AcquireMethod.tryAcquireTimed); }
544 >    public void testReleaseAcquireDifferentThreads_tryAcquireTimed_fair()  { testReleaseAcquireDifferentThreads(true, AcquireMethod.tryAcquireTimed); }
545 >    public void testReleaseAcquireDifferentThreads_tryAcquireTimedN()      { testReleaseAcquireDifferentThreads(false, AcquireMethod.tryAcquireTimedN); }
546 >    public void testReleaseAcquireDifferentThreads_tryAcquireTimedN_fair() { testReleaseAcquireDifferentThreads(true, AcquireMethod.tryAcquireTimedN); }
547 >    public void testReleaseAcquireDifferentThreads(boolean fair,
548 >                                                   final AcquireMethod acquirer) {
549 >        final Semaphore s = new Semaphore(0, fair);
550 >        final int rounds = 4;
551 >        long startTime = System.nanoTime();
552 >        Thread t = newStartedThread(new CheckedRunnable() {
553 >            public void realRun() throws InterruptedException {
554 >                for (int i = 0; i < rounds; i++) {
555 >                    assertFalse(s.hasQueuedThreads());
556 >                    if (i % 2 == 0)
557 >                        acquirer.acquire(s);
558 >                    else
559 >                        acquirer.acquire(s, 3);
560 >                }}});
561 >
562 >        for (int i = 0; i < rounds; i++) {
563 >            while (! (s.availablePermits() == 0 && s.hasQueuedThreads()))
564 >                Thread.yield();
565 >            assertTrue(t.isAlive());
566 >            if (i % 2 == 0)
567 >                s.release();
568 >            else
569 >                s.release(3);
570          }
571 +        awaitTermination(t);
572 +        assertEquals(0, s.availablePermits());
573 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
574      }
575  
576      /**
577 <     * getQueueLength reports number of waiting threads
790 <     */
791 <    public void testGetQueueLength_fair() {
792 <        final Semaphore lock = new Semaphore(1, true);
793 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
794 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
795 <        try {
796 <            assertEquals(0, lock.getQueueLength());
797 <            lock.acquireUninterruptibly();
798 <            t1.start();
799 <            Thread.sleep(SHORT_DELAY_MS);
800 <            assertEquals(1, lock.getQueueLength());
801 <            t2.start();
802 <            Thread.sleep(SHORT_DELAY_MS);
803 <            assertEquals(2, lock.getQueueLength());
804 <            t1.interrupt();
805 <            Thread.sleep(SHORT_DELAY_MS);
806 <            assertEquals(1, lock.getQueueLength());
807 <            lock.release();
808 <            Thread.sleep(SHORT_DELAY_MS);
809 <            assertEquals(0, lock.getQueueLength());
810 <            t1.join();
811 <            t2.join();
812 <        } catch(Exception e){
813 <            unexpectedException();
814 <        }
815 <    }
816 <
817 <
818 <    /**
819 <     * a deserialized serialized semaphore has same number of permits
577 >     * fair locks are strictly FIFO
578       */
579 <    public void testSerialization_fair() {
580 <        Semaphore l = new Semaphore(3, true);
581 <
582 <        try {
583 <            l.acquire();
584 <            l.release();
585 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
586 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
587 <            out.writeObject(l);
588 <            out.close();
589 <
590 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
591 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
592 <            Semaphore r = (Semaphore) in.readObject();
593 <            assertEquals(3, r.availablePermits());
594 <            assertTrue(r.isFair());
595 <            r.acquire();
596 <            r.release();
597 <        } catch(Exception e){
598 <            unexpectedException();
599 <        }
579 >    public void testFairLocksFifo() {
580 >        final PublicSemaphore s = new PublicSemaphore(1, true);
581 >        final CountDownLatch pleaseRelease = new CountDownLatch(1);
582 >        Thread t1 = newStartedThread(new CheckedRunnable() {
583 >            public void realRun() throws InterruptedException {
584 >                // Will block; permits are available, but not three
585 >                s.acquire(3);
586 >            }});
587 >
588 >        waitForQueuedThreads(s);
589 >
590 >        Thread t2 = newStartedThread(new CheckedRunnable() {
591 >            public void realRun() throws InterruptedException {
592 >                // Will fail, even though 1 permit is available
593 >                assertFalse(s.tryAcquire(0L, MILLISECONDS));
594 >                assertFalse(s.tryAcquire(1, 0L, MILLISECONDS));
595 >
596 >                // untimed tryAcquire will barge and succeed
597 >                assertTrue(s.tryAcquire());
598 >                s.release(2);
599 >                assertTrue(s.tryAcquire(2));
600 >                s.release();
601 >
602 >                pleaseRelease.countDown();
603 >                // Will queue up behind t1, even though 1 permit is available
604 >                s.acquire();
605 >            }});
606 >
607 >        await(pleaseRelease);
608 >        waitForQueuedThread(s, t2);
609 >        s.release(2);
610 >        awaitTermination(t1);
611 >        assertTrue(t2.isAlive());
612 >        s.release();
613 >        awaitTermination(t2);
614 >   }
615 >
616 >    /**
617 >     * toString indicates current number of permits
618 >     */
619 >    public void testToString()      { testToString(false); }
620 >    public void testToString_fair() { testToString(true); }
621 >    public void testToString(boolean fair) {
622 >        PublicSemaphore s = new PublicSemaphore(0, fair);
623 >        assertTrue(s.toString().contains("Permits = 0"));
624 >        s.release();
625 >        assertTrue(s.toString().contains("Permits = 1"));
626 >        s.release(2);
627 >        assertTrue(s.toString().contains("Permits = 3"));
628 >        s.reducePermits(5);
629 >        assertTrue(s.toString().contains("Permits = -2"));
630      }
631  
844
632   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines