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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.10 by dl, Fri Jan 2 00:38:47 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 11 | Line 12 | import java.util.concurrent.*;
12   import java.io.*;
13  
14   public class SemaphoreTest extends JSR166TestCase {
14
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 <    public void testConstructor1() {
23 <        Semaphore s = new Semaphore(0);
24 <        assertEquals(0, s.availablePermits());
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 testConstructor2() {
36 <        Semaphore s = new Semaphore(-1);
37 <        assertEquals(-1, 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 >
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 39 | Line 86 | public class SemaphoreTest extends JSR16
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 54 | Line 104 | public class SemaphoreTest extends JSR16
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 76 | Line 130 | public class SemaphoreTest extends JSR16
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 <                        threadFail("unexpected exception");
168 >                    } catch(InterruptedException ie){
169 >                        threadUnexpectedException();
170                      }
171                  }
172              });
94        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 +            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 <            fail("unexpected exception");
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 >    /**
385 >     * reducePermits reduces number of permits
386 >     */
387 >    public void testReducePermits() {
388 >        PublicSemaphore s = new PublicSemaphore(10, false);
389 >        assertEquals(10, s.availablePermits());
390 >        s.reducePermits(1);
391 >        assertEquals(9, s.availablePermits());
392 >        s.reducePermits(10);
393 >        assertEquals(-1, s.availablePermits());
394 >    }
395 >
396 >    /**
397 >     * a deserialized serialized semaphore has same number of permits
398 >     */
399 >    public void testSerialization() {
400 >        Semaphore l = new Semaphore(3, false);
401 >        try {
402 >            l.acquire();
403 >            l.release();
404 >            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
405 >            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 >    }
420 >
421 >
422 >    /**
423 >     * Zero, negative, and positive initial values are allowed in constructor
424 >     */
425 >    public void testConstructor_fair() {
426 >        Semaphore s0 = new Semaphore(0, true);
427 >        assertEquals(0, s0.availablePermits());
428 >        assertTrue(s0.isFair());
429 >        Semaphore s1 = new Semaphore(-1, true);
430 >        assertEquals(-1, s1.availablePermits());
431 >        Semaphore s2 = new Semaphore(-1, true);
432 >        assertEquals(-1, s2.availablePermits());
433 >    }
434 >
435 >    /**
436 >     * 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());
445 >    }
446 >
447 >    /**
448 >     * tryAcquire(n) succeeds when sufficient permits, else fails
449 >     */
450 >    public void testTryAcquireNInSameThread_fair() {
451 >        Semaphore s = new Semaphore(2, true);
452 >        assertEquals(2, s.availablePermits());
453 >        assertTrue(s.tryAcquire(2));
454 >        assertEquals(0, s.availablePermits());
455 >        assertFalse(s.tryAcquire());
456 >    }
457 >
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          }
543      }
544  
545 <    public void testTimedAcquireReleaseInSameThread(){
546 <        Semaphore s = new Semaphore(1);
545 >    /**
546 >     * release in one thread enables timed acquire in another thread
547 >     */
548 >    public void testTimedAcquireReleaseInSameThread_fair() {
549 >        Semaphore s = new Semaphore(1, true);
550          try {
551              assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552              s.release();
# Line 118 | Line 560 | public class SemaphoreTest extends JSR16
560              s.release();
561              assertEquals(1, s.availablePermits());
562          } catch( InterruptedException e){
563 <            fail("unexpected exception");
563 >            unexpectedException();
564          }
565      }
566  
567 <    public void testTimedAcquireReleaseInDifferentThreads() {
568 <        final Semaphore s = new Semaphore(1);
569 <        Thread t = new Thread(new Runnable(){
570 <                public void run(){
571 <                    try{
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();
597 >        }
598 >    }
599 >
600 >    /**
601 >     * release(n) in one thread enables acquire(n) in another thread
602 >     */
603 >    public void testAcquireReleaseNInDifferentThreads_fair() {
604 >        final Semaphore s = new Semaphore(0, true);
605 >        Thread t = new Thread(new Runnable() {
606 >                public void run() {
607 >                    try {
608 >                        s.acquire();
609                          s.release();
131                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
610                          s.release();
611 +                        s.acquire();
612 +                    } catch(InterruptedException ie){
613 +                        threadUnexpectedException();
614 +                    }
615 +                }
616 +            });
617 +        try {
618 +            t.start();
619 +            Thread.sleep(SHORT_DELAY_MS);
620 +            s.release();
621 +            s.release();
622 +            s.acquire();
623 +            s.acquire();
624 +            s.release();
625 +            t.join();
626 +        } catch( InterruptedException e){
627 +            unexpectedException();
628 +        }
629 +    }
630 +
631 +
632 +
633 +    /**
634 +     * release in one thread enables timed acquire in another thread
635 +     */
636 +    public void testTimedAcquireReleaseInDifferentThreads_fair() {
637 +        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 <                        threadFail("unexpected exception");
647 >                    } catch(InterruptedException ie){
648 >                        threadUnexpectedException();
649                      }
650                  }
651              });
652          t.start();
653          try {
142            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
654              s.release();
655 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
655 >            s.release();
656 >            s.release();
657 >            s.release();
658              s.release();
659              t.join();
660          } catch( InterruptedException e){
661 <            fail("unexpected exception");
661 >            unexpectedException();
662          }
663      }
664  
665 <    public void testAcquire_InterruptedException(){
666 <        final Semaphore s = new Semaphore(0);
667 <        Thread t = new Thread(new Runnable(){
668 <                public void run(){
669 <                    try{
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 <                        threadFail("should throw");
704 <                    }catch(InterruptedException success){}
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{
731 >        try {
732              Thread.sleep(SHORT_DELAY_MS);
733              t.interrupt();
734              t.join();
735          } catch(InterruptedException e){
736 <            fail("unexpected exception");
736 >            unexpectedException();
737          }
738      }
739      
740 <    public void testTryAcquire_InterruptedException(){
741 <        final Semaphore s = new Semaphore(0);
742 <        Thread t = new Thread(new Runnable(){
743 <                public void run(){
744 <                    try{
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 <                        threadFail("should throw");
750 <                    }catch(InterruptedException success){
749 >                        threadShouldThrow();
750 >                    } catch(InterruptedException success){
751                      }
752                  }
753              });
754          t.start();
755 <        try{
755 >        try {
756              Thread.sleep(SHORT_DELAY_MS);
757              t.interrupt();
758              t.join();
759          } catch(InterruptedException e){
760 <            fail("unexpected exception");
760 >            unexpectedException();
761          }
762      }
763  
764 <    public void testSerialization() {
765 <        Semaphore l = new Semaphore(3);
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();
785 >        }
786 >    }
787 >
788 >    /**
789 >     * 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
820 >     */
821 >    public void testSerialization_fair() {
822 >        Semaphore l = new Semaphore(3, true);
823 >
824          try {
825              l.acquire();
826              l.release();
# Line 204 | Line 833 | public class SemaphoreTest extends JSR16
833              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
834              Semaphore r = (Semaphore) in.readObject();
835              assertEquals(3, r.availablePermits());
836 +            assertTrue(r.isFair());
837              r.acquire();
838              r.release();
839          } catch(Exception e){
840 <            e.printStackTrace();
211 <            fail("unexpected exception");
840 >            unexpectedException();
841          }
842      }
843  
844 +
845   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines