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