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.27 by dl, Fri May 6 11:22:07 2011 UTC vs.
Revision 1.28 by jsr166, Tue May 24 23:43:14 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines