ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SemaphoreTest.java
Revision: 1.11
Committed: Sun Jan 4 00:57:21 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.10: +17 -7 lines
Log Message:
added a few tests

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.7 * 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 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 dl 1.2 import java.io.*;
13 dl 1.1
14 dl 1.3 public class SemaphoreTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(SemaphoreTest.class);
20     }
21    
22 dl 1.4 /**
23 dl 1.6 * 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     /**
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 dl 1.5 * Zero, negative, and positive initial values are allowed in constructor
66 dl 1.4 */
67 dl 1.5 public void testConstructor() {
68 dl 1.6 Semaphore s0 = new Semaphore(0, false);
69 dl 1.5 assertEquals(0, s0.availablePermits());
70 dl 1.6 assertFalse(s0.isFair());
71     Semaphore s1 = new Semaphore(-1, false);
72 dl 1.5 assertEquals(-1, s1.availablePermits());
73 dl 1.6 Semaphore s2 = new Semaphore(-1, false);
74 dl 1.5 assertEquals(-1, s2.availablePermits());
75 dl 1.1 }
76    
77 dl 1.4 /**
78 dl 1.9 * tryAcquire succeeds when sufficient permits, else fails
79 dl 1.4 */
80 dl 1.1 public void testTryAcquireInSameThread() {
81 dl 1.6 Semaphore s = new Semaphore(2, false);
82 dl 1.1 assertEquals(2, s.availablePermits());
83     assertTrue(s.tryAcquire());
84     assertTrue(s.tryAcquire());
85     assertEquals(0, s.availablePermits());
86     assertFalse(s.tryAcquire());
87     }
88    
89 dl 1.4 /**
90 dl 1.5 * Acquire and release of semaphore succeed if initially available
91 dl 1.4 */
92     public void testAcquireReleaseInSameThread() {
93 dl 1.6 Semaphore s = new Semaphore(1, false);
94 dl 1.1 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 dl 1.4 unexpectedException();
108 dl 1.1 }
109     }
110    
111 dl 1.4 /**
112 dl 1.5 * Uninterruptible acquire and release of semaphore succeed if
113     * initially available
114 dl 1.4 */
115     public void testAcquireUninterruptiblyReleaseInSameThread() {
116 dl 1.6 Semaphore s = new Semaphore(1, false);
117 dl 1.1 try {
118     s.acquireUninterruptibly();
119     s.release();
120     s.acquireUninterruptibly();
121     s.release();
122     s.acquireUninterruptibly();
123     s.release();
124     s.acquireUninterruptibly();
125     s.release();
126     s.acquireUninterruptibly();
127     s.release();
128     assertEquals(1, s.availablePermits());
129     } finally {
130     }
131     }
132    
133 dl 1.5 /**
134     * Timed Acquire and release of semaphore succeed if
135     * initially available
136     */
137     public void testTimedAcquireReleaseInSameThread() {
138 dl 1.6 Semaphore s = new Semaphore(1, false);
139 dl 1.5 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 dl 1.1
156 dl 1.4 /**
157 dl 1.5 * A release in one thread enables an acquire in another thread
158 dl 1.4 */
159 dl 1.1 public void testAcquireReleaseInDifferentThreads() {
160 dl 1.6 final Semaphore s = new Semaphore(0, false);
161 dl 1.4 Thread t = new Thread(new Runnable() {
162     public void run() {
163     try {
164 dl 1.1 s.acquire();
165     s.release();
166     s.release();
167     s.acquire();
168 dl 1.4 } catch(InterruptedException ie){
169     threadUnexpectedException();
170 dl 1.1 }
171     }
172     });
173     try {
174 dl 1.5 t.start();
175     Thread.sleep(SHORT_DELAY_MS);
176 dl 1.1 s.release();
177     s.release();
178     s.acquire();
179     s.acquire();
180 dl 1.5 s.release();
181 dl 1.1 t.join();
182     } catch( InterruptedException e){
183 dl 1.4 unexpectedException();
184 dl 1.1 }
185     }
186    
187 dl 1.4 /**
188 dl 1.5 * A release in one thread enables an uninterruptible acquire in another thread
189 dl 1.4 */
190 dl 1.5 public void testUninterruptibleAcquireReleaseInDifferentThreads() {
191 dl 1.6 final Semaphore s = new Semaphore(0, false);
192 dl 1.5 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 dl 1.1 try {
201 dl 1.5 t.start();
202     Thread.sleep(SHORT_DELAY_MS);
203 dl 1.1 s.release();
204     s.release();
205 dl 1.5 s.acquireUninterruptibly();
206     s.acquireUninterruptibly();
207 dl 1.1 s.release();
208 dl 1.5 t.join();
209 dl 1.1 } catch( InterruptedException e){
210 dl 1.4 unexpectedException();
211 dl 1.1 }
212     }
213    
214 dl 1.5
215 dl 1.4 /**
216 dl 1.5 * A release in one thread enables a timed acquire in another thread
217 dl 1.4 */
218 dl 1.1 public void testTimedAcquireReleaseInDifferentThreads() {
219 dl 1.6 final Semaphore s = new Semaphore(1, false);
220 dl 1.4 Thread t = new Thread(new Runnable() {
221     public void run() {
222     try {
223 dl 1.1 s.release();
224 dl 1.3 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
225 dl 1.1 s.release();
226 dl 1.3 threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
227 dl 1.1
228 dl 1.4 } catch(InterruptedException ie){
229     threadUnexpectedException();
230 dl 1.1 }
231     }
232     });
233     try {
234 dl 1.5 t.start();
235 dl 1.1 assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
236     s.release();
237     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
238     s.release();
239 dl 1.5 s.release();
240 dl 1.1 t.join();
241     } catch( InterruptedException e){
242 dl 1.4 unexpectedException();
243 dl 1.1 }
244     }
245    
246 dl 1.4 /**
247 dl 1.5 * A waiting acquire blocks interruptibly
248 dl 1.4 */
249     public void testAcquire_InterruptedException() {
250 dl 1.6 final Semaphore s = new Semaphore(0, false);
251 dl 1.4 Thread t = new Thread(new Runnable() {
252     public void run() {
253     try {
254 dl 1.1 s.acquire();
255 dl 1.4 threadShouldThrow();
256     } catch(InterruptedException success){}
257 dl 1.1 }
258     });
259     t.start();
260 dl 1.4 try {
261 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
262     t.interrupt();
263     t.join();
264     } catch(InterruptedException e){
265 dl 1.4 unexpectedException();
266 dl 1.1 }
267     }
268    
269 dl 1.4 /**
270 dl 1.5 * A waiting timed acquire blocks interruptibly
271 dl 1.4 */
272     public void testTryAcquire_InterruptedException() {
273 dl 1.6 final Semaphore s = new Semaphore(0, false);
274 dl 1.4 Thread t = new Thread(new Runnable() {
275     public void run() {
276     try {
277 dl 1.1 s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
278 dl 1.4 threadShouldThrow();
279     } catch(InterruptedException success){
280 dl 1.1 }
281     }
282     });
283     t.start();
284 dl 1.4 try {
285 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
286     t.interrupt();
287     t.join();
288     } catch(InterruptedException e){
289 dl 1.4 unexpectedException();
290 dl 1.1 }
291     }
292 dl 1.2
293 dl 1.4 /**
294 dl 1.8 * 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 dl 1.6 * 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 dl 1.11 /**
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 dl 1.6
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 dl 1.5 * a deserialized serialized semaphore has same number of permits
411 dl 1.4 */
412 dl 1.2 public void testSerialization() {
413 dl 1.6 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 dl 1.9 * tryAcquire succeeds when sufficient permits, else fails
450 dl 1.6 */
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 dl 1.9 * tryAcquire(n) succeeds when sufficient permits, else fails
462 dl 1.6 */
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     /**
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();
566     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
567     s.release();
568     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
569     s.release();
570     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
571     s.release();
572     assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
573     s.release();
574     assertEquals(1, s.availablePermits());
575     } catch( InterruptedException e){
576     unexpectedException();
577     }
578     }
579    
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     /**
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 dl 1.10 s.acquire();
622 dl 1.11 s.release(2);
623 dl 1.10 s.acquire();
624 dl 1.6 } catch(InterruptedException ie){
625     threadUnexpectedException();
626     }
627     }
628     });
629     try {
630     t.start();
631     Thread.sleep(SHORT_DELAY_MS);
632 dl 1.11 s.release(2);
633     s.acquire(2);
634     s.release(1);
635 dl 1.6 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 {
664     s.release();
665     s.release();
666     s.release();
667     s.release();
668     s.release();
669     t.join();
670     } catch( InterruptedException e){
671     unexpectedException();
672     }
673     }
674    
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     threadShouldThrow();
714     } catch(InterruptedException success){}
715     }
716     });
717     t.start();
718     try {
719     Thread.sleep(SHORT_DELAY_MS);
720     t.interrupt();
721     t.join();
722     } catch(InterruptedException e){
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     /**
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     threadShouldThrow();
760     } catch(InterruptedException success){
761     }
762     }
763     });
764     t.start();
765     try {
766     Thread.sleep(SHORT_DELAY_MS);
767     t.interrupt();
768     t.join();
769     } catch(InterruptedException e){
770     unexpectedException();
771     }
772     }
773    
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 dl 1.2 try {
835     l.acquire();
836     l.release();
837     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
838     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
839     out.writeObject(l);
840     out.close();
841    
842     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
843     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
844     Semaphore r = (Semaphore) in.readObject();
845     assertEquals(3, r.availablePermits());
846 dl 1.6 assertTrue(r.isFair());
847 dl 1.2 r.acquire();
848     r.release();
849     } catch(Exception e){
850 dl 1.4 unexpectedException();
851 dl 1.2 }
852     }
853 dl 1.6
854 dl 1.2
855 dl 1.1 }