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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.11 by dl, Sun Jan 4 00:57:21 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import junit.framework.*;
# Line 10 | Line 11 | import java.util.*;
11   import java.util.concurrent.*;
12   import java.io.*;
13  
14 < public class SemaphoreTest extends TestCase{
14 <
14 > public class SemaphoreTest extends JSR166TestCase {
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
17      }
18    
18      public static Test suite() {
19          return new TestSuite(SemaphoreTest.class);
20      }
21  
22 <    private static long SHORT_DELAY_MS = 100;
23 <    private static long MEDIUM_DELAY_MS = 1000;
24 <    private static long LONG_DELAY_MS = 10000;
25 <
22 >    /**
23 >     * Subclass to expose protected methods
24 >     */
25 >    static class PublicSemaphore extends Semaphore {
26 >        PublicSemaphore(int p, boolean f) { super(p, f); }
27 >        public Collection<Thread> getQueuedThreads() {
28 >            return super.getQueuedThreads();
29 >        }
30 >        public void reducePermits(int p) {
31 >            super.reducePermits(p);
32 >        }
33 >    }
34  
35 <    public void testConstructor1() {
36 <        Semaphore s = new Semaphore(0);
37 <        assertEquals(0, s.availablePermits());
35 >    /**
36 >     * A runnable calling acquire
37 >     */
38 >    class InterruptibleLockRunnable implements Runnable {
39 >        final Semaphore lock;
40 >        InterruptibleLockRunnable(Semaphore l) { lock = l; }
41 >        public void run() {
42 >            try {
43 >                lock.acquire();
44 >            } catch(InterruptedException success){}
45 >        }
46      }
47  
48 <    public void testConstructor2() {
49 <        Semaphore s = new Semaphore(-1);
50 <        assertEquals(-1, s.availablePermits());
48 >
49 >    /**
50 >     * A runnable calling acquire that expects to be
51 >     * interrupted
52 >     */
53 >    class InterruptedLockRunnable implements Runnable {
54 >        final Semaphore lock;
55 >        InterruptedLockRunnable(Semaphore l) { lock = l; }
56 >        public void run() {
57 >            try {
58 >                lock.acquire();
59 >                threadShouldThrow();
60 >            } catch(InterruptedException success){}
61 >        }
62      }
63  
64 +    /**
65 +     * Zero, negative, and positive initial values are allowed in constructor
66 +     */
67 +    public void testConstructor() {
68 +        Semaphore s0 = new Semaphore(0, false);
69 +        assertEquals(0, s0.availablePermits());
70 +        assertFalse(s0.isFair());
71 +        Semaphore s1 = new Semaphore(-1, false);
72 +        assertEquals(-1, s1.availablePermits());
73 +        Semaphore s2 = new Semaphore(-1, false);
74 +        assertEquals(-1, s2.availablePermits());
75 +    }
76 +
77 +    /**
78 +     * tryAcquire succeeds when sufficient permits, else fails
79 +     */
80      public void testTryAcquireInSameThread() {
81 <        Semaphore s = new Semaphore(2);
81 >        Semaphore s = new Semaphore(2, false);
82          assertEquals(2, s.availablePermits());
83          assertTrue(s.tryAcquire());
84          assertTrue(s.tryAcquire());
# Line 44 | Line 86 | public class SemaphoreTest extends TestC
86          assertFalse(s.tryAcquire());
87      }
88  
89 <    public void testAcquireReleaseInSameThread(){
90 <        Semaphore s = new Semaphore(1);
89 >    /**
90 >     * 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();
# Line 59 | Line 104 | public class SemaphoreTest extends TestC
104              s.release();
105              assertEquals(1, s.availablePermits());
106          } catch( InterruptedException e){
107 <            fail("unexpected exception");
107 >            unexpectedException();
108          }
109      }
110  
111 <    public void testAcquireUninterruptiblyReleaseInSameThread(){
112 <        Semaphore s = new Semaphore(1);
111 >    /**
112 >     * Uninterruptible acquire and release of semaphore succeed if
113 >     * initially available
114 >     */
115 >    public void testAcquireUninterruptiblyReleaseInSameThread() {
116 >        Semaphore s = new Semaphore(1, false);
117          try {
118              s.acquireUninterruptibly();
119              s.release();
# Line 81 | Line 130 | public class SemaphoreTest extends TestC
130          }
131      }
132  
133 +    /**
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 +        }
154 +    }
155  
156 +    /**
157 +     * A release in one thread enables an acquire in another thread
158 +     */
159      public void testAcquireReleaseInDifferentThreads() {
160 <        final Semaphore s = new Semaphore(1);
161 <        Thread t = new Thread(new Runnable(){
162 <                public void run(){
163 <                    try{
160 >        final Semaphore s = new Semaphore(0, false);
161 >        Thread t = new Thread(new Runnable() {
162 >                public void run() {
163 >                    try {
164                          s.acquire();
165                          s.release();
166                          s.release();
167                          s.acquire();
168 <                    }catch(InterruptedException ie){
169 <                        fail("unexpected exception");
168 >                    } catch(InterruptedException ie){
169 >                        threadUnexpectedException();
170                      }
171                  }
172              });
99        t.start();
173          try {
174 +            t.start();
175 +            Thread.sleep(SHORT_DELAY_MS);
176              s.release();
177              s.release();
178              s.acquire();
179              s.acquire();
180 +            s.release();
181 +            t.join();
182 +        } catch( InterruptedException e){
183 +            unexpectedException();
184 +        }
185 +    }
186 +
187 +    /**
188 +     * A release in one thread enables an uninterruptible acquire in another thread
189 +     */
190 +    public void testUninterruptibleAcquireReleaseInDifferentThreads() {
191 +        final Semaphore s = new Semaphore(0, false);
192 +        Thread t = new Thread(new Runnable() {
193 +                public void run() {
194 +                    s.acquireUninterruptibly();
195 +                    s.release();
196 +                    s.release();
197 +                    s.acquireUninterruptibly();
198 +                }
199 +            });
200 +        try {
201 +            t.start();
202 +            Thread.sleep(SHORT_DELAY_MS);
203 +            s.release();
204 +            s.release();
205 +            s.acquireUninterruptibly();
206 +            s.acquireUninterruptibly();
207 +            s.release();
208              t.join();
209          } catch( InterruptedException e){
210 <            fail("unexpected exception");
210 >            unexpectedException();
211 >        }
212 >    }
213 >
214 >
215 >    /**
216 >     *  A release in one thread enables a timed acquire in another thread
217 >     */
218 >    public void testTimedAcquireReleaseInDifferentThreads() {
219 >        final Semaphore s = new Semaphore(1, false);
220 >        Thread t = new Thread(new Runnable() {
221 >                public void run() {
222 >                    try {
223 >                        s.release();
224 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
225 >                        s.release();
226 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
227 >
228 >                    } catch(InterruptedException ie){
229 >                        threadUnexpectedException();
230 >                    }
231 >                }
232 >            });
233 >        try {
234 >            t.start();
235 >            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
236 >            s.release();
237 >            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
238 >            s.release();
239 >            s.release();
240 >            t.join();
241 >        } catch( InterruptedException e){
242 >            unexpectedException();
243 >        }
244 >    }
245 >
246 >    /**
247 >     * A waiting acquire blocks interruptibly
248 >     */
249 >    public void testAcquire_InterruptedException() {
250 >        final Semaphore s = new Semaphore(0, false);
251 >        Thread t = new Thread(new Runnable() {
252 >                public void run() {
253 >                    try {
254 >                        s.acquire();
255 >                        threadShouldThrow();
256 >                    } catch(InterruptedException success){}
257 >                }
258 >            });
259 >        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 >        }
291 >    }
292 >
293 >    /**
294 >     * 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
324 >     */
325 >    public void testGetQueueLength() {
326 >        final Semaphore lock = new Semaphore(1, false);
327 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
328 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
329 >        try {
330 >            assertEquals(0, lock.getQueueLength());
331 >            lock.acquireUninterruptibly();
332 >            t1.start();
333 >            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 >    }
350 >
351 >    /**
352 >     * getQueuedThreads includes waiting threads
353 >     */
354 >    public void testGetQueuedThreads() {
355 >        final PublicSemaphore lock = new PublicSemaphore(1, false);
356 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
357 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
358 >        try {
359 >            assertTrue(lock.getQueuedThreads().isEmpty());
360 >            lock.acquireUninterruptibly();
361 >            assertTrue(lock.getQueuedThreads().isEmpty());
362 >            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 >
383 >    /**
384 >     * drainPermits reports and removes given number of permits
385 >     */
386 >    public void testDrainPermits() {
387 >        Semaphore s = new Semaphore(0, false);
388 >        assertEquals(0, s.availablePermits());
389 >        assertEquals(0, s.drainPermits());
390 >        s.release(10);
391 >        assertEquals(10, s.availablePermits());
392 >        assertEquals(10, s.drainPermits());
393 >        assertEquals(0, s.availablePermits());
394 >        assertEquals(0, s.drainPermits());
395 >    }
396 >
397 >    /**
398 >     * reducePermits reduces number of permits
399 >     */
400 >    public void testReducePermits() {
401 >        PublicSemaphore s = new PublicSemaphore(10, false);
402 >        assertEquals(10, s.availablePermits());
403 >        s.reducePermits(1);
404 >        assertEquals(9, s.availablePermits());
405 >        s.reducePermits(10);
406 >        assertEquals(-1, s.availablePermits());
407 >    }
408 >
409 >    /**
410 >     * a deserialized serialized semaphore has same number of permits
411 >     */
412 >    public void testSerialization() {
413 >        Semaphore l = new Semaphore(3, false);
414 >        try {
415 >            l.acquire();
416 >            l.release();
417 >            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
418 >            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
419 >            out.writeObject(l);
420 >            out.close();
421 >
422 >            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
423 >            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
424 >            Semaphore r = (Semaphore) in.readObject();
425 >            assertEquals(3, r.availablePermits());
426 >            assertFalse(r.isFair());
427 >            r.acquire();
428 >            r.release();
429 >        } catch(Exception e){
430 >            unexpectedException();
431 >        }
432 >    }
433 >
434 >
435 >    /**
436 >     * Zero, negative, and positive initial values are allowed in constructor
437 >     */
438 >    public void testConstructor_fair() {
439 >        Semaphore s0 = new Semaphore(0, true);
440 >        assertEquals(0, s0.availablePermits());
441 >        assertTrue(s0.isFair());
442 >        Semaphore s1 = new Semaphore(-1, true);
443 >        assertEquals(-1, s1.availablePermits());
444 >        Semaphore s2 = new Semaphore(-1, true);
445 >        assertEquals(-1, s2.availablePermits());
446 >    }
447 >
448 >    /**
449 >     * tryAcquire succeeds when sufficient permits, else fails
450 >     */
451 >    public void testTryAcquireInSameThread_fair() {
452 >        Semaphore s = new Semaphore(2, true);
453 >        assertEquals(2, s.availablePermits());
454 >        assertTrue(s.tryAcquire());
455 >        assertTrue(s.tryAcquire());
456 >        assertEquals(0, s.availablePermits());
457 >        assertFalse(s.tryAcquire());
458 >    }
459 >
460 >    /**
461 >     * tryAcquire(n) succeeds when sufficient permits, else fails
462 >     */
463 >    public void testTryAcquireNInSameThread_fair() {
464 >        Semaphore s = new Semaphore(2, true);
465 >        assertEquals(2, s.availablePermits());
466 >        assertTrue(s.tryAcquire(2));
467 >        assertEquals(0, s.availablePermits());
468 >        assertFalse(s.tryAcquire());
469 >    }
470 >
471 >    /**
472 >     * Acquire and release of semaphore succeed if initially available
473 >     */
474 >    public void testAcquireReleaseInSameThread_fair() {
475 >        Semaphore s = new Semaphore(1, true);
476 >        try {
477 >            s.acquire();
478 >            s.release();
479 >            s.acquire();
480 >            s.release();
481 >            s.acquire();
482 >            s.release();
483 >            s.acquire();
484 >            s.release();
485 >            s.acquire();
486 >            s.release();
487 >            assertEquals(1, s.availablePermits());
488 >        } catch( InterruptedException e){
489 >            unexpectedException();
490 >        }
491 >    }
492 >
493 >    /**
494 >     * Acquire(n) and release(n) of semaphore succeed if initially available
495 >     */
496 >    public void testAcquireReleaseNInSameThread_fair() {
497 >        Semaphore s = new Semaphore(1, true);
498 >        try {
499 >            s.release(1);
500 >            s.acquire(1);
501 >            s.release(2);
502 >            s.acquire(2);
503 >            s.release(3);
504 >            s.acquire(3);
505 >            s.release(4);
506 >            s.acquire(4);
507 >            s.release(5);
508 >            s.acquire(5);
509 >            assertEquals(1, s.availablePermits());
510 >        } catch( InterruptedException e){
511 >            unexpectedException();
512 >        }
513 >    }
514 >
515 >    /**
516 >     * Acquire(n) and release(n) of semaphore succeed if initially available
517 >     */
518 >    public void testAcquireUninterruptiblyReleaseNInSameThread_fair() {
519 >        Semaphore s = new Semaphore(1, true);
520 >        try {
521 >            s.release(1);
522 >            s.acquireUninterruptibly(1);
523 >            s.release(2);
524 >            s.acquireUninterruptibly(2);
525 >            s.release(3);
526 >            s.acquireUninterruptibly(3);
527 >            s.release(4);
528 >            s.acquireUninterruptibly(4);
529 >            s.release(5);
530 >            s.acquireUninterruptibly(5);
531 >            assertEquals(1, s.availablePermits());
532 >        } finally {
533 >        }
534 >    }
535 >
536 >    /**
537 >     * release(n) in one thread enables timed acquire(n) in another thread
538 >     */
539 >    public void testTimedAcquireReleaseNInSameThread_fair() {
540 >        Semaphore s = new Semaphore(1, true);
541 >        try {
542 >            s.release(1);
543 >            assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
544 >            s.release(2);
545 >            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
546 >            s.release(3);
547 >            assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
548 >            s.release(4);
549 >            assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
550 >            s.release(5);
551 >            assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552 >            assertEquals(1, s.availablePermits());
553 >        } catch( InterruptedException e){
554 >            unexpectedException();
555          }
556      }
557  
558 <    public void testTimedAcquireReleaseInSameThread(){
559 <        Semaphore s = new Semaphore(1);
558 >    /**
559 >     * release in one thread enables timed acquire in another thread
560 >     */
561 >    public void testTimedAcquireReleaseInSameThread_fair() {
562 >        Semaphore s = new Semaphore(1, true);
563          try {
564              assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
565              s.release();
# Line 123 | Line 573 | public class SemaphoreTest extends TestC
573              s.release();
574              assertEquals(1, s.availablePermits());
575          } catch( InterruptedException e){
576 <            fail("unexpected exception");
576 >            unexpectedException();
577          }
578      }
579  
580 <    public void testTimedAcquireReleaseInDifferentThreads() {
581 <        final Semaphore s = new Semaphore(1);
582 <        Thread t = new Thread(new Runnable(){
583 <                public void run(){
584 <                    try{
585 <                        s.release();
586 <                        assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
587 <                        s.release();
588 <                        assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
580 >    /**
581 >     * A release in one thread enables an acquire in another thread
582 >     */
583 >    public void testAcquireReleaseInDifferentThreads_fair() {
584 >        final Semaphore s = new Semaphore(0, true);
585 >        Thread t = new Thread(new Runnable() {
586 >                public void run() {
587 >                    try {
588 >                        s.acquire();
589 >                        s.acquire();
590 >                        s.acquire();
591 >                        s.acquire();
592 >                    } catch(InterruptedException ie){
593 >                        threadUnexpectedException();
594 >                    }
595 >                }
596 >            });
597 >        try {
598 >            t.start();
599 >            Thread.sleep(SHORT_DELAY_MS);
600 >            s.release();
601 >            s.release();
602 >            s.release();
603 >            s.release();
604 >            s.release();
605 >            s.release();
606 >            t.join();
607 >            assertEquals(2, s.availablePermits());
608 >        } catch( InterruptedException e){
609 >            unexpectedException();
610 >        }
611 >    }
612  
613 <                    }catch(InterruptedException ie){
614 <                        fail("unexpected exception");
613 >    /**
614 >     * release(n) in one thread enables acquire(n) in another thread
615 >     */
616 >    public void testAcquireReleaseNInDifferentThreads_fair() {
617 >        final Semaphore s = new Semaphore(0, true);
618 >        Thread t = new Thread(new Runnable() {
619 >                public void run() {
620 >                    try {
621 >                        s.acquire();
622 >                        s.release(2);
623 >                        s.acquire();
624 >                    } catch(InterruptedException ie){
625 >                        threadUnexpectedException();
626 >                    }
627 >                }
628 >            });
629 >        try {
630 >            t.start();
631 >            Thread.sleep(SHORT_DELAY_MS);
632 >            s.release(2);
633 >            s.acquire(2);
634 >            s.release(1);
635 >            t.join();
636 >        } catch( InterruptedException e){
637 >            unexpectedException();
638 >        }
639 >    }
640 >
641 >
642 >
643 >    /**
644 >     * release in one thread enables timed acquire in another thread
645 >     */
646 >    public void testTimedAcquireReleaseInDifferentThreads_fair() {
647 >        final Semaphore s = new Semaphore(1, true);
648 >        Thread t = new Thread(new Runnable() {
649 >                public void run() {
650 >                    try {
651 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
652 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
653 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
654 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
655 >                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
656 >
657 >                    } catch(InterruptedException ie){
658 >                        threadUnexpectedException();
659                      }
660                  }
661              });
662          t.start();
663          try {
147            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
664              s.release();
665 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
665 >            s.release();
666 >            s.release();
667 >            s.release();
668              s.release();
669              t.join();
670          } catch( InterruptedException e){
671 <            fail("unexpected exception");
671 >            unexpectedException();
672          }
673      }
674  
675 <    public void testAcquire_InterruptedException(){
676 <        final Semaphore s = new Semaphore(0);
677 <        Thread t = new Thread(new Runnable(){
678 <                public void run(){
679 <                    try{
675 >    /**
676 >     * release(n) in one thread enables timed acquire(n) in another thread
677 >     */
678 >    public void testTimedAcquireReleaseNInDifferentThreads_fair() {
679 >        final Semaphore s = new Semaphore(2, true);
680 >        Thread t = new Thread(new Runnable() {
681 >                public void run() {
682 >                    try {
683 >                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
684 >                        s.release(2);
685 >                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
686 >                        s.release(2);
687 >                    } catch(InterruptedException ie){
688 >                        threadUnexpectedException();
689 >                    }
690 >                }
691 >            });
692 >        t.start();
693 >        try {
694 >            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
695 >            s.release(2);
696 >            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
697 >            s.release(2);
698 >            t.join();
699 >        } catch( InterruptedException e){
700 >            unexpectedException();
701 >        }
702 >    }
703 >
704 >    /**
705 >     * A waiting acquire blocks interruptibly
706 >     */
707 >    public void testAcquire_InterruptedException_fair() {
708 >        final Semaphore s = new Semaphore(0, true);
709 >        Thread t = new Thread(new Runnable() {
710 >                public void run() {
711 >                    try {
712                          s.acquire();
713 <                        fail("should throw");
714 <                    }catch(InterruptedException success){}
713 >                        threadShouldThrow();
714 >                    } catch(InterruptedException success){}
715                  }
716              });
717          t.start();
718 <        try{
718 >        try {
719              Thread.sleep(SHORT_DELAY_MS);
720              t.interrupt();
721              t.join();
722          } catch(InterruptedException e){
723 <            fail("unexpected exception");
723 >            unexpectedException();
724 >        }
725 >    }
726 >
727 >    /**
728 >     * A waiting acquire(n) blocks interruptibly
729 >     */
730 >    public void testAcquireN_InterruptedException_fair() {
731 >        final Semaphore s = new Semaphore(2, true);
732 >        Thread t = new Thread(new Runnable() {
733 >                public void run() {
734 >                    try {
735 >                        s.acquire(3);
736 >                        threadShouldThrow();
737 >                    } catch(InterruptedException success){}
738 >                }
739 >            });
740 >        t.start();
741 >        try {
742 >            Thread.sleep(SHORT_DELAY_MS);
743 >            t.interrupt();
744 >            t.join();
745 >        } catch(InterruptedException e){
746 >            unexpectedException();
747          }
748      }
749      
750 <    public void testTryAcquire_InterruptedException(){
751 <        final Semaphore s = new Semaphore(0);
752 <        Thread t = new Thread(new Runnable(){
753 <                public void run(){
754 <                    try{
750 >    /**
751 >     *  A waiting tryAcquire blocks interruptibly
752 >     */
753 >    public void testTryAcquire_InterruptedException_fair() {
754 >        final Semaphore s = new Semaphore(0, true);
755 >        Thread t = new Thread(new Runnable() {
756 >                public void run() {
757 >                    try {
758                          s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
759 <                        fail("should throw");
760 <                    }catch(InterruptedException success){
759 >                        threadShouldThrow();
760 >                    } catch(InterruptedException success){
761                      }
762                  }
763              });
764          t.start();
765 <        try{
765 >        try {
766              Thread.sleep(SHORT_DELAY_MS);
767              t.interrupt();
768              t.join();
769          } catch(InterruptedException e){
770 <            fail("unexpected exception");
770 >            unexpectedException();
771          }
772      }
773  
774 <    public void testSerialization() {
775 <        Semaphore l = new Semaphore(3);
774 >    /**
775 >     *  A waiting tryAcquire(n) blocks interruptibly
776 >     */
777 >    public void testTryAcquireN_InterruptedException_fair() {
778 >        final Semaphore s = new Semaphore(1, true);
779 >        Thread t = new Thread(new Runnable() {
780 >                public void run() {
781 >                    try {
782 >                        s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
783 >                        threadShouldThrow();
784 >                    } catch(InterruptedException success){
785 >                    }
786 >                }
787 >            });
788 >        t.start();
789 >        try {
790 >            Thread.sleep(SHORT_DELAY_MS);
791 >            t.interrupt();
792 >            t.join();
793 >        } catch(InterruptedException e){
794 >            unexpectedException();
795 >        }
796 >    }
797 >
798 >    /**
799 >     * getQueueLength reports number of waiting threads
800 >     */
801 >    public void testGetQueueLength_fair() {
802 >        final Semaphore lock = new Semaphore(1, true);
803 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
804 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
805 >        try {
806 >            assertEquals(0, lock.getQueueLength());
807 >            lock.acquireUninterruptibly();
808 >            t1.start();
809 >            Thread.sleep(SHORT_DELAY_MS);
810 >            assertEquals(1, lock.getQueueLength());
811 >            t2.start();
812 >            Thread.sleep(SHORT_DELAY_MS);
813 >            assertEquals(2, lock.getQueueLength());
814 >            t1.interrupt();
815 >            Thread.sleep(SHORT_DELAY_MS);
816 >            assertEquals(1, lock.getQueueLength());
817 >            lock.release();
818 >            Thread.sleep(SHORT_DELAY_MS);
819 >            assertEquals(0, lock.getQueueLength());
820 >            t1.join();
821 >            t2.join();
822 >        } catch(Exception e){
823 >            unexpectedException();
824 >        }
825 >    }
826 >
827 >
828 >    /**
829 >     * a deserialized serialized semaphore has same number of permits
830 >     */
831 >    public void testSerialization_fair() {
832 >        Semaphore l = new Semaphore(3, true);
833  
834          try {
835              l.acquire();
# Line 210 | Line 843 | public class SemaphoreTest extends TestC
843              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
844              Semaphore r = (Semaphore) in.readObject();
845              assertEquals(3, r.availablePermits());
846 +            assertTrue(r.isFair());
847              r.acquire();
848              r.release();
849          } catch(Exception e){
850 <            e.printStackTrace();
217 <            fail("unexpected exception");
850 >            unexpectedException();
851          }
852      }
853  
854 +
855   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines