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 |
|
10 |
import junit.framework.*; |
11 |
import java.util.*; |
12 |
import java.util.concurrent.*; |
13 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
import java.util.concurrent.locks.*; |
15 |
import java.io.*; |
16 |
|
17 |
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
public static void main(String[] args) { |
19 |
junit.textui.TestRunner.run(suite()); |
20 |
} |
21 |
public static Test suite() { |
22 |
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
23 |
} |
24 |
|
25 |
/** |
26 |
* A simple mutex class, adapted from the |
27 |
* AbstractQueuedLongSynchronizer javadoc. Exclusive acquire tests |
28 |
* exercise this as a sample user extension. Other |
29 |
* methods/features of AbstractQueuedLongSynchronizerTest are tested |
30 |
* via other test classes, including those for ReentrantLock, |
31 |
* ReentrantReadWriteLock, and Semaphore |
32 |
*/ |
33 |
static class Mutex extends AbstractQueuedLongSynchronizer { |
34 |
// Use value > 32 bits for locked state |
35 |
static final long LOCKED = 1 << 48; |
36 |
public boolean isHeldExclusively() { |
37 |
return getState() == LOCKED; |
38 |
} |
39 |
|
40 |
public boolean tryAcquire(long acquires) { |
41 |
return compareAndSetState(0, LOCKED); |
42 |
} |
43 |
|
44 |
public boolean tryRelease(long releases) { |
45 |
if (getState() == 0) throw new IllegalMonitorStateException(); |
46 |
setState(0); |
47 |
return true; |
48 |
} |
49 |
|
50 |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
} |
53 |
|
54 |
} |
55 |
|
56 |
|
57 |
/** |
58 |
* A simple latch class, to test shared mode. |
59 |
*/ |
60 |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
61 |
public boolean isSignalled() { return getState() != 0; } |
62 |
|
63 |
public long tryAcquireShared(long ignore) { |
64 |
return isSignalled() ? 1 : -1; |
65 |
} |
66 |
|
67 |
public boolean tryReleaseShared(long ignore) { |
68 |
setState(1 << 62); |
69 |
return true; |
70 |
} |
71 |
} |
72 |
|
73 |
/** |
74 |
* A runnable calling acquireInterruptibly that does not expect to |
75 |
* be interrupted. |
76 |
*/ |
77 |
class InterruptibleSyncRunnable extends CheckedRunnable { |
78 |
final Mutex sync; |
79 |
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
80 |
public void realRun() throws InterruptedException { |
81 |
sync.acquireInterruptibly(1); |
82 |
} |
83 |
} |
84 |
|
85 |
|
86 |
/** |
87 |
* A runnable calling acquireInterruptibly that expects to be |
88 |
* interrupted. |
89 |
*/ |
90 |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
91 |
final Mutex sync; |
92 |
InterruptedSyncRunnable(Mutex l) { sync = l; } |
93 |
public void realRun() throws InterruptedException { |
94 |
sync.acquireInterruptibly(1); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* isHeldExclusively is false upon construction |
100 |
*/ |
101 |
public void testIsHeldExclusively() { |
102 |
Mutex rl = new Mutex(); |
103 |
assertFalse(rl.isHeldExclusively()); |
104 |
} |
105 |
|
106 |
/** |
107 |
* acquiring released sync succeeds |
108 |
*/ |
109 |
public void testAcquire() { |
110 |
Mutex rl = new Mutex(); |
111 |
rl.acquire(1); |
112 |
assertTrue(rl.isHeldExclusively()); |
113 |
rl.release(1); |
114 |
assertFalse(rl.isHeldExclusively()); |
115 |
} |
116 |
|
117 |
/** |
118 |
* tryAcquire on an released sync succeeds |
119 |
*/ |
120 |
public void testTryAcquire() { |
121 |
Mutex rl = new Mutex(); |
122 |
assertTrue(rl.tryAcquire(1)); |
123 |
assertTrue(rl.isHeldExclusively()); |
124 |
rl.release(1); |
125 |
} |
126 |
|
127 |
/** |
128 |
* hasQueuedThreads reports whether there are waiting threads |
129 |
*/ |
130 |
public void testhasQueuedThreads() throws InterruptedException { |
131 |
final Mutex sync = new Mutex(); |
132 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
133 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
134 |
assertFalse(sync.hasQueuedThreads()); |
135 |
sync.acquire(1); |
136 |
t1.start(); |
137 |
Thread.sleep(SHORT_DELAY_MS); |
138 |
assertTrue(sync.hasQueuedThreads()); |
139 |
t2.start(); |
140 |
Thread.sleep(SHORT_DELAY_MS); |
141 |
assertTrue(sync.hasQueuedThreads()); |
142 |
t1.interrupt(); |
143 |
Thread.sleep(SHORT_DELAY_MS); |
144 |
assertTrue(sync.hasQueuedThreads()); |
145 |
sync.release(1); |
146 |
Thread.sleep(SHORT_DELAY_MS); |
147 |
assertFalse(sync.hasQueuedThreads()); |
148 |
t1.join(); |
149 |
t2.join(); |
150 |
} |
151 |
|
152 |
/** |
153 |
* isQueued(null) throws NPE |
154 |
*/ |
155 |
public void testIsQueuedNPE() { |
156 |
final Mutex sync = new Mutex(); |
157 |
try { |
158 |
sync.isQueued(null); |
159 |
shouldThrow(); |
160 |
} catch (NullPointerException success) {} |
161 |
} |
162 |
|
163 |
/** |
164 |
* isQueued reports whether a thread is queued. |
165 |
*/ |
166 |
public void testIsQueued() throws InterruptedException { |
167 |
final Mutex sync = new Mutex(); |
168 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
169 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
170 |
assertFalse(sync.isQueued(t1)); |
171 |
assertFalse(sync.isQueued(t2)); |
172 |
sync.acquire(1); |
173 |
t1.start(); |
174 |
Thread.sleep(SHORT_DELAY_MS); |
175 |
assertTrue(sync.isQueued(t1)); |
176 |
t2.start(); |
177 |
Thread.sleep(SHORT_DELAY_MS); |
178 |
assertTrue(sync.isQueued(t1)); |
179 |
assertTrue(sync.isQueued(t2)); |
180 |
t1.interrupt(); |
181 |
Thread.sleep(SHORT_DELAY_MS); |
182 |
assertFalse(sync.isQueued(t1)); |
183 |
assertTrue(sync.isQueued(t2)); |
184 |
sync.release(1); |
185 |
Thread.sleep(SHORT_DELAY_MS); |
186 |
assertFalse(sync.isQueued(t1)); |
187 |
Thread.sleep(SHORT_DELAY_MS); |
188 |
assertFalse(sync.isQueued(t2)); |
189 |
t1.join(); |
190 |
t2.join(); |
191 |
} |
192 |
|
193 |
/** |
194 |
* getFirstQueuedThread returns first waiting thread or null if none |
195 |
*/ |
196 |
public void testGetFirstQueuedThread() throws InterruptedException { |
197 |
final Mutex sync = new Mutex(); |
198 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
199 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
200 |
assertNull(sync.getFirstQueuedThread()); |
201 |
sync.acquire(1); |
202 |
t1.start(); |
203 |
Thread.sleep(SHORT_DELAY_MS); |
204 |
assertEquals(t1, sync.getFirstQueuedThread()); |
205 |
t2.start(); |
206 |
Thread.sleep(SHORT_DELAY_MS); |
207 |
assertEquals(t1, sync.getFirstQueuedThread()); |
208 |
t1.interrupt(); |
209 |
Thread.sleep(SHORT_DELAY_MS); |
210 |
Thread.sleep(SHORT_DELAY_MS); |
211 |
assertEquals(t2, sync.getFirstQueuedThread()); |
212 |
sync.release(1); |
213 |
Thread.sleep(SHORT_DELAY_MS); |
214 |
assertNull(sync.getFirstQueuedThread()); |
215 |
t1.join(); |
216 |
t2.join(); |
217 |
} |
218 |
|
219 |
|
220 |
/** |
221 |
* hasContended reports false if no thread has ever blocked, else true |
222 |
*/ |
223 |
public void testHasContended() throws InterruptedException { |
224 |
final Mutex sync = new Mutex(); |
225 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
226 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
227 |
assertFalse(sync.hasContended()); |
228 |
sync.acquire(1); |
229 |
t1.start(); |
230 |
Thread.sleep(SHORT_DELAY_MS); |
231 |
assertTrue(sync.hasContended()); |
232 |
t2.start(); |
233 |
Thread.sleep(SHORT_DELAY_MS); |
234 |
assertTrue(sync.hasContended()); |
235 |
t1.interrupt(); |
236 |
Thread.sleep(SHORT_DELAY_MS); |
237 |
assertTrue(sync.hasContended()); |
238 |
sync.release(1); |
239 |
Thread.sleep(SHORT_DELAY_MS); |
240 |
assertTrue(sync.hasContended()); |
241 |
t1.join(); |
242 |
t2.join(); |
243 |
} |
244 |
|
245 |
/** |
246 |
* getQueuedThreads includes waiting threads |
247 |
*/ |
248 |
public void testGetQueuedThreads() throws InterruptedException { |
249 |
final Mutex sync = new Mutex(); |
250 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
251 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
assertTrue(sync.getQueuedThreads().isEmpty()); |
253 |
sync.acquire(1); |
254 |
assertTrue(sync.getQueuedThreads().isEmpty()); |
255 |
t1.start(); |
256 |
Thread.sleep(SHORT_DELAY_MS); |
257 |
assertTrue(sync.getQueuedThreads().contains(t1)); |
258 |
t2.start(); |
259 |
Thread.sleep(SHORT_DELAY_MS); |
260 |
assertTrue(sync.getQueuedThreads().contains(t1)); |
261 |
assertTrue(sync.getQueuedThreads().contains(t2)); |
262 |
t1.interrupt(); |
263 |
Thread.sleep(SHORT_DELAY_MS); |
264 |
assertFalse(sync.getQueuedThreads().contains(t1)); |
265 |
assertTrue(sync.getQueuedThreads().contains(t2)); |
266 |
sync.release(1); |
267 |
Thread.sleep(SHORT_DELAY_MS); |
268 |
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
t1.join(); |
270 |
t2.join(); |
271 |
} |
272 |
|
273 |
/** |
274 |
* getExclusiveQueuedThreads includes waiting threads |
275 |
*/ |
276 |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
277 |
final Mutex sync = new Mutex(); |
278 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
279 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
280 |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
281 |
sync.acquire(1); |
282 |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
283 |
t1.start(); |
284 |
Thread.sleep(SHORT_DELAY_MS); |
285 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
286 |
t2.start(); |
287 |
Thread.sleep(SHORT_DELAY_MS); |
288 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
289 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
290 |
t1.interrupt(); |
291 |
Thread.sleep(SHORT_DELAY_MS); |
292 |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
293 |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
294 |
sync.release(1); |
295 |
Thread.sleep(SHORT_DELAY_MS); |
296 |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
297 |
t1.join(); |
298 |
t2.join(); |
299 |
} |
300 |
|
301 |
/** |
302 |
* getSharedQueuedThreads does not include exclusively waiting threads |
303 |
*/ |
304 |
public void testGetSharedQueuedThreads() throws InterruptedException { |
305 |
final Mutex sync = new Mutex(); |
306 |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
307 |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
308 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
sync.acquire(1); |
310 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
t1.start(); |
312 |
Thread.sleep(SHORT_DELAY_MS); |
313 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
314 |
t2.start(); |
315 |
Thread.sleep(SHORT_DELAY_MS); |
316 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
317 |
t1.interrupt(); |
318 |
Thread.sleep(SHORT_DELAY_MS); |
319 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
320 |
sync.release(1); |
321 |
Thread.sleep(SHORT_DELAY_MS); |
322 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
323 |
t1.join(); |
324 |
t2.join(); |
325 |
} |
326 |
|
327 |
/** |
328 |
* tryAcquireNanos is interruptible. |
329 |
*/ |
330 |
public void testInterruptedException2() throws InterruptedException { |
331 |
final Mutex sync = new Mutex(); |
332 |
sync.acquire(1); |
333 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
public void realRun() throws InterruptedException { |
335 |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
}}); |
337 |
|
338 |
t.start(); |
339 |
Thread.sleep(SHORT_DELAY_MS); |
340 |
t.interrupt(); |
341 |
t.join(); |
342 |
} |
343 |
|
344 |
|
345 |
/** |
346 |
* TryAcquire on exclusively held sync fails |
347 |
*/ |
348 |
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
final Mutex sync = new Mutex(); |
350 |
sync.acquire(1); |
351 |
Thread t = new Thread(new CheckedRunnable() { |
352 |
public void realRun() { |
353 |
assertFalse(sync.tryAcquire(1)); |
354 |
}}); |
355 |
|
356 |
t.start(); |
357 |
t.join(); |
358 |
sync.release(1); |
359 |
} |
360 |
|
361 |
/** |
362 |
* tryAcquireNanos on an exclusively held sync times out |
363 |
*/ |
364 |
public void testAcquireNanos_Timeout() throws InterruptedException { |
365 |
final Mutex sync = new Mutex(); |
366 |
sync.acquire(1); |
367 |
Thread t = new Thread(new CheckedRunnable() { |
368 |
public void realRun() throws InterruptedException { |
369 |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
}}); |
372 |
|
373 |
t.start(); |
374 |
t.join(); |
375 |
sync.release(1); |
376 |
} |
377 |
|
378 |
|
379 |
/** |
380 |
* getState is true when acquired and false when not |
381 |
*/ |
382 |
public void testGetState() throws InterruptedException { |
383 |
final Mutex sync = new Mutex(); |
384 |
sync.acquire(1); |
385 |
assertTrue(sync.isHeldExclusively()); |
386 |
sync.release(1); |
387 |
assertFalse(sync.isHeldExclusively()); |
388 |
Thread t = new Thread(new CheckedRunnable() { |
389 |
public void realRun() throws InterruptedException { |
390 |
sync.acquire(1); |
391 |
Thread.sleep(SMALL_DELAY_MS); |
392 |
sync.release(1); |
393 |
}}); |
394 |
|
395 |
t.start(); |
396 |
Thread.sleep(SHORT_DELAY_MS); |
397 |
assertTrue(sync.isHeldExclusively()); |
398 |
t.join(); |
399 |
assertFalse(sync.isHeldExclusively()); |
400 |
} |
401 |
|
402 |
|
403 |
/** |
404 |
* acquireInterruptibly is interruptible. |
405 |
*/ |
406 |
public void testAcquireInterruptibly1() throws InterruptedException { |
407 |
final Mutex sync = new Mutex(); |
408 |
sync.acquire(1); |
409 |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
t.start(); |
411 |
Thread.sleep(SHORT_DELAY_MS); |
412 |
t.interrupt(); |
413 |
Thread.sleep(SHORT_DELAY_MS); |
414 |
sync.release(1); |
415 |
t.join(); |
416 |
} |
417 |
|
418 |
/** |
419 |
* acquireInterruptibly succeeds when released, else is interruptible |
420 |
*/ |
421 |
public void testAcquireInterruptibly2() throws InterruptedException { |
422 |
final Mutex sync = new Mutex(); |
423 |
sync.acquireInterruptibly(1); |
424 |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
t.start(); |
426 |
Thread.sleep(SHORT_DELAY_MS); |
427 |
t.interrupt(); |
428 |
assertTrue(sync.isHeldExclusively()); |
429 |
t.join(); |
430 |
} |
431 |
|
432 |
/** |
433 |
* owns is true for a condition created by sync else false |
434 |
*/ |
435 |
public void testOwns() { |
436 |
final Mutex sync = new Mutex(); |
437 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
438 |
final Mutex sync2 = new Mutex(); |
439 |
assertTrue(sync.owns(c)); |
440 |
assertFalse(sync2.owns(c)); |
441 |
} |
442 |
|
443 |
/** |
444 |
* Calling await without holding sync throws IllegalMonitorStateException |
445 |
*/ |
446 |
public void testAwait_IllegalMonitor() throws InterruptedException { |
447 |
final Mutex sync = new Mutex(); |
448 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
449 |
try { |
450 |
c.await(); |
451 |
shouldThrow(); |
452 |
} catch (IllegalMonitorStateException success) {} |
453 |
} |
454 |
|
455 |
/** |
456 |
* Calling signal without holding sync throws IllegalMonitorStateException |
457 |
*/ |
458 |
public void testSignal_IllegalMonitor() throws InterruptedException { |
459 |
final Mutex sync = new Mutex(); |
460 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
try { |
462 |
c.signal(); |
463 |
shouldThrow(); |
464 |
} catch (IllegalMonitorStateException success) {} |
465 |
} |
466 |
|
467 |
/** |
468 |
* awaitNanos without a signal times out |
469 |
*/ |
470 |
public void testAwaitNanos_Timeout() throws InterruptedException { |
471 |
final Mutex sync = new Mutex(); |
472 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
473 |
sync.acquire(1); |
474 |
long t = c.awaitNanos(100); |
475 |
assertTrue(t <= 0); |
476 |
sync.release(1); |
477 |
} |
478 |
|
479 |
/** |
480 |
* Timed await without a signal times out |
481 |
*/ |
482 |
public void testAwait_Timeout() throws InterruptedException { |
483 |
final Mutex sync = new Mutex(); |
484 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
sync.acquire(1); |
486 |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
sync.release(1); |
488 |
} |
489 |
|
490 |
/** |
491 |
* awaitUntil without a signal times out |
492 |
*/ |
493 |
public void testAwaitUntil_Timeout() throws InterruptedException { |
494 |
final Mutex sync = new Mutex(); |
495 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
496 |
sync.acquire(1); |
497 |
java.util.Date d = new java.util.Date(); |
498 |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
499 |
sync.release(1); |
500 |
} |
501 |
|
502 |
/** |
503 |
* await returns when signalled |
504 |
*/ |
505 |
public void testAwait() throws InterruptedException { |
506 |
final Mutex sync = new Mutex(); |
507 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
Thread t = new Thread(new CheckedRunnable() { |
509 |
public void realRun() throws InterruptedException { |
510 |
sync.acquire(1); |
511 |
c.await(); |
512 |
sync.release(1); |
513 |
}}); |
514 |
|
515 |
t.start(); |
516 |
Thread.sleep(SHORT_DELAY_MS); |
517 |
sync.acquire(1); |
518 |
c.signal(); |
519 |
sync.release(1); |
520 |
t.join(SHORT_DELAY_MS); |
521 |
assertFalse(t.isAlive()); |
522 |
} |
523 |
|
524 |
|
525 |
|
526 |
/** |
527 |
* hasWaiters throws NPE if null |
528 |
*/ |
529 |
public void testHasWaitersNPE() { |
530 |
final Mutex sync = new Mutex(); |
531 |
try { |
532 |
sync.hasWaiters(null); |
533 |
shouldThrow(); |
534 |
} catch (NullPointerException success) {} |
535 |
} |
536 |
|
537 |
/** |
538 |
* getWaitQueueLength throws NPE if null |
539 |
*/ |
540 |
public void testGetWaitQueueLengthNPE() { |
541 |
final Mutex sync = new Mutex(); |
542 |
try { |
543 |
sync.getWaitQueueLength(null); |
544 |
shouldThrow(); |
545 |
} catch (NullPointerException success) {} |
546 |
} |
547 |
|
548 |
|
549 |
/** |
550 |
* getWaitingThreads throws NPE if null |
551 |
*/ |
552 |
public void testGetWaitingThreadsNPE() { |
553 |
final Mutex sync = new Mutex(); |
554 |
try { |
555 |
sync.getWaitingThreads(null); |
556 |
shouldThrow(); |
557 |
} catch (NullPointerException success) {} |
558 |
} |
559 |
|
560 |
|
561 |
/** |
562 |
* hasWaiters throws IAE if not owned |
563 |
*/ |
564 |
public void testHasWaitersIAE() { |
565 |
final Mutex sync = new Mutex(); |
566 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
567 |
final Mutex sync2 = new Mutex(); |
568 |
try { |
569 |
sync2.hasWaiters(c); |
570 |
shouldThrow(); |
571 |
} catch (IllegalArgumentException success) {} |
572 |
} |
573 |
|
574 |
/** |
575 |
* hasWaiters throws IMSE if not synced |
576 |
*/ |
577 |
public void testHasWaitersIMSE() { |
578 |
final Mutex sync = new Mutex(); |
579 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
580 |
try { |
581 |
sync.hasWaiters(c); |
582 |
shouldThrow(); |
583 |
} catch (IllegalMonitorStateException success) {} |
584 |
} |
585 |
|
586 |
|
587 |
/** |
588 |
* getWaitQueueLength throws IAE if not owned |
589 |
*/ |
590 |
public void testGetWaitQueueLengthIAE() { |
591 |
final Mutex sync = new Mutex(); |
592 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
593 |
final Mutex sync2 = new Mutex(); |
594 |
try { |
595 |
sync2.getWaitQueueLength(c); |
596 |
shouldThrow(); |
597 |
} catch (IllegalArgumentException success) {} |
598 |
} |
599 |
|
600 |
/** |
601 |
* getWaitQueueLength throws IMSE if not synced |
602 |
*/ |
603 |
public void testGetWaitQueueLengthIMSE() { |
604 |
final Mutex sync = new Mutex(); |
605 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
606 |
try { |
607 |
sync.getWaitQueueLength(c); |
608 |
shouldThrow(); |
609 |
} catch (IllegalMonitorStateException success) {} |
610 |
} |
611 |
|
612 |
|
613 |
/** |
614 |
* getWaitingThreads throws IAE if not owned |
615 |
*/ |
616 |
public void testGetWaitingThreadsIAE() { |
617 |
final Mutex sync = new Mutex(); |
618 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
619 |
final Mutex sync2 = new Mutex(); |
620 |
try { |
621 |
sync2.getWaitingThreads(c); |
622 |
shouldThrow(); |
623 |
} catch (IllegalArgumentException success) {} |
624 |
} |
625 |
|
626 |
/** |
627 |
* getWaitingThreads throws IMSE if not synced |
628 |
*/ |
629 |
public void testGetWaitingThreadsIMSE() { |
630 |
final Mutex sync = new Mutex(); |
631 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
632 |
try { |
633 |
sync.getWaitingThreads(c); |
634 |
shouldThrow(); |
635 |
} catch (IllegalMonitorStateException success) {} |
636 |
} |
637 |
|
638 |
|
639 |
|
640 |
/** |
641 |
* hasWaiters returns true when a thread is waiting, else false |
642 |
*/ |
643 |
public void testHasWaiters() throws InterruptedException { |
644 |
final Mutex sync = new Mutex(); |
645 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
646 |
Thread t = new Thread(new CheckedRunnable() { |
647 |
public void realRun() throws InterruptedException { |
648 |
sync.acquire(1); |
649 |
assertFalse(sync.hasWaiters(c)); |
650 |
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
c.await(); |
652 |
sync.release(1); |
653 |
}}); |
654 |
|
655 |
t.start(); |
656 |
Thread.sleep(SHORT_DELAY_MS); |
657 |
sync.acquire(1); |
658 |
assertTrue(sync.hasWaiters(c)); |
659 |
assertEquals(1, sync.getWaitQueueLength(c)); |
660 |
c.signal(); |
661 |
sync.release(1); |
662 |
Thread.sleep(SHORT_DELAY_MS); |
663 |
sync.acquire(1); |
664 |
assertFalse(sync.hasWaiters(c)); |
665 |
assertEquals(0, sync.getWaitQueueLength(c)); |
666 |
sync.release(1); |
667 |
t.join(SHORT_DELAY_MS); |
668 |
assertFalse(t.isAlive()); |
669 |
} |
670 |
|
671 |
/** |
672 |
* getWaitQueueLength returns number of waiting threads |
673 |
*/ |
674 |
public void testGetWaitQueueLength() throws InterruptedException { |
675 |
final Mutex sync = new Mutex(); |
676 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
677 |
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
public void realRun() throws InterruptedException { |
679 |
sync.acquire(1); |
680 |
assertFalse(sync.hasWaiters(c)); |
681 |
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
c.await(); |
683 |
sync.release(1); |
684 |
}}); |
685 |
|
686 |
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
public void realRun() throws InterruptedException { |
688 |
sync.acquire(1); |
689 |
assertTrue(sync.hasWaiters(c)); |
690 |
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
c.await(); |
692 |
sync.release(1); |
693 |
}}); |
694 |
|
695 |
t1.start(); |
696 |
Thread.sleep(SHORT_DELAY_MS); |
697 |
t2.start(); |
698 |
Thread.sleep(SHORT_DELAY_MS); |
699 |
sync.acquire(1); |
700 |
assertTrue(sync.hasWaiters(c)); |
701 |
assertEquals(2, sync.getWaitQueueLength(c)); |
702 |
c.signalAll(); |
703 |
sync.release(1); |
704 |
Thread.sleep(SHORT_DELAY_MS); |
705 |
sync.acquire(1); |
706 |
assertFalse(sync.hasWaiters(c)); |
707 |
assertEquals(0, sync.getWaitQueueLength(c)); |
708 |
sync.release(1); |
709 |
t1.join(SHORT_DELAY_MS); |
710 |
t2.join(SHORT_DELAY_MS); |
711 |
assertFalse(t1.isAlive()); |
712 |
assertFalse(t2.isAlive()); |
713 |
} |
714 |
|
715 |
/** |
716 |
* getWaitingThreads returns only and all waiting threads |
717 |
*/ |
718 |
public void testGetWaitingThreads() throws InterruptedException { |
719 |
final Mutex sync = new Mutex(); |
720 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
721 |
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
public void realRun() throws InterruptedException { |
723 |
sync.acquire(1); |
724 |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
c.await(); |
726 |
sync.release(1); |
727 |
}}); |
728 |
|
729 |
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
public void realRun() throws InterruptedException { |
731 |
sync.acquire(1); |
732 |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
c.await(); |
734 |
sync.release(1); |
735 |
}}); |
736 |
|
737 |
sync.acquire(1); |
738 |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
739 |
sync.release(1); |
740 |
t1.start(); |
741 |
Thread.sleep(SHORT_DELAY_MS); |
742 |
t2.start(); |
743 |
Thread.sleep(SHORT_DELAY_MS); |
744 |
sync.acquire(1); |
745 |
assertTrue(sync.hasWaiters(c)); |
746 |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
747 |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
748 |
c.signalAll(); |
749 |
sync.release(1); |
750 |
Thread.sleep(SHORT_DELAY_MS); |
751 |
sync.acquire(1); |
752 |
assertFalse(sync.hasWaiters(c)); |
753 |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
754 |
sync.release(1); |
755 |
t1.join(SHORT_DELAY_MS); |
756 |
t2.join(SHORT_DELAY_MS); |
757 |
assertFalse(t1.isAlive()); |
758 |
assertFalse(t2.isAlive()); |
759 |
} |
760 |
|
761 |
|
762 |
|
763 |
/** |
764 |
* awaitUninterruptibly doesn't abort on interrupt |
765 |
*/ |
766 |
public void testAwaitUninterruptibly() throws InterruptedException { |
767 |
final Mutex sync = new Mutex(); |
768 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
Thread t = new Thread(new CheckedRunnable() { |
770 |
public void realRun() { |
771 |
sync.acquire(1); |
772 |
c.awaitUninterruptibly(); |
773 |
sync.release(1); |
774 |
}}); |
775 |
|
776 |
t.start(); |
777 |
Thread.sleep(SHORT_DELAY_MS); |
778 |
t.interrupt(); |
779 |
sync.acquire(1); |
780 |
c.signal(); |
781 |
sync.release(1); |
782 |
t.join(SHORT_DELAY_MS); |
783 |
assertFalse(t.isAlive()); |
784 |
} |
785 |
|
786 |
/** |
787 |
* await is interruptible |
788 |
*/ |
789 |
public void testAwait_Interrupt() throws InterruptedException { |
790 |
final Mutex sync = new Mutex(); |
791 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
792 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
public void realRun() throws InterruptedException { |
794 |
sync.acquire(1); |
795 |
c.await(); |
796 |
}}); |
797 |
|
798 |
t.start(); |
799 |
Thread.sleep(SHORT_DELAY_MS); |
800 |
t.interrupt(); |
801 |
t.join(SHORT_DELAY_MS); |
802 |
assertFalse(t.isAlive()); |
803 |
} |
804 |
|
805 |
/** |
806 |
* awaitNanos is interruptible |
807 |
*/ |
808 |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
809 |
final Mutex sync = new Mutex(); |
810 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
public void realRun() throws InterruptedException { |
813 |
sync.acquire(1); |
814 |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
}}); |
816 |
|
817 |
t.start(); |
818 |
Thread.sleep(SHORT_DELAY_MS); |
819 |
t.interrupt(); |
820 |
t.join(SHORT_DELAY_MS); |
821 |
assertFalse(t.isAlive()); |
822 |
} |
823 |
|
824 |
/** |
825 |
* awaitUntil is interruptible |
826 |
*/ |
827 |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
828 |
final Mutex sync = new Mutex(); |
829 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
830 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
public void realRun() throws InterruptedException { |
832 |
sync.acquire(1); |
833 |
java.util.Date d = new java.util.Date(); |
834 |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
835 |
}}); |
836 |
|
837 |
t.start(); |
838 |
Thread.sleep(SHORT_DELAY_MS); |
839 |
t.interrupt(); |
840 |
t.join(SHORT_DELAY_MS); |
841 |
assertFalse(t.isAlive()); |
842 |
} |
843 |
|
844 |
/** |
845 |
* signalAll wakes up all threads |
846 |
*/ |
847 |
public void testSignalAll() throws InterruptedException { |
848 |
final Mutex sync = new Mutex(); |
849 |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
850 |
Thread t1 = new Thread(new CheckedRunnable() { |
851 |
public void realRun() throws InterruptedException { |
852 |
sync.acquire(1); |
853 |
c.await(); |
854 |
sync.release(1); |
855 |
}}); |
856 |
|
857 |
Thread t2 = new Thread(new CheckedRunnable() { |
858 |
public void realRun() throws InterruptedException { |
859 |
sync.acquire(1); |
860 |
c.await(); |
861 |
sync.release(1); |
862 |
}}); |
863 |
|
864 |
t1.start(); |
865 |
t2.start(); |
866 |
Thread.sleep(SHORT_DELAY_MS); |
867 |
sync.acquire(1); |
868 |
c.signalAll(); |
869 |
sync.release(1); |
870 |
t1.join(SHORT_DELAY_MS); |
871 |
t2.join(SHORT_DELAY_MS); |
872 |
assertFalse(t1.isAlive()); |
873 |
assertFalse(t2.isAlive()); |
874 |
} |
875 |
|
876 |
|
877 |
/** |
878 |
* toString indicates current state |
879 |
*/ |
880 |
public void testToString() { |
881 |
Mutex sync = new Mutex(); |
882 |
String us = sync.toString(); |
883 |
assertTrue(us.indexOf("State = 0") >= 0); |
884 |
sync.acquire(1); |
885 |
String ls = sync.toString(); |
886 |
assertTrue(ls.indexOf("State = " + Mutex.LOCKED) >= 0); |
887 |
} |
888 |
|
889 |
/** |
890 |
* A serialized AQS deserializes with current state |
891 |
*/ |
892 |
public void testSerialization() throws Exception { |
893 |
Mutex l = new Mutex(); |
894 |
l.acquire(1); |
895 |
assertTrue(l.isHeldExclusively()); |
896 |
|
897 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
898 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
899 |
out.writeObject(l); |
900 |
out.close(); |
901 |
|
902 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
903 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
904 |
Mutex r = (Mutex) in.readObject(); |
905 |
assertTrue(r.isHeldExclusively()); |
906 |
} |
907 |
|
908 |
|
909 |
/** |
910 |
* tryReleaseShared setting state changes getState |
911 |
*/ |
912 |
public void testGetStateWithReleaseShared() { |
913 |
final BooleanLatch l = new BooleanLatch(); |
914 |
assertFalse(l.isSignalled()); |
915 |
l.releaseShared(0); |
916 |
assertTrue(l.isSignalled()); |
917 |
} |
918 |
|
919 |
/** |
920 |
* releaseShared has no effect when already signalled |
921 |
*/ |
922 |
public void testReleaseShared() { |
923 |
final BooleanLatch l = new BooleanLatch(); |
924 |
assertFalse(l.isSignalled()); |
925 |
l.releaseShared(0); |
926 |
assertTrue(l.isSignalled()); |
927 |
l.releaseShared(0); |
928 |
assertTrue(l.isSignalled()); |
929 |
} |
930 |
|
931 |
/** |
932 |
* acquireSharedInterruptibly returns after release, but not before |
933 |
*/ |
934 |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
935 |
final BooleanLatch l = new BooleanLatch(); |
936 |
|
937 |
Thread t = new Thread(new CheckedRunnable() { |
938 |
public void realRun() throws InterruptedException { |
939 |
assertFalse(l.isSignalled()); |
940 |
l.acquireSharedInterruptibly(0); |
941 |
assertTrue(l.isSignalled()); |
942 |
}}); |
943 |
|
944 |
t.start(); |
945 |
assertFalse(l.isSignalled()); |
946 |
Thread.sleep(SHORT_DELAY_MS); |
947 |
l.releaseShared(0); |
948 |
assertTrue(l.isSignalled()); |
949 |
t.join(); |
950 |
} |
951 |
|
952 |
|
953 |
/** |
954 |
* acquireSharedTimed returns after release |
955 |
*/ |
956 |
public void testAcquireSharedTimed() throws InterruptedException { |
957 |
final BooleanLatch l = new BooleanLatch(); |
958 |
|
959 |
Thread t = new Thread(new CheckedRunnable() { |
960 |
public void realRun() throws InterruptedException { |
961 |
assertFalse(l.isSignalled()); |
962 |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
assertTrue(l.isSignalled()); |
965 |
}}); |
966 |
|
967 |
t.start(); |
968 |
assertFalse(l.isSignalled()); |
969 |
Thread.sleep(SHORT_DELAY_MS); |
970 |
l.releaseShared(0); |
971 |
assertTrue(l.isSignalled()); |
972 |
t.join(); |
973 |
} |
974 |
|
975 |
/** |
976 |
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
*/ |
978 |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
throws InterruptedException { |
980 |
final BooleanLatch l = new BooleanLatch(); |
981 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
public void realRun() throws InterruptedException { |
983 |
assertFalse(l.isSignalled()); |
984 |
l.acquireSharedInterruptibly(0); |
985 |
}}); |
986 |
|
987 |
t.start(); |
988 |
assertFalse(l.isSignalled()); |
989 |
t.interrupt(); |
990 |
t.join(); |
991 |
} |
992 |
|
993 |
/** |
994 |
* acquireSharedTimed throws IE if interrupted before released |
995 |
*/ |
996 |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
997 |
final BooleanLatch l = new BooleanLatch(); |
998 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
public void realRun() throws InterruptedException { |
1000 |
assertFalse(l.isSignalled()); |
1001 |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
l.tryAcquireSharedNanos(0, nanos); |
1003 |
}}); |
1004 |
|
1005 |
t.start(); |
1006 |
Thread.sleep(SHORT_DELAY_MS); |
1007 |
assertFalse(l.isSignalled()); |
1008 |
t.interrupt(); |
1009 |
t.join(); |
1010 |
} |
1011 |
|
1012 |
/** |
1013 |
* acquireSharedTimed times out if not released before timeout |
1014 |
*/ |
1015 |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1016 |
final BooleanLatch l = new BooleanLatch(); |
1017 |
Thread t = new Thread(new CheckedRunnable() { |
1018 |
public void realRun() throws InterruptedException { |
1019 |
assertFalse(l.isSignalled()); |
1020 |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
}}); |
1023 |
|
1024 |
t.start(); |
1025 |
Thread.sleep(SHORT_DELAY_MS); |
1026 |
assertFalse(l.isSignalled()); |
1027 |
t.join(); |
1028 |
} |
1029 |
|
1030 |
} |