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