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.7 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.19 by jsr166, Tue Nov 17 02:22:50 2009 UTC

# Line 2 | Line 2
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.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class SemaphoreTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20          return new TestSuite(SemaphoreTest.class);
# Line 24 | Line 25 | public class SemaphoreTest extends JSR16
25       */
26      static class PublicSemaphore extends Semaphore {
27          PublicSemaphore(int p, boolean f) { super(p, f); }
28 <        public Collection<Thread> getQueuedThreads() {
29 <            return super.getQueuedThreads();
28 >        public Collection<Thread> getQueuedThreads() {
29 >            return super.getQueuedThreads();
30          }
31 <        public void reducePermits(int p) {
31 >        public void reducePermits(int p) {
32              super.reducePermits(p);
33          }
34      }
# Line 35 | Line 36 | public class SemaphoreTest extends JSR16
36      /**
37       * A runnable calling acquire
38       */
39 <    class InterruptibleLockRunnable implements Runnable {
39 >    class InterruptibleLockRunnable extends CheckedRunnable {
40          final Semaphore lock;
41          InterruptibleLockRunnable(Semaphore l) { lock = l; }
42 <        public void run() {
42 >        public void realRun() {
43              try {
44                  lock.acquire();
45 <            } catch(InterruptedException success){}
45 >            }
46 >            catch (InterruptedException ignored) {}
47          }
48      }
49  
# Line 50 | Line 52 | public class SemaphoreTest extends JSR16
52       * A runnable calling acquire that expects to be
53       * interrupted
54       */
55 <    class InterruptedLockRunnable implements Runnable {
55 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
56          final Semaphore lock;
57          InterruptedLockRunnable(Semaphore l) { lock = l; }
58 <        public void run() {
59 <            try {
58 <                lock.acquire();
59 <                threadShouldThrow();
60 <            } catch(InterruptedException success){}
58 >        public void realRun() throws InterruptedException {
59 >            lock.acquire();
60          }
61      }
62  
# Line 65 | Line 64 | public class SemaphoreTest extends JSR16
64       * Zero, negative, and positive initial values are allowed in constructor
65       */
66      public void testConstructor() {
67 <        Semaphore s0 = new Semaphore(0, false);
68 <        assertEquals(0, s0.availablePermits());
69 <        assertFalse(s0.isFair());
70 <        Semaphore s1 = new Semaphore(-1, false);
71 <        assertEquals(-1, s1.availablePermits());
72 <        Semaphore s2 = new Semaphore(-1, false);
73 <        assertEquals(-1, s2.availablePermits());
67 >        for (int permits : new int[] { -1, 0, 1 }) {
68 >            for (boolean fair : new boolean[] { false, true }) {
69 >                Semaphore s = new Semaphore(permits, fair);
70 >                assertEquals(permits, s.availablePermits());
71 >                assertEquals(fair, s.isFair());
72 >            }
73 >        }
74 >    }
75 >
76 >    /**
77 >     * Constructor without fairness argument behaves as nonfair
78 >     */
79 >    public void testConstructor2() {
80 >        for (int permits : new int[] { -1, 0, 1 }) {
81 >            Semaphore s = new Semaphore(permits);
82 >            assertEquals(permits, s.availablePermits());
83 >            assertFalse(s.isFair());
84 >        }
85      }
86  
87      /**
88 <     * tryAcquire succeeds when sufficent permits, else fails
88 >     * tryAcquire succeeds when sufficient permits, else fails
89       */
90      public void testTryAcquireInSameThread() {
91          Semaphore s = new Semaphore(2, false);
# Line 89 | Line 99 | public class SemaphoreTest extends JSR16
99      /**
100       * Acquire and release of semaphore succeed if initially available
101       */
102 <    public void testAcquireReleaseInSameThread() {
102 >    public void testAcquireReleaseInSameThread()
103 >        throws InterruptedException {
104          Semaphore s = new Semaphore(1, false);
105 <        try {
106 <            s.acquire();
107 <            s.release();
108 <            s.acquire();
109 <            s.release();
110 <            s.acquire();
111 <            s.release();
112 <            s.acquire();
113 <            s.release();
114 <            s.acquire();
115 <            s.release();
105 <            assertEquals(1, s.availablePermits());
106 <        } catch( InterruptedException e){
107 <            unexpectedException();
108 <        }
105 >        s.acquire();
106 >        s.release();
107 >        s.acquire();
108 >        s.release();
109 >        s.acquire();
110 >        s.release();
111 >        s.acquire();
112 >        s.release();
113 >        s.acquire();
114 >        s.release();
115 >        assertEquals(1, s.availablePermits());
116      }
117  
118      /**
119       * Uninterruptible acquire and release of semaphore succeed if
120       * initially available
121       */
122 <    public void testAcquireUninterruptiblyReleaseInSameThread() {
122 >    public void testAcquireUninterruptiblyReleaseInSameThread()
123 >        throws InterruptedException {
124          Semaphore s = new Semaphore(1, false);
125 <        try {
126 <            s.acquireUninterruptibly();
127 <            s.release();
128 <            s.acquireUninterruptibly();
129 <            s.release();
130 <            s.acquireUninterruptibly();
131 <            s.release();
132 <            s.acquireUninterruptibly();
133 <            s.release();
134 <            s.acquireUninterruptibly();
135 <            s.release();
128 <            assertEquals(1, s.availablePermits());
129 <        } finally {
130 <        }
125 >        s.acquireUninterruptibly();
126 >        s.release();
127 >        s.acquireUninterruptibly();
128 >        s.release();
129 >        s.acquireUninterruptibly();
130 >        s.release();
131 >        s.acquireUninterruptibly();
132 >        s.release();
133 >        s.acquireUninterruptibly();
134 >        s.release();
135 >        assertEquals(1, s.availablePermits());
136      }
137  
138      /**
139       * Timed Acquire and release of semaphore succeed if
140       * initially available
141       */
142 <    public void testTimedAcquireReleaseInSameThread() {
142 >    public void testTimedAcquireReleaseInSameThread()
143 >        throws InterruptedException {
144          Semaphore s = new Semaphore(1, false);
145 <        try {
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 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
151 <            s.release();
152 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
153 <            s.release();
154 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
155 <            s.release();
150 <            assertEquals(1, s.availablePermits());
151 <        } catch( InterruptedException e){
152 <            unexpectedException();
153 <        }
145 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
146 >        s.release();
147 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
148 >        s.release();
149 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
150 >        s.release();
151 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
152 >        s.release();
153 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
154 >        s.release();
155 >        assertEquals(1, s.availablePermits());
156      }
157  
158      /**
159       * A release in one thread enables an acquire in another thread
160       */
161 <    public void testAcquireReleaseInDifferentThreads() {
161 >    public void testAcquireReleaseInDifferentThreads()
162 >        throws InterruptedException {
163          final Semaphore s = new Semaphore(0, false);
164 <        Thread t = new Thread(new Runnable() {
165 <                public void run() {
166 <                    try {
167 <                        s.acquire();
168 <                        s.release();
169 <                        s.release();
170 <                        s.acquire();
171 <                    } catch(InterruptedException ie){
172 <                        threadUnexpectedException();
173 <                    }
174 <                }
175 <            });
176 <        try {
177 <            t.start();
178 <            Thread.sleep(SHORT_DELAY_MS);
179 <            s.release();
177 <            s.release();
178 <            s.acquire();
179 <            s.acquire();
180 <            s.release();
181 <            t.join();
182 <        } catch( InterruptedException e){
183 <            unexpectedException();
184 <        }
164 >        Thread t = new Thread(new CheckedRunnable() {
165 >            public void realRun() throws InterruptedException {
166 >                s.acquire();
167 >                s.release();
168 >                s.release();
169 >                s.acquire();
170 >            }});
171 >
172 >        t.start();
173 >        Thread.sleep(SHORT_DELAY_MS);
174 >        s.release();
175 >        s.release();
176 >        s.acquire();
177 >        s.acquire();
178 >        s.release();
179 >        t.join();
180      }
181  
182      /**
183       * A release in one thread enables an uninterruptible acquire in another thread
184       */
185 <    public void testUninterruptibleAcquireReleaseInDifferentThreads() {
185 >    public void testUninterruptibleAcquireReleaseInDifferentThreads()
186 >        throws InterruptedException {
187          final Semaphore s = new Semaphore(0, false);
188 <        Thread t = new Thread(new Runnable() {
189 <                public void run() {
190 <                    s.acquireUninterruptibly();
191 <                    s.release();
192 <                    s.release();
193 <                    s.acquireUninterruptibly();
194 <                }
195 <            });
196 <        try {
197 <            t.start();
198 <            Thread.sleep(SHORT_DELAY_MS);
199 <            s.release();
200 <            s.release();
201 <            s.acquireUninterruptibly();
202 <            s.acquireUninterruptibly();
203 <            s.release();
208 <            t.join();
209 <        } catch( InterruptedException e){
210 <            unexpectedException();
211 <        }
188 >        Thread t = new Thread(new CheckedRunnable() {
189 >            public void realRun() throws InterruptedException {
190 >                s.acquireUninterruptibly();
191 >                s.release();
192 >                s.release();
193 >                s.acquireUninterruptibly();
194 >            }});
195 >
196 >        t.start();
197 >        Thread.sleep(SHORT_DELAY_MS);
198 >        s.release();
199 >        s.release();
200 >        s.acquireUninterruptibly();
201 >        s.acquireUninterruptibly();
202 >        s.release();
203 >        t.join();
204      }
205  
206  
207      /**
208       *  A release in one thread enables a timed acquire in another thread
209       */
210 <    public void testTimedAcquireReleaseInDifferentThreads() {
210 >    public void testTimedAcquireReleaseInDifferentThreads()
211 >        throws InterruptedException {
212          final Semaphore s = new Semaphore(1, false);
213 <        Thread t = new Thread(new Runnable() {
214 <                public void run() {
215 <                    try {
216 <                        s.release();
217 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
218 <                        s.release();
219 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
220 <
221 <                    } catch(InterruptedException ie){
222 <                        threadUnexpectedException();
223 <                    }
224 <                }
225 <            });
226 <        try {
227 <            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 <        }
213 >        Thread t = new Thread(new CheckedRunnable() {
214 >            public void realRun() throws InterruptedException {
215 >                s.release();
216 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
217 >                s.release();
218 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
219 >            }});
220 >
221 >        t.start();
222 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
223 >        s.release();
224 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
225 >        s.release();
226 >        s.release();
227 >        t.join();
228      }
229  
230      /**
231       * A waiting acquire blocks interruptibly
232       */
233 <    public void testAcquire_InterruptedException() {
233 >    public void testAcquire_InterruptedException()
234 >        throws InterruptedException {
235          final Semaphore s = new Semaphore(0, false);
236 <        Thread t = new Thread(new Runnable() {
237 <                public void run() {
238 <                    try {
239 <                        s.acquire();
240 <                        threadShouldThrow();
256 <                    } catch(InterruptedException success){}
257 <                }
258 <            });
236 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
237 >            public void realRun() throws InterruptedException {
238 >                s.acquire();
239 >            }});
240 >
241          t.start();
242 <        try {
243 <            Thread.sleep(SHORT_DELAY_MS);
244 <            t.interrupt();
263 <            t.join();
264 <        } catch(InterruptedException e){
265 <            unexpectedException();
266 <        }
242 >        Thread.sleep(SHORT_DELAY_MS);
243 >        t.interrupt();
244 >        t.join();
245      }
246 <    
246 >
247      /**
248       *  A waiting timed acquire blocks interruptibly
249       */
250 <    public void testTryAcquire_InterruptedException() {
250 >    public void testTryAcquire_InterruptedException()
251 >        throws InterruptedException {
252          final Semaphore s = new Semaphore(0, false);
253 <        Thread t = new Thread(new Runnable() {
254 <                public void run() {
255 <                    try {
256 <                        s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
257 <                        threadShouldThrow();
279 <                    } catch(InterruptedException success){
280 <                    }
281 <                }
282 <            });
253 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
254 >            public void realRun() throws InterruptedException {
255 >                s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS);
256 >            }});
257 >
258          t.start();
259 <        try {
260 <            Thread.sleep(SHORT_DELAY_MS);
261 <            t.interrupt();
262 <            t.join();
263 <        } catch(InterruptedException e){
264 <            unexpectedException();
265 <        }
259 >        Thread.sleep(SHORT_DELAY_MS);
260 >        t.interrupt();
261 >        t.join();
262 >    }
263 >
264 >    /**
265 >     * hasQueuedThreads reports whether there are waiting threads
266 >     */
267 >    public void testHasQueuedThreads() throws InterruptedException {
268 >        final Semaphore lock = new Semaphore(1, false);
269 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
270 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
271 >        assertFalse(lock.hasQueuedThreads());
272 >        lock.acquireUninterruptibly();
273 >        t1.start();
274 >        Thread.sleep(SHORT_DELAY_MS);
275 >        assertTrue(lock.hasQueuedThreads());
276 >        t2.start();
277 >        Thread.sleep(SHORT_DELAY_MS);
278 >        assertTrue(lock.hasQueuedThreads());
279 >        t1.interrupt();
280 >        Thread.sleep(SHORT_DELAY_MS);
281 >        assertTrue(lock.hasQueuedThreads());
282 >        lock.release();
283 >        Thread.sleep(SHORT_DELAY_MS);
284 >        assertFalse(lock.hasQueuedThreads());
285 >        t1.join();
286 >        t2.join();
287      }
288  
289      /**
290       * getQueueLength reports number of waiting threads
291       */
292 <    public void testGetQueueLength() {
292 >    public void testGetQueueLength() throws InterruptedException {
293          final Semaphore lock = new Semaphore(1, false);
294          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
295          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
296 <        try {
297 <            assertEquals(0, lock.getQueueLength());
298 <            lock.acquireUninterruptibly();
299 <            t1.start();
300 <            Thread.sleep(SHORT_DELAY_MS);
301 <            assertEquals(1, lock.getQueueLength());
302 <            t2.start();
303 <            Thread.sleep(SHORT_DELAY_MS);
304 <            assertEquals(2, lock.getQueueLength());
305 <            t1.interrupt();
306 <            Thread.sleep(SHORT_DELAY_MS);
307 <            assertEquals(1, lock.getQueueLength());
308 <            lock.release();
309 <            Thread.sleep(SHORT_DELAY_MS);
310 <            assertEquals(0, lock.getQueueLength());
311 <            t1.join();
312 <            t2.join();
317 <        } catch(Exception e){
318 <            unexpectedException();
319 <        }
320 <    }
296 >        assertEquals(0, lock.getQueueLength());
297 >        lock.acquireUninterruptibly();
298 >        t1.start();
299 >        Thread.sleep(SHORT_DELAY_MS);
300 >        assertEquals(1, lock.getQueueLength());
301 >        t2.start();
302 >        Thread.sleep(SHORT_DELAY_MS);
303 >        assertEquals(2, lock.getQueueLength());
304 >        t1.interrupt();
305 >        Thread.sleep(SHORT_DELAY_MS);
306 >        assertEquals(1, lock.getQueueLength());
307 >        lock.release();
308 >        Thread.sleep(SHORT_DELAY_MS);
309 >        assertEquals(0, lock.getQueueLength());
310 >        t1.join();
311 >        t2.join();
312 >    }
313  
314      /**
315       * getQueuedThreads includes waiting threads
316       */
317 <    public void testGetQueuedThreads() {
317 >    public void testGetQueuedThreads() throws InterruptedException {
318          final PublicSemaphore lock = new PublicSemaphore(1, false);
319          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
320          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
321 <        try {
322 <            assertTrue(lock.getQueuedThreads().isEmpty());
323 <            lock.acquireUninterruptibly();
324 <            assertTrue(lock.getQueuedThreads().isEmpty());
325 <            t1.start();
326 <            Thread.sleep(SHORT_DELAY_MS);
327 <            assertTrue(lock.getQueuedThreads().contains(t1));
328 <            t2.start();
329 <            Thread.sleep(SHORT_DELAY_MS);
330 <            assertTrue(lock.getQueuedThreads().contains(t1));
331 <            assertTrue(lock.getQueuedThreads().contains(t2));
332 <            t1.interrupt();
333 <            Thread.sleep(SHORT_DELAY_MS);
334 <            assertFalse(lock.getQueuedThreads().contains(t1));
335 <            assertTrue(lock.getQueuedThreads().contains(t2));
336 <            lock.release();
337 <            Thread.sleep(SHORT_DELAY_MS);
338 <            assertTrue(lock.getQueuedThreads().isEmpty());
339 <            t1.join();
340 <            t2.join();
349 <        } catch(Exception e){
350 <            unexpectedException();
351 <        }
352 <    }
321 >        assertTrue(lock.getQueuedThreads().isEmpty());
322 >        lock.acquireUninterruptibly();
323 >        assertTrue(lock.getQueuedThreads().isEmpty());
324 >        t1.start();
325 >        Thread.sleep(SHORT_DELAY_MS);
326 >        assertTrue(lock.getQueuedThreads().contains(t1));
327 >        t2.start();
328 >        Thread.sleep(SHORT_DELAY_MS);
329 >        assertTrue(lock.getQueuedThreads().contains(t1));
330 >        assertTrue(lock.getQueuedThreads().contains(t2));
331 >        t1.interrupt();
332 >        Thread.sleep(SHORT_DELAY_MS);
333 >        assertFalse(lock.getQueuedThreads().contains(t1));
334 >        assertTrue(lock.getQueuedThreads().contains(t2));
335 >        lock.release();
336 >        Thread.sleep(SHORT_DELAY_MS);
337 >        assertTrue(lock.getQueuedThreads().isEmpty());
338 >        t1.join();
339 >        t2.join();
340 >    }
341  
342 +    /**
343 +     * drainPermits reports and removes given number of permits
344 +     */
345 +    public void testDrainPermits() {
346 +        Semaphore s = new Semaphore(0, false);
347 +        assertEquals(0, s.availablePermits());
348 +        assertEquals(0, s.drainPermits());
349 +        s.release(10);
350 +        assertEquals(10, s.availablePermits());
351 +        assertEquals(10, s.drainPermits());
352 +        assertEquals(0, s.availablePermits());
353 +        assertEquals(0, s.drainPermits());
354 +    }
355  
356      /**
357       * reducePermits reduces number of permits
# Line 367 | Line 368 | public class SemaphoreTest extends JSR16
368      /**
369       * a deserialized serialized semaphore has same number of permits
370       */
371 <    public void testSerialization() {
371 >    public void testSerialization() throws Exception {
372          Semaphore l = new Semaphore(3, false);
373 <        try {
374 <            l.acquire();
375 <            l.release();
376 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
377 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
378 <            out.writeObject(l);
379 <            out.close();
380 <
381 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
382 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
383 <            Semaphore r = (Semaphore) in.readObject();
384 <            assertEquals(3, r.availablePermits());
385 <            assertFalse(r.isFair());
386 <            r.acquire();
386 <            r.release();
387 <        } catch(Exception e){
388 <            unexpectedException();
389 <        }
373 >        l.acquire();
374 >        l.release();
375 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
376 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
377 >        out.writeObject(l);
378 >        out.close();
379 >
380 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
381 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
382 >        Semaphore r = (Semaphore) in.readObject();
383 >        assertEquals(3, r.availablePermits());
384 >        assertFalse(r.isFair());
385 >        r.acquire();
386 >        r.release();
387      }
388  
389  
# Line 404 | Line 401 | public class SemaphoreTest extends JSR16
401      }
402  
403      /**
404 <     * tryAcquire succeeds when sufficent permits, else fails
404 >     * tryAcquire succeeds when sufficient permits, else fails
405       */
406      public void testTryAcquireInSameThread_fair() {
407          Semaphore s = new Semaphore(2, true);
# Line 416 | Line 413 | public class SemaphoreTest extends JSR16
413      }
414  
415      /**
416 <     * tryAcquire(n) succeeds when sufficent permits, else fails
416 >     * tryAcquire(n) succeeds when sufficient permits, else fails
417       */
418      public void testTryAcquireNInSameThread_fair() {
419          Semaphore s = new Semaphore(2, true);
# Line 429 | Line 426 | public class SemaphoreTest extends JSR16
426      /**
427       * Acquire and release of semaphore succeed if initially available
428       */
429 <    public void testAcquireReleaseInSameThread_fair() {
429 >    public void testAcquireReleaseInSameThread_fair()
430 >        throws InterruptedException {
431          Semaphore s = new Semaphore(1, true);
432 <        try {
433 <            s.acquire();
434 <            s.release();
435 <            s.acquire();
436 <            s.release();
437 <            s.acquire();
438 <            s.release();
439 <            s.acquire();
440 <            s.release();
441 <            s.acquire();
442 <            s.release();
445 <            assertEquals(1, s.availablePermits());
446 <        } catch( InterruptedException e){
447 <            unexpectedException();
448 <        }
432 >        s.acquire();
433 >        s.release();
434 >        s.acquire();
435 >        s.release();
436 >        s.acquire();
437 >        s.release();
438 >        s.acquire();
439 >        s.release();
440 >        s.acquire();
441 >        s.release();
442 >        assertEquals(1, s.availablePermits());
443      }
444  
445      /**
446       * Acquire(n) and release(n) of semaphore succeed if initially available
447       */
448 <    public void testAcquireReleaseNInSameThread_fair() {
448 >    public void testAcquireReleaseNInSameThread_fair()
449 >        throws InterruptedException {
450          Semaphore s = new Semaphore(1, true);
451 <        try {
452 <            s.release(1);
453 <            s.acquire(1);
454 <            s.release(2);
455 <            s.acquire(2);
456 <            s.release(3);
457 <            s.acquire(3);
458 <            s.release(4);
459 <            s.acquire(4);
460 <            s.release(5);
461 <            s.acquire(5);
467 <            assertEquals(1, s.availablePermits());
468 <        } catch( InterruptedException e){
469 <            unexpectedException();
470 <        }
451 >        s.release(1);
452 >        s.acquire(1);
453 >        s.release(2);
454 >        s.acquire(2);
455 >        s.release(3);
456 >        s.acquire(3);
457 >        s.release(4);
458 >        s.acquire(4);
459 >        s.release(5);
460 >        s.acquire(5);
461 >        assertEquals(1, s.availablePermits());
462      }
463  
464      /**
# Line 475 | Line 466 | public class SemaphoreTest extends JSR16
466       */
467      public void testAcquireUninterruptiblyReleaseNInSameThread_fair() {
468          Semaphore s = new Semaphore(1, true);
469 <        try {
470 <            s.release(1);
471 <            s.acquireUninterruptibly(1);
472 <            s.release(2);
473 <            s.acquireUninterruptibly(2);
474 <            s.release(3);
475 <            s.acquireUninterruptibly(3);
476 <            s.release(4);
477 <            s.acquireUninterruptibly(4);
478 <            s.release(5);
479 <            s.acquireUninterruptibly(5);
489 <            assertEquals(1, s.availablePermits());
490 <        } finally {
491 <        }
469 >        s.release(1);
470 >        s.acquireUninterruptibly(1);
471 >        s.release(2);
472 >        s.acquireUninterruptibly(2);
473 >        s.release(3);
474 >        s.acquireUninterruptibly(3);
475 >        s.release(4);
476 >        s.acquireUninterruptibly(4);
477 >        s.release(5);
478 >        s.acquireUninterruptibly(5);
479 >        assertEquals(1, s.availablePermits());
480      }
481  
482      /**
483       * release(n) in one thread enables timed acquire(n) in another thread
484       */
485 <    public void testTimedAcquireReleaseNInSameThread_fair() {
485 >    public void testTimedAcquireReleaseNInSameThread_fair()
486 >        throws InterruptedException {
487          Semaphore s = new Semaphore(1, true);
488 <        try {
489 <            s.release(1);
490 <            assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
491 <            s.release(2);
492 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
493 <            s.release(3);
494 <            assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
495 <            s.release(4);
496 <            assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
497 <            s.release(5);
498 <            assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510 <            assertEquals(1, s.availablePermits());
511 <        } catch( InterruptedException e){
512 <            unexpectedException();
513 <        }
488 >        s.release(1);
489 >        assertTrue(s.tryAcquire(1, SHORT_DELAY_MS, MILLISECONDS));
490 >        s.release(2);
491 >        assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
492 >        s.release(3);
493 >        assertTrue(s.tryAcquire(3, SHORT_DELAY_MS, MILLISECONDS));
494 >        s.release(4);
495 >        assertTrue(s.tryAcquire(4, SHORT_DELAY_MS, MILLISECONDS));
496 >        s.release(5);
497 >        assertTrue(s.tryAcquire(5, SHORT_DELAY_MS, MILLISECONDS));
498 >        assertEquals(1, s.availablePermits());
499      }
500  
501      /**
502       * release in one thread enables timed acquire in another thread
503       */
504 <    public void testTimedAcquireReleaseInSameThread_fair() {
504 >    public void testTimedAcquireReleaseInSameThread_fair()
505 >        throws InterruptedException {
506          Semaphore s = new Semaphore(1, true);
507 <        try {
508 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
509 <            s.release();
510 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
511 <            s.release();
512 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
513 <            s.release();
514 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
515 <            s.release();
516 <            assertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
517 <            s.release();
532 <            assertEquals(1, s.availablePermits());
533 <        } catch( InterruptedException e){
534 <            unexpectedException();
535 <        }
507 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
508 >        s.release();
509 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
510 >        s.release();
511 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
512 >        s.release();
513 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
514 >        s.release();
515 >        assertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
516 >        s.release();
517 >        assertEquals(1, s.availablePermits());
518      }
519  
520      /**
521       * A release in one thread enables an acquire in another thread
522       */
523 <    public void testAcquireReleaseInDifferentThreads_fair() {
523 >    public void testAcquireReleaseInDifferentThreads_fair()
524 >        throws InterruptedException {
525          final Semaphore s = new Semaphore(0, true);
526 <        Thread t = new Thread(new Runnable() {
527 <                public void run() {
528 <                    try {
529 <                        s.acquire();
530 <                        s.acquire();
531 <                        s.acquire();
532 <                        s.acquire();
533 <                    } catch(InterruptedException ie){
534 <                        threadUnexpectedException();
535 <                    }
536 <                }
537 <            });
538 <        try {
539 <            t.start();
540 <            Thread.sleep(SHORT_DELAY_MS);
541 <            s.release();
542 <            s.release();
543 <            s.release();
561 <            s.release();
562 <            s.release();
563 <            s.release();
564 <            t.join();
565 <            assertEquals(2, s.availablePermits());
566 <        } catch( InterruptedException e){
567 <            unexpectedException();
568 <        }
526 >        Thread t = new Thread(new CheckedRunnable() {
527 >            public void realRun() throws InterruptedException {
528 >                s.acquire();
529 >                s.acquire();
530 >                s.acquire();
531 >                s.acquire();
532 >            }});
533 >
534 >        t.start();
535 >        Thread.sleep(SHORT_DELAY_MS);
536 >        s.release();
537 >        s.release();
538 >        s.release();
539 >        s.release();
540 >        s.release();
541 >        s.release();
542 >        t.join();
543 >        assertEquals(2, s.availablePermits());
544      }
545  
546      /**
547       * release(n) in one thread enables acquire(n) in another thread
548       */
549 <    public void testAcquireReleaseNInDifferentThreads_fair() {
549 >    public void testAcquireReleaseNInDifferentThreads_fair()
550 >        throws InterruptedException {
551          final Semaphore s = new Semaphore(0, true);
552 <        Thread t = new Thread(new Runnable() {
553 <                public void run() {
554 <                    try {
555 <                        s.acquire(2);
556 <                        s.acquire(2);
557 <                        s.release(4);
558 <                    } catch(InterruptedException ie){
559 <                        threadUnexpectedException();
560 <                    }
561 <                }
562 <            });
563 <        try {
564 <            t.start();
589 <            Thread.sleep(SHORT_DELAY_MS);
590 <            s.release(6);
591 <            s.acquire(2);
592 <            s.acquire(2);
593 <            s.release(2);
594 <            t.join();
595 <        } catch( InterruptedException e){
596 <            unexpectedException();
597 <        }
552 >        Thread t = new Thread(new CheckedRunnable() {
553 >            public void realRun() throws InterruptedException {
554 >                s.acquire();
555 >                s.release(2);
556 >                s.acquire();
557 >            }});
558 >
559 >        t.start();
560 >        Thread.sleep(SHORT_DELAY_MS);
561 >        s.release(2);
562 >        s.acquire(2);
563 >        s.release(1);
564 >        t.join();
565      }
566  
567 +    /**
568 +     * release(n) in one thread enables acquire(n) in another thread
569 +     */
570 +    public void testAcquireReleaseNInDifferentThreads_fair2()
571 +        throws InterruptedException {
572 +        final Semaphore s = new Semaphore(0, true);
573 +        Thread t = new Thread(new CheckedRunnable() {
574 +            public void realRun() throws InterruptedException {
575 +                s.acquire(2);
576 +                s.acquire(2);
577 +                s.release(4);
578 +            }});
579 +
580 +        t.start();
581 +        Thread.sleep(SHORT_DELAY_MS);
582 +        s.release(6);
583 +        s.acquire(2);
584 +        s.acquire(2);
585 +        s.release(2);
586 +        t.join();
587 +    }
588  
589  
590      /**
591       * release in one thread enables timed acquire in another thread
592       */
593 <    public void testTimedAcquireReleaseInDifferentThreads_fair() {
593 >    public void testTimedAcquireReleaseInDifferentThreads_fair()
594 >        throws InterruptedException {
595          final Semaphore s = new Semaphore(1, true);
596 <        Thread t = new Thread(new Runnable() {
597 <                public void run() {
598 <                    try {
599 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
600 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
601 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
602 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
603 <                        threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
604 <
616 <                    } catch(InterruptedException ie){
617 <                        threadUnexpectedException();
618 <                    }
619 <                }
620 <            });
596 >        Thread t = new Thread(new CheckedRunnable() {
597 >            public void realRun() throws InterruptedException {
598 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
599 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
600 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
601 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
602 >                threadAssertTrue(s.tryAcquire(SHORT_DELAY_MS, MILLISECONDS));
603 >            }});
604 >
605          t.start();
606 <        try {
607 <            s.release();
608 <            s.release();
609 <            s.release();
610 <            s.release();
611 <            s.release();
628 <            t.join();
629 <        } catch( InterruptedException e){
630 <            unexpectedException();
631 <        }
606 >        s.release();
607 >        s.release();
608 >        s.release();
609 >        s.release();
610 >        s.release();
611 >        t.join();
612      }
613  
614      /**
615       * release(n) in one thread enables timed acquire(n) in another thread
616       */
617 <    public void testTimedAcquireReleaseNInDifferentThreads_fair() {
617 >    public void testTimedAcquireReleaseNInDifferentThreads_fair()
618 >        throws InterruptedException {
619          final Semaphore s = new Semaphore(2, true);
620 <        Thread t = new Thread(new Runnable() {
621 <                public void run() {
622 <                    try {
623 <                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
624 <                        s.release(2);
625 <                        threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
626 <                        s.release(2);
627 <                    } catch(InterruptedException ie){
647 <                        threadUnexpectedException();
648 <                    }
649 <                }
650 <            });
620 >        Thread t = new Thread(new CheckedRunnable() {
621 >            public void realRun() throws InterruptedException {
622 >                threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
623 >                s.release(2);
624 >                threadAssertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
625 >                s.release(2);
626 >            }});
627 >
628          t.start();
629 <        try {
630 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
631 <            s.release(2);
632 <            assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
633 <            s.release(2);
657 <            t.join();
658 <        } catch( InterruptedException e){
659 <            unexpectedException();
660 <        }
629 >        assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
630 >        s.release(2);
631 >        assertTrue(s.tryAcquire(2, SHORT_DELAY_MS, MILLISECONDS));
632 >        s.release(2);
633 >        t.join();
634      }
635  
636      /**
637       * A waiting acquire blocks interruptibly
638       */
639 <    public void testAcquire_InterruptedException_fair() {
639 >    public void testAcquire_InterruptedException_fair()
640 >        throws InterruptedException {
641          final Semaphore s = new Semaphore(0, true);
642 <        Thread t = new Thread(new Runnable() {
643 <                public void run() {
644 <                    try {
645 <                        s.acquire();
646 <                        threadShouldThrow();
673 <                    } catch(InterruptedException success){}
674 <                }
675 <            });
642 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
643 >            public void realRun() throws InterruptedException {
644 >                s.acquire();
645 >            }});
646 >
647          t.start();
648 <        try {
649 <            Thread.sleep(SHORT_DELAY_MS);
650 <            t.interrupt();
680 <            t.join();
681 <        } catch(InterruptedException e){
682 <            unexpectedException();
683 <        }
648 >        Thread.sleep(SHORT_DELAY_MS);
649 >        t.interrupt();
650 >        t.join();
651      }
652  
653      /**
654       * A waiting acquire(n) blocks interruptibly
655       */
656 <    public void testAcquireN_InterruptedException_fair() {
656 >    public void testAcquireN_InterruptedException_fair()
657 >        throws InterruptedException {
658          final Semaphore s = new Semaphore(2, true);
659 <        Thread t = new Thread(new Runnable() {
660 <                public void run() {
661 <                    try {
662 <                        s.acquire(3);
663 <                        threadShouldThrow();
696 <                    } catch(InterruptedException success){}
697 <                }
698 <            });
659 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
660 >            public void realRun() throws InterruptedException {
661 >                s.acquire(3);
662 >            }});
663 >
664          t.start();
665 <        try {
666 <            Thread.sleep(SHORT_DELAY_MS);
667 <            t.interrupt();
703 <            t.join();
704 <        } catch(InterruptedException e){
705 <            unexpectedException();
706 <        }
665 >        Thread.sleep(SHORT_DELAY_MS);
666 >        t.interrupt();
667 >        t.join();
668      }
669 <    
669 >
670      /**
671       *  A waiting tryAcquire blocks interruptibly
672       */
673 <    public void testTryAcquire_InterruptedException_fair() {
673 >    public void testTryAcquire_InterruptedException_fair()
674 >        throws InterruptedException {
675          final Semaphore s = new Semaphore(0, true);
676 <        Thread t = new Thread(new Runnable() {
677 <                public void run() {
678 <                    try {
679 <                        s.tryAcquire(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
680 <                        threadShouldThrow();
719 <                    } catch(InterruptedException success){
720 <                    }
721 <                }
722 <            });
676 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
677 >            public void realRun() throws InterruptedException {
678 >                s.tryAcquire(MEDIUM_DELAY_MS, MILLISECONDS);
679 >            }});
680 >
681          t.start();
682 <        try {
683 <            Thread.sleep(SHORT_DELAY_MS);
684 <            t.interrupt();
727 <            t.join();
728 <        } catch(InterruptedException e){
729 <            unexpectedException();
730 <        }
682 >        Thread.sleep(SHORT_DELAY_MS);
683 >        t.interrupt();
684 >        t.join();
685      }
686  
687      /**
688       *  A waiting tryAcquire(n) blocks interruptibly
689       */
690 <    public void testTryAcquireN_InterruptedException_fair() {
690 >    public void testTryAcquireN_InterruptedException_fair()
691 >        throws InterruptedException {
692          final Semaphore s = new Semaphore(1, true);
693 <        Thread t = new Thread(new Runnable() {
694 <                public void run() {
695 <                    try {
696 <                        s.tryAcquire(4, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
697 <                        threadShouldThrow();
743 <                    } catch(InterruptedException success){
744 <                    }
745 <                }
746 <            });
693 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
694 >            public void realRun() throws InterruptedException {
695 >                s.tryAcquire(4, MEDIUM_DELAY_MS, MILLISECONDS);
696 >            }});
697 >
698          t.start();
699 <        try {
700 <            Thread.sleep(SHORT_DELAY_MS);
701 <            t.interrupt();
751 <            t.join();
752 <        } catch(InterruptedException e){
753 <            unexpectedException();
754 <        }
699 >        Thread.sleep(SHORT_DELAY_MS);
700 >        t.interrupt();
701 >        t.join();
702      }
703  
704      /**
705       * getQueueLength reports number of waiting threads
706       */
707 <    public void testGetQueueLength_fair() {
707 >    public void testGetQueueLength_fair() throws InterruptedException {
708          final Semaphore lock = new Semaphore(1, true);
709          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
710          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
711 <        try {
712 <            assertEquals(0, lock.getQueueLength());
713 <            lock.acquireUninterruptibly();
714 <            t1.start();
715 <            Thread.sleep(SHORT_DELAY_MS);
716 <            assertEquals(1, lock.getQueueLength());
717 <            t2.start();
718 <            Thread.sleep(SHORT_DELAY_MS);
719 <            assertEquals(2, lock.getQueueLength());
720 <            t1.interrupt();
721 <            Thread.sleep(SHORT_DELAY_MS);
722 <            assertEquals(1, lock.getQueueLength());
723 <            lock.release();
724 <            Thread.sleep(SHORT_DELAY_MS);
725 <            assertEquals(0, lock.getQueueLength());
726 <            t1.join();
727 <            t2.join();
781 <        } catch(Exception e){
782 <            unexpectedException();
783 <        }
784 <    }
711 >        assertEquals(0, lock.getQueueLength());
712 >        lock.acquireUninterruptibly();
713 >        t1.start();
714 >        Thread.sleep(SHORT_DELAY_MS);
715 >        assertEquals(1, lock.getQueueLength());
716 >        t2.start();
717 >        Thread.sleep(SHORT_DELAY_MS);
718 >        assertEquals(2, lock.getQueueLength());
719 >        t1.interrupt();
720 >        Thread.sleep(SHORT_DELAY_MS);
721 >        assertEquals(1, lock.getQueueLength());
722 >        lock.release();
723 >        Thread.sleep(SHORT_DELAY_MS);
724 >        assertEquals(0, lock.getQueueLength());
725 >        t1.join();
726 >        t2.join();
727 >    }
728  
729  
730      /**
731       * a deserialized serialized semaphore has same number of permits
732       */
733 <    public void testSerialization_fair() {
733 >    public void testSerialization_fair() throws Exception {
734          Semaphore l = new Semaphore(3, true);
735  
736 <        try {
737 <            l.acquire();
738 <            l.release();
739 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
740 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
741 <            out.writeObject(l);
742 <            out.close();
743 <
744 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
745 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
746 <            Semaphore r = (Semaphore) in.readObject();
747 <            assertEquals(3, r.availablePermits());
748 <            assertTrue(r.isFair());
749 <            r.acquire();
807 <            r.release();
808 <        } catch(Exception e){
809 <            unexpectedException();
810 <        }
736 >        l.acquire();
737 >        l.release();
738 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
739 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
740 >        out.writeObject(l);
741 >        out.close();
742 >
743 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
744 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
745 >        Semaphore r = (Semaphore) in.readObject();
746 >        assertEquals(3, r.availablePermits());
747 >        assertTrue(r.isFair());
748 >        r.acquire();
749 >        r.release();
750      }
751  
752 +    /**
753 +     * toString indicates current number of permits
754 +     */
755 +    public void testToString() {
756 +        Semaphore s = new Semaphore(0);
757 +        String us = s.toString();
758 +        assertTrue(us.indexOf("Permits = 0") >= 0);
759 +        s.release();
760 +        String s1 = s.toString();
761 +        assertTrue(s1.indexOf("Permits = 1") >= 0);
762 +        s.release();
763 +        String s2 = s.toString();
764 +        assertTrue(s2.indexOf("Permits = 2") >= 0);
765 +    }
766  
767   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines