ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java
Revision: 1.40
Committed: Sun May 14 02:24:10 2017 UTC (7 years ago) by jsr166
Branch: MAIN
Changes since 1.39: +1 -1 lines
Log Message:
improve testTryAcquireSharedNanos

File Contents

# Content
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/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 import static java.util.concurrent.TimeUnit.NANOSECONDS;
11
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
16 import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject;
17
18 import junit.framework.AssertionFailedError;
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase {
23 public static void main(String[] args) {
24 main(suite(), args);
25 }
26 public static Test suite() {
27 return new TestSuite(AbstractQueuedLongSynchronizerTest.class);
28 }
29
30 /**
31 * A simple mutex class, adapted from the class javadoc. Exclusive
32 * acquire tests exercise this as a sample user extension.
33 */
34 static class Mutex extends AbstractQueuedLongSynchronizer {
35 /** An eccentric value > 32 bits for locked synchronizer state. */
36 static final long LOCKED = (1L << 63) | (1L << 15);
37
38 static final long UNLOCKED = 0;
39
40 public boolean isHeldExclusively() {
41 long state = getState();
42 assertTrue(state == UNLOCKED || state == LOCKED);
43 return state == LOCKED;
44 }
45
46 public boolean tryAcquire(long acquires) {
47 assertEquals(LOCKED, acquires);
48 return compareAndSetState(UNLOCKED, LOCKED);
49 }
50
51 public boolean tryRelease(long releases) {
52 if (getState() != LOCKED) throw new IllegalMonitorStateException();
53 setState(UNLOCKED);
54 return true;
55 }
56
57 public boolean tryAcquireNanos(long nanos) throws InterruptedException {
58 return tryAcquireNanos(LOCKED, nanos);
59 }
60
61 public boolean tryAcquire() {
62 return tryAcquire(LOCKED);
63 }
64
65 public boolean tryRelease() {
66 return tryRelease(LOCKED);
67 }
68
69 public void acquire() {
70 acquire(LOCKED);
71 }
72
73 public void acquireInterruptibly() throws InterruptedException {
74 acquireInterruptibly(LOCKED);
75 }
76
77 public void release() {
78 release(LOCKED);
79 }
80
81 public ConditionObject newCondition() {
82 return new ConditionObject();
83 }
84 }
85
86 /**
87 * A simple latch class, to test shared mode.
88 */
89 static class BooleanLatch extends AbstractQueuedLongSynchronizer {
90 public boolean isSignalled() { return getState() != 0; }
91
92 public long tryAcquireShared(long ignore) {
93 return isSignalled() ? 1 : -1;
94 }
95
96 public boolean tryReleaseShared(long ignore) {
97 setState(1L << 62);
98 return true;
99 }
100 }
101
102 /**
103 * A runnable calling acquireInterruptibly that does not expect to
104 * be interrupted.
105 */
106 class InterruptibleSyncRunnable extends CheckedRunnable {
107 final Mutex sync;
108 InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; }
109 public void realRun() throws InterruptedException {
110 sync.acquireInterruptibly();
111 }
112 }
113
114 /**
115 * A runnable calling acquireInterruptibly that expects to be
116 * interrupted.
117 */
118 class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
119 final Mutex sync;
120 InterruptedSyncRunnable(Mutex sync) { this.sync = sync; }
121 public void realRun() throws InterruptedException {
122 sync.acquireInterruptibly();
123 }
124 }
125
126 /** A constant to clarify calls to checking methods below. */
127 static final Thread[] NO_THREADS = new Thread[0];
128
129 /**
130 * Spin-waits until sync.isQueued(t) becomes true.
131 */
132 void waitForQueuedThread(AbstractQueuedLongSynchronizer sync,
133 Thread t) {
134 long startTime = System.nanoTime();
135 while (!sync.isQueued(t)) {
136 if (millisElapsedSince(startTime) > LONG_DELAY_MS)
137 throw new AssertionFailedError("timed out");
138 Thread.yield();
139 }
140 assertTrue(t.isAlive());
141 }
142
143 /**
144 * Checks that sync has exactly the given queued threads.
145 */
146 void assertHasQueuedThreads(AbstractQueuedLongSynchronizer sync,
147 Thread... expected) {
148 Collection<Thread> actual = sync.getQueuedThreads();
149 assertEquals(expected.length > 0, sync.hasQueuedThreads());
150 assertEquals(expected.length, sync.getQueueLength());
151 assertEquals(expected.length, actual.size());
152 assertEquals(expected.length == 0, actual.isEmpty());
153 assertEquals(new HashSet<Thread>(actual),
154 new HashSet<Thread>(Arrays.asList(expected)));
155 }
156
157 /**
158 * Checks that sync has exactly the given (exclusive) queued threads.
159 */
160 void assertHasExclusiveQueuedThreads(AbstractQueuedLongSynchronizer sync,
161 Thread... expected) {
162 assertHasQueuedThreads(sync, expected);
163 assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()),
164 new HashSet<Thread>(sync.getQueuedThreads()));
165 assertEquals(0, sync.getSharedQueuedThreads().size());
166 assertTrue(sync.getSharedQueuedThreads().isEmpty());
167 }
168
169 /**
170 * Checks that sync has exactly the given (shared) queued threads.
171 */
172 void assertHasSharedQueuedThreads(AbstractQueuedLongSynchronizer sync,
173 Thread... expected) {
174 assertHasQueuedThreads(sync, expected);
175 assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()),
176 new HashSet<Thread>(sync.getQueuedThreads()));
177 assertEquals(0, sync.getExclusiveQueuedThreads().size());
178 assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
179 }
180
181 /**
182 * Checks that condition c has exactly the given waiter threads,
183 * after acquiring mutex.
184 */
185 void assertHasWaitersUnlocked(Mutex sync, ConditionObject c,
186 Thread... threads) {
187 sync.acquire();
188 assertHasWaitersLocked(sync, c, threads);
189 sync.release();
190 }
191
192 /**
193 * Checks that condition c has exactly the given waiter threads.
194 */
195 void assertHasWaitersLocked(Mutex sync, ConditionObject c,
196 Thread... threads) {
197 assertEquals(threads.length > 0, sync.hasWaiters(c));
198 assertEquals(threads.length, sync.getWaitQueueLength(c));
199 assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty());
200 assertEquals(threads.length, sync.getWaitingThreads(c).size());
201 assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)),
202 new HashSet<Thread>(Arrays.asList(threads)));
203 }
204
205 enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
206
207 /**
208 * Awaits condition using the specified AwaitMethod.
209 */
210 void await(ConditionObject c, AwaitMethod awaitMethod)
211 throws InterruptedException {
212 long timeoutMillis = 2 * LONG_DELAY_MS;
213 switch (awaitMethod) {
214 case await:
215 c.await();
216 break;
217 case awaitTimed:
218 assertTrue(c.await(timeoutMillis, MILLISECONDS));
219 break;
220 case awaitNanos:
221 long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
222 long nanosRemaining = c.awaitNanos(nanosTimeout);
223 assertTrue(nanosRemaining > 0);
224 break;
225 case awaitUntil:
226 assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
227 break;
228 default:
229 throw new AssertionError();
230 }
231 }
232
233 /**
234 * Checks that awaiting the given condition times out (using the
235 * default timeout duration).
236 */
237 void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
238 final long timeoutMillis = timeoutMillis();
239 final long startTime;
240 try {
241 switch (awaitMethod) {
242 case awaitTimed:
243 startTime = System.nanoTime();
244 assertFalse(c.await(timeoutMillis, MILLISECONDS));
245 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
246 break;
247 case awaitNanos:
248 startTime = System.nanoTime();
249 long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
250 long nanosRemaining = c.awaitNanos(nanosTimeout);
251 assertTrue(nanosRemaining <= 0);
252 assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS));
253 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
254 break;
255 case awaitUntil:
256 // We shouldn't assume that nanoTime and currentTimeMillis
257 // use the same time source, so don't use nanoTime here.
258 java.util.Date delayedDate = delayedDate(timeoutMillis);
259 assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
260 assertTrue(new java.util.Date().getTime() >= delayedDate.getTime());
261 break;
262 default:
263 throw new UnsupportedOperationException();
264 }
265 } catch (InterruptedException ie) { threadUnexpectedException(ie); }
266 }
267
268 /**
269 * isHeldExclusively is false upon construction
270 */
271 public void testIsHeldExclusively() {
272 Mutex sync = new Mutex();
273 assertFalse(sync.isHeldExclusively());
274 }
275
276 /**
277 * acquiring released sync succeeds
278 */
279 public void testAcquire() {
280 Mutex sync = new Mutex();
281 sync.acquire();
282 assertTrue(sync.isHeldExclusively());
283 sync.release();
284 assertFalse(sync.isHeldExclusively());
285 }
286
287 /**
288 * tryAcquire on a released sync succeeds
289 */
290 public void testTryAcquire() {
291 Mutex sync = new Mutex();
292 assertTrue(sync.tryAcquire());
293 assertTrue(sync.isHeldExclusively());
294 sync.release();
295 assertFalse(sync.isHeldExclusively());
296 }
297
298 /**
299 * hasQueuedThreads reports whether there are waiting threads
300 */
301 public void testHasQueuedThreads() {
302 final Mutex sync = new Mutex();
303 assertFalse(sync.hasQueuedThreads());
304 sync.acquire();
305 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
306 waitForQueuedThread(sync, t1);
307 assertTrue(sync.hasQueuedThreads());
308 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
309 waitForQueuedThread(sync, t2);
310 assertTrue(sync.hasQueuedThreads());
311 t1.interrupt();
312 awaitTermination(t1);
313 assertTrue(sync.hasQueuedThreads());
314 sync.release();
315 awaitTermination(t2);
316 assertFalse(sync.hasQueuedThreads());
317 }
318
319 /**
320 * isQueued(null) throws NullPointerException
321 */
322 public void testIsQueuedNPE() {
323 final Mutex sync = new Mutex();
324 try {
325 sync.isQueued(null);
326 shouldThrow();
327 } catch (NullPointerException success) {}
328 }
329
330 /**
331 * isQueued reports whether a thread is queued
332 */
333 public void testIsQueued() {
334 final Mutex sync = new Mutex();
335 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
336 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
337 assertFalse(sync.isQueued(t1));
338 assertFalse(sync.isQueued(t2));
339 sync.acquire();
340 t1.start();
341 waitForQueuedThread(sync, t1);
342 assertTrue(sync.isQueued(t1));
343 assertFalse(sync.isQueued(t2));
344 t2.start();
345 waitForQueuedThread(sync, t2);
346 assertTrue(sync.isQueued(t1));
347 assertTrue(sync.isQueued(t2));
348 t1.interrupt();
349 awaitTermination(t1);
350 assertFalse(sync.isQueued(t1));
351 assertTrue(sync.isQueued(t2));
352 sync.release();
353 awaitTermination(t2);
354 assertFalse(sync.isQueued(t1));
355 assertFalse(sync.isQueued(t2));
356 }
357
358 /**
359 * getFirstQueuedThread returns first waiting thread or null if none
360 */
361 public void testGetFirstQueuedThread() {
362 final Mutex sync = new Mutex();
363 assertNull(sync.getFirstQueuedThread());
364 sync.acquire();
365 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
366 waitForQueuedThread(sync, t1);
367 assertEquals(t1, sync.getFirstQueuedThread());
368 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
369 waitForQueuedThread(sync, t2);
370 assertEquals(t1, sync.getFirstQueuedThread());
371 t1.interrupt();
372 awaitTermination(t1);
373 assertEquals(t2, sync.getFirstQueuedThread());
374 sync.release();
375 awaitTermination(t2);
376 assertNull(sync.getFirstQueuedThread());
377 }
378
379 /**
380 * hasContended reports false if no thread has ever blocked, else true
381 */
382 public void testHasContended() {
383 final Mutex sync = new Mutex();
384 assertFalse(sync.hasContended());
385 sync.acquire();
386 assertFalse(sync.hasContended());
387 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
388 waitForQueuedThread(sync, t1);
389 assertTrue(sync.hasContended());
390 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
391 waitForQueuedThread(sync, t2);
392 assertTrue(sync.hasContended());
393 t1.interrupt();
394 awaitTermination(t1);
395 assertTrue(sync.hasContended());
396 sync.release();
397 awaitTermination(t2);
398 assertTrue(sync.hasContended());
399 }
400
401 /**
402 * getQueuedThreads returns all waiting threads
403 */
404 public void testGetQueuedThreads() {
405 final Mutex sync = new Mutex();
406 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
407 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
408 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
409 sync.acquire();
410 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
411 t1.start();
412 waitForQueuedThread(sync, t1);
413 assertHasExclusiveQueuedThreads(sync, t1);
414 assertTrue(sync.getQueuedThreads().contains(t1));
415 assertFalse(sync.getQueuedThreads().contains(t2));
416 t2.start();
417 waitForQueuedThread(sync, t2);
418 assertHasExclusiveQueuedThreads(sync, t1, t2);
419 assertTrue(sync.getQueuedThreads().contains(t1));
420 assertTrue(sync.getQueuedThreads().contains(t2));
421 t1.interrupt();
422 awaitTermination(t1);
423 assertHasExclusiveQueuedThreads(sync, t2);
424 sync.release();
425 awaitTermination(t2);
426 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
427 }
428
429 /**
430 * getExclusiveQueuedThreads returns all exclusive waiting threads
431 */
432 public void testGetExclusiveQueuedThreads() {
433 final Mutex sync = new Mutex();
434 Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
435 Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
436 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
437 sync.acquire();
438 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
439 t1.start();
440 waitForQueuedThread(sync, t1);
441 assertHasExclusiveQueuedThreads(sync, t1);
442 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
443 assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
444 t2.start();
445 waitForQueuedThread(sync, t2);
446 assertHasExclusiveQueuedThreads(sync, t1, t2);
447 assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
448 assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
449 t1.interrupt();
450 awaitTermination(t1);
451 assertHasExclusiveQueuedThreads(sync, t2);
452 sync.release();
453 awaitTermination(t2);
454 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
455 }
456
457 /**
458 * getSharedQueuedThreads does not include exclusively waiting threads
459 */
460 public void testGetSharedQueuedThreads_Exclusive() {
461 final Mutex sync = new Mutex();
462 assertTrue(sync.getSharedQueuedThreads().isEmpty());
463 sync.acquire();
464 assertTrue(sync.getSharedQueuedThreads().isEmpty());
465 Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
466 waitForQueuedThread(sync, t1);
467 assertTrue(sync.getSharedQueuedThreads().isEmpty());
468 Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
469 waitForQueuedThread(sync, t2);
470 assertTrue(sync.getSharedQueuedThreads().isEmpty());
471 t1.interrupt();
472 awaitTermination(t1);
473 assertTrue(sync.getSharedQueuedThreads().isEmpty());
474 sync.release();
475 awaitTermination(t2);
476 assertTrue(sync.getSharedQueuedThreads().isEmpty());
477 }
478
479 /**
480 * getSharedQueuedThreads returns all shared waiting threads
481 */
482 public void testGetSharedQueuedThreads_Shared() {
483 final BooleanLatch l = new BooleanLatch();
484 assertHasSharedQueuedThreads(l, NO_THREADS);
485 Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
486 public void realRun() throws InterruptedException {
487 l.acquireSharedInterruptibly(0);
488 }});
489 waitForQueuedThread(l, t1);
490 assertHasSharedQueuedThreads(l, t1);
491 Thread t2 = newStartedThread(new CheckedRunnable() {
492 public void realRun() throws InterruptedException {
493 l.acquireSharedInterruptibly(0);
494 }});
495 waitForQueuedThread(l, t2);
496 assertHasSharedQueuedThreads(l, t1, t2);
497 t1.interrupt();
498 awaitTermination(t1);
499 assertHasSharedQueuedThreads(l, t2);
500 assertTrue(l.releaseShared(0));
501 awaitTermination(t2);
502 assertHasSharedQueuedThreads(l, NO_THREADS);
503 }
504
505 /**
506 * tryAcquireNanos is interruptible
507 */
508 public void testTryAcquireNanos_Interruptible() {
509 final Mutex sync = new Mutex();
510 sync.acquire();
511 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
512 public void realRun() throws InterruptedException {
513 sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
514 }});
515
516 waitForQueuedThread(sync, t);
517 t.interrupt();
518 awaitTermination(t);
519 }
520
521 /**
522 * tryAcquire on exclusively held sync fails
523 */
524 public void testTryAcquireWhenSynced() {
525 final Mutex sync = new Mutex();
526 sync.acquire();
527 Thread t = newStartedThread(new CheckedRunnable() {
528 public void realRun() {
529 assertFalse(sync.tryAcquire());
530 }});
531
532 awaitTermination(t);
533 sync.release();
534 }
535
536 /**
537 * tryAcquireNanos on an exclusively held sync times out
538 */
539 public void testAcquireNanos_Timeout() {
540 final Mutex sync = new Mutex();
541 sync.acquire();
542 Thread t = newStartedThread(new CheckedRunnable() {
543 public void realRun() throws InterruptedException {
544 long startTime = System.nanoTime();
545 long nanos = MILLISECONDS.toNanos(timeoutMillis());
546 assertFalse(sync.tryAcquireNanos(nanos));
547 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
548 }});
549
550 awaitTermination(t);
551 sync.release();
552 }
553
554 /**
555 * getState is true when acquired and false when not
556 */
557 public void testGetState() {
558 final Mutex sync = new Mutex();
559 sync.acquire();
560 assertTrue(sync.isHeldExclusively());
561 sync.release();
562 assertFalse(sync.isHeldExclusively());
563
564 final BooleanLatch acquired = new BooleanLatch();
565 final BooleanLatch done = new BooleanLatch();
566 Thread t = newStartedThread(new CheckedRunnable() {
567 public void realRun() throws InterruptedException {
568 sync.acquire();
569 assertTrue(acquired.releaseShared(0));
570 done.acquireShared(0);
571 sync.release();
572 }});
573
574 acquired.acquireShared(0);
575 assertTrue(sync.isHeldExclusively());
576 assertTrue(done.releaseShared(0));
577 awaitTermination(t);
578 assertFalse(sync.isHeldExclusively());
579 }
580
581 /**
582 * acquireInterruptibly succeeds when released, else is interruptible
583 */
584 public void testAcquireInterruptibly() throws InterruptedException {
585 final Mutex sync = new Mutex();
586 final BooleanLatch threadStarted = new BooleanLatch();
587 sync.acquireInterruptibly();
588 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
589 public void realRun() throws InterruptedException {
590 assertTrue(threadStarted.releaseShared(0));
591 sync.acquireInterruptibly();
592 }});
593
594 threadStarted.acquireShared(0);
595 waitForQueuedThread(sync, t);
596 t.interrupt();
597 awaitTermination(t);
598 assertTrue(sync.isHeldExclusively());
599 }
600
601 /**
602 * owns is true for a condition created by sync else false
603 */
604 public void testOwns() {
605 final Mutex sync = new Mutex();
606 final ConditionObject c = sync.newCondition();
607 final Mutex sync2 = new Mutex();
608 assertTrue(sync.owns(c));
609 assertFalse(sync2.owns(c));
610 }
611
612 /**
613 * Calling await without holding sync throws IllegalMonitorStateException
614 */
615 public void testAwait_IMSE() {
616 final Mutex sync = new Mutex();
617 final ConditionObject c = sync.newCondition();
618 for (AwaitMethod awaitMethod : AwaitMethod.values()) {
619 long startTime = System.nanoTime();
620 try {
621 await(c, awaitMethod);
622 shouldThrow();
623 } catch (IllegalMonitorStateException success) {
624 } catch (InterruptedException e) { threadUnexpectedException(e); }
625 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
626 }
627 }
628
629 /**
630 * Calling signal without holding sync throws IllegalMonitorStateException
631 */
632 public void testSignal_IMSE() {
633 final Mutex sync = new Mutex();
634 final ConditionObject c = sync.newCondition();
635 try {
636 c.signal();
637 shouldThrow();
638 } catch (IllegalMonitorStateException success) {}
639 assertHasWaitersUnlocked(sync, c, NO_THREADS);
640 }
641
642 /**
643 * Calling signalAll without holding sync throws IllegalMonitorStateException
644 */
645 public void testSignalAll_IMSE() {
646 final Mutex sync = new Mutex();
647 final ConditionObject c = sync.newCondition();
648 try {
649 c.signalAll();
650 shouldThrow();
651 } catch (IllegalMonitorStateException success) {}
652 }
653
654 /**
655 * await/awaitNanos/awaitUntil without a signal times out
656 */
657 public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); }
658 public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); }
659 public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); }
660 public void testAwait_Timeout(AwaitMethod awaitMethod) {
661 final Mutex sync = new Mutex();
662 final ConditionObject c = sync.newCondition();
663 sync.acquire();
664 assertAwaitTimesOut(c, awaitMethod);
665 sync.release();
666 }
667
668 /**
669 * await/awaitNanos/awaitUntil returns when signalled
670 */
671 public void testSignal_await() { testSignal(AwaitMethod.await); }
672 public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); }
673 public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); }
674 public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); }
675 public void testSignal(final AwaitMethod awaitMethod) {
676 final Mutex sync = new Mutex();
677 final ConditionObject c = sync.newCondition();
678 final BooleanLatch acquired = new BooleanLatch();
679 Thread t = newStartedThread(new CheckedRunnable() {
680 public void realRun() throws InterruptedException {
681 sync.acquire();
682 assertTrue(acquired.releaseShared(0));
683 await(c, awaitMethod);
684 sync.release();
685 }});
686
687 acquired.acquireShared(0);
688 sync.acquire();
689 assertHasWaitersLocked(sync, c, t);
690 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
691 c.signal();
692 assertHasWaitersLocked(sync, c, NO_THREADS);
693 assertHasExclusiveQueuedThreads(sync, t);
694 sync.release();
695 awaitTermination(t);
696 }
697
698 /**
699 * hasWaiters(null) throws NullPointerException
700 */
701 public void testHasWaitersNPE() {
702 final Mutex sync = new Mutex();
703 try {
704 sync.hasWaiters(null);
705 shouldThrow();
706 } catch (NullPointerException success) {}
707 }
708
709 /**
710 * getWaitQueueLength(null) throws NullPointerException
711 */
712 public void testGetWaitQueueLengthNPE() {
713 final Mutex sync = new Mutex();
714 try {
715 sync.getWaitQueueLength(null);
716 shouldThrow();
717 } catch (NullPointerException success) {}
718 }
719
720 /**
721 * getWaitingThreads throws NPE if null
722 */
723 public void testGetWaitingThreadsNPE() {
724 final Mutex sync = new Mutex();
725 try {
726 sync.getWaitingThreads(null);
727 shouldThrow();
728 } catch (NullPointerException success) {}
729 }
730
731 /**
732 * hasWaiters throws IllegalArgumentException if not owned
733 */
734 public void testHasWaitersIAE() {
735 final Mutex sync = new Mutex();
736 final ConditionObject c = sync.newCondition();
737 final Mutex sync2 = new Mutex();
738 try {
739 sync2.hasWaiters(c);
740 shouldThrow();
741 } catch (IllegalArgumentException success) {}
742 assertHasWaitersUnlocked(sync, c, NO_THREADS);
743 }
744
745 /**
746 * hasWaiters throws IllegalMonitorStateException if not synced
747 */
748 public void testHasWaitersIMSE() {
749 final Mutex sync = new Mutex();
750 final ConditionObject c = sync.newCondition();
751 try {
752 sync.hasWaiters(c);
753 shouldThrow();
754 } catch (IllegalMonitorStateException success) {}
755 assertHasWaitersUnlocked(sync, c, NO_THREADS);
756 }
757
758 /**
759 * getWaitQueueLength throws IllegalArgumentException if not owned
760 */
761 public void testGetWaitQueueLengthIAE() {
762 final Mutex sync = new Mutex();
763 final ConditionObject c = sync.newCondition();
764 final Mutex sync2 = new Mutex();
765 try {
766 sync2.getWaitQueueLength(c);
767 shouldThrow();
768 } catch (IllegalArgumentException success) {}
769 assertHasWaitersUnlocked(sync, c, NO_THREADS);
770 }
771
772 /**
773 * getWaitQueueLength throws IllegalMonitorStateException if not synced
774 */
775 public void testGetWaitQueueLengthIMSE() {
776 final Mutex sync = new Mutex();
777 final ConditionObject c = sync.newCondition();
778 try {
779 sync.getWaitQueueLength(c);
780 shouldThrow();
781 } catch (IllegalMonitorStateException success) {}
782 assertHasWaitersUnlocked(sync, c, NO_THREADS);
783 }
784
785 /**
786 * getWaitingThreads throws IllegalArgumentException if not owned
787 */
788 public void testGetWaitingThreadsIAE() {
789 final Mutex sync = new Mutex();
790 final ConditionObject c = sync.newCondition();
791 final Mutex sync2 = new Mutex();
792 try {
793 sync2.getWaitingThreads(c);
794 shouldThrow();
795 } catch (IllegalArgumentException success) {}
796 assertHasWaitersUnlocked(sync, c, NO_THREADS);
797 }
798
799 /**
800 * getWaitingThreads throws IllegalMonitorStateException if not synced
801 */
802 public void testGetWaitingThreadsIMSE() {
803 final Mutex sync = new Mutex();
804 final ConditionObject c = sync.newCondition();
805 try {
806 sync.getWaitingThreads(c);
807 shouldThrow();
808 } catch (IllegalMonitorStateException success) {}
809 assertHasWaitersUnlocked(sync, c, NO_THREADS);
810 }
811
812 /**
813 * hasWaiters returns true when a thread is waiting, else false
814 */
815 public void testHasWaiters() {
816 final Mutex sync = new Mutex();
817 final ConditionObject c = sync.newCondition();
818 final BooleanLatch acquired = new BooleanLatch();
819 Thread t = newStartedThread(new CheckedRunnable() {
820 public void realRun() throws InterruptedException {
821 sync.acquire();
822 assertHasWaitersLocked(sync, c, NO_THREADS);
823 assertFalse(sync.hasWaiters(c));
824 assertTrue(acquired.releaseShared(0));
825 c.await();
826 sync.release();
827 }});
828
829 acquired.acquireShared(0);
830 sync.acquire();
831 assertHasWaitersLocked(sync, c, t);
832 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
833 assertTrue(sync.hasWaiters(c));
834 c.signal();
835 assertHasWaitersLocked(sync, c, NO_THREADS);
836 assertHasExclusiveQueuedThreads(sync, t);
837 assertFalse(sync.hasWaiters(c));
838 sync.release();
839
840 awaitTermination(t);
841 assertHasWaitersUnlocked(sync, c, NO_THREADS);
842 }
843
844 /**
845 * getWaitQueueLength returns number of waiting threads
846 */
847 public void testGetWaitQueueLength() {
848 final Mutex sync = new Mutex();
849 final ConditionObject c = sync.newCondition();
850 final BooleanLatch acquired1 = new BooleanLatch();
851 final BooleanLatch acquired2 = new BooleanLatch();
852 final Thread t1 = newStartedThread(new CheckedRunnable() {
853 public void realRun() throws InterruptedException {
854 sync.acquire();
855 assertHasWaitersLocked(sync, c, NO_THREADS);
856 assertEquals(0, sync.getWaitQueueLength(c));
857 assertTrue(acquired1.releaseShared(0));
858 c.await();
859 sync.release();
860 }});
861 acquired1.acquireShared(0);
862 sync.acquire();
863 assertHasWaitersLocked(sync, c, t1);
864 assertEquals(1, sync.getWaitQueueLength(c));
865 sync.release();
866
867 final Thread t2 = newStartedThread(new CheckedRunnable() {
868 public void realRun() throws InterruptedException {
869 sync.acquire();
870 assertHasWaitersLocked(sync, c, t1);
871 assertEquals(1, sync.getWaitQueueLength(c));
872 assertTrue(acquired2.releaseShared(0));
873 c.await();
874 sync.release();
875 }});
876 acquired2.acquireShared(0);
877 sync.acquire();
878 assertHasWaitersLocked(sync, c, t1, t2);
879 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
880 assertEquals(2, sync.getWaitQueueLength(c));
881 c.signalAll();
882 assertHasWaitersLocked(sync, c, NO_THREADS);
883 assertHasExclusiveQueuedThreads(sync, t1, t2);
884 assertEquals(0, sync.getWaitQueueLength(c));
885 sync.release();
886
887 awaitTermination(t1);
888 awaitTermination(t2);
889 assertHasWaitersUnlocked(sync, c, NO_THREADS);
890 }
891
892 /**
893 * getWaitingThreads returns only and all waiting threads
894 */
895 public void testGetWaitingThreads() {
896 final Mutex sync = new Mutex();
897 final ConditionObject c = sync.newCondition();
898 final BooleanLatch acquired1 = new BooleanLatch();
899 final BooleanLatch acquired2 = new BooleanLatch();
900 final Thread t1 = new Thread(new CheckedRunnable() {
901 public void realRun() throws InterruptedException {
902 sync.acquire();
903 assertHasWaitersLocked(sync, c, NO_THREADS);
904 assertTrue(sync.getWaitingThreads(c).isEmpty());
905 assertTrue(acquired1.releaseShared(0));
906 c.await();
907 sync.release();
908 }});
909
910 final Thread t2 = new Thread(new CheckedRunnable() {
911 public void realRun() throws InterruptedException {
912 sync.acquire();
913 assertHasWaitersLocked(sync, c, t1);
914 assertTrue(sync.getWaitingThreads(c).contains(t1));
915 assertFalse(sync.getWaitingThreads(c).isEmpty());
916 assertEquals(1, sync.getWaitingThreads(c).size());
917 assertTrue(acquired2.releaseShared(0));
918 c.await();
919 sync.release();
920 }});
921
922 sync.acquire();
923 assertHasWaitersLocked(sync, c, NO_THREADS);
924 assertFalse(sync.getWaitingThreads(c).contains(t1));
925 assertFalse(sync.getWaitingThreads(c).contains(t2));
926 assertTrue(sync.getWaitingThreads(c).isEmpty());
927 assertEquals(0, sync.getWaitingThreads(c).size());
928 sync.release();
929
930 t1.start();
931 acquired1.acquireShared(0);
932 sync.acquire();
933 assertHasWaitersLocked(sync, c, t1);
934 assertTrue(sync.getWaitingThreads(c).contains(t1));
935 assertFalse(sync.getWaitingThreads(c).contains(t2));
936 assertFalse(sync.getWaitingThreads(c).isEmpty());
937 assertEquals(1, sync.getWaitingThreads(c).size());
938 sync.release();
939
940 t2.start();
941 acquired2.acquireShared(0);
942 sync.acquire();
943 assertHasWaitersLocked(sync, c, t1, t2);
944 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
945 assertTrue(sync.getWaitingThreads(c).contains(t1));
946 assertTrue(sync.getWaitingThreads(c).contains(t2));
947 assertFalse(sync.getWaitingThreads(c).isEmpty());
948 assertEquals(2, sync.getWaitingThreads(c).size());
949 c.signalAll();
950 assertHasWaitersLocked(sync, c, NO_THREADS);
951 assertHasExclusiveQueuedThreads(sync, t1, t2);
952 assertFalse(sync.getWaitingThreads(c).contains(t1));
953 assertFalse(sync.getWaitingThreads(c).contains(t2));
954 assertTrue(sync.getWaitingThreads(c).isEmpty());
955 assertEquals(0, sync.getWaitingThreads(c).size());
956 sync.release();
957
958 awaitTermination(t1);
959 awaitTermination(t2);
960 assertHasWaitersUnlocked(sync, c, NO_THREADS);
961 }
962
963 /**
964 * awaitUninterruptibly is uninterruptible
965 */
966 public void testAwaitUninterruptibly() {
967 final Mutex sync = new Mutex();
968 final ConditionObject condition = sync.newCondition();
969 final BooleanLatch pleaseInterrupt = new BooleanLatch();
970 Thread t = newStartedThread(new CheckedRunnable() {
971 public void realRun() {
972 sync.acquire();
973 assertTrue(pleaseInterrupt.releaseShared(0));
974 condition.awaitUninterruptibly();
975 assertTrue(Thread.interrupted());
976 assertHasWaitersLocked(sync, condition, NO_THREADS);
977 sync.release();
978 }});
979
980 pleaseInterrupt.acquireShared(0);
981 sync.acquire();
982 assertHasWaitersLocked(sync, condition, t);
983 sync.release();
984 t.interrupt();
985 assertHasWaitersUnlocked(sync, condition, t);
986 assertThreadBlocks(t, Thread.State.WAITING);
987 sync.acquire();
988 assertHasWaitersLocked(sync, condition, t);
989 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
990 condition.signal();
991 assertHasWaitersLocked(sync, condition, NO_THREADS);
992 assertHasExclusiveQueuedThreads(sync, t);
993 sync.release();
994 awaitTermination(t);
995 }
996
997 /**
998 * await/awaitNanos/awaitUntil is interruptible
999 */
1000 public void testInterruptible_await() { testInterruptible(AwaitMethod.await); }
1001 public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); }
1002 public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); }
1003 public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); }
1004 public void testInterruptible(final AwaitMethod awaitMethod) {
1005 final Mutex sync = new Mutex();
1006 final ConditionObject c = sync.newCondition();
1007 final BooleanLatch pleaseInterrupt = new BooleanLatch();
1008 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1009 public void realRun() throws InterruptedException {
1010 sync.acquire();
1011 assertTrue(pleaseInterrupt.releaseShared(0));
1012 await(c, awaitMethod);
1013 }});
1014
1015 pleaseInterrupt.acquireShared(0);
1016 t.interrupt();
1017 awaitTermination(t);
1018 }
1019
1020 /**
1021 * signalAll wakes up all threads
1022 */
1023 public void testSignalAll_await() { testSignalAll(AwaitMethod.await); }
1024 public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); }
1025 public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); }
1026 public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); }
1027 public void testSignalAll(final AwaitMethod awaitMethod) {
1028 final Mutex sync = new Mutex();
1029 final ConditionObject c = sync.newCondition();
1030 final BooleanLatch acquired1 = new BooleanLatch();
1031 final BooleanLatch acquired2 = new BooleanLatch();
1032 Thread t1 = newStartedThread(new CheckedRunnable() {
1033 public void realRun() throws InterruptedException {
1034 sync.acquire();
1035 acquired1.releaseShared(0);
1036 await(c, awaitMethod);
1037 sync.release();
1038 }});
1039
1040 Thread t2 = newStartedThread(new CheckedRunnable() {
1041 public void realRun() throws InterruptedException {
1042 sync.acquire();
1043 acquired2.releaseShared(0);
1044 await(c, awaitMethod);
1045 sync.release();
1046 }});
1047
1048 acquired1.acquireShared(0);
1049 acquired2.acquireShared(0);
1050 sync.acquire();
1051 assertHasWaitersLocked(sync, c, t1, t2);
1052 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1053 c.signalAll();
1054 assertHasWaitersLocked(sync, c, NO_THREADS);
1055 assertHasExclusiveQueuedThreads(sync, t1, t2);
1056 sync.release();
1057 awaitTermination(t1);
1058 awaitTermination(t2);
1059 }
1060
1061 /**
1062 * toString indicates current state
1063 */
1064 public void testToString() {
1065 Mutex sync = new Mutex();
1066 assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1067 sync.acquire();
1068 assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
1069 }
1070
1071 /**
1072 * A serialized AQS deserializes with current state, but no queued threads
1073 */
1074 public void testSerialization() {
1075 Mutex sync = new Mutex();
1076 assertFalse(serialClone(sync).isHeldExclusively());
1077 sync.acquire();
1078 Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1079 waitForQueuedThread(sync, t);
1080 assertTrue(sync.isHeldExclusively());
1081
1082 Mutex clone = serialClone(sync);
1083 assertTrue(clone.isHeldExclusively());
1084 assertHasExclusiveQueuedThreads(sync, t);
1085 assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1086 t.interrupt();
1087 awaitTermination(t);
1088 sync.release();
1089 assertFalse(sync.isHeldExclusively());
1090 assertTrue(clone.isHeldExclusively());
1091 assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1092 assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1093 }
1094
1095 /**
1096 * tryReleaseShared setting state changes getState
1097 */
1098 public void testGetStateWithReleaseShared() {
1099 final BooleanLatch l = new BooleanLatch();
1100 assertFalse(l.isSignalled());
1101 assertTrue(l.releaseShared(0));
1102 assertTrue(l.isSignalled());
1103 }
1104
1105 /**
1106 * releaseShared has no effect when already signalled
1107 */
1108 public void testReleaseShared() {
1109 final BooleanLatch l = new BooleanLatch();
1110 assertFalse(l.isSignalled());
1111 assertTrue(l.releaseShared(0));
1112 assertTrue(l.isSignalled());
1113 assertTrue(l.releaseShared(0));
1114 assertTrue(l.isSignalled());
1115 }
1116
1117 /**
1118 * acquireSharedInterruptibly returns after release, but not before
1119 */
1120 public void testAcquireSharedInterruptibly() {
1121 final BooleanLatch l = new BooleanLatch();
1122
1123 Thread t = newStartedThread(new CheckedRunnable() {
1124 public void realRun() throws InterruptedException {
1125 assertFalse(l.isSignalled());
1126 l.acquireSharedInterruptibly(0);
1127 assertTrue(l.isSignalled());
1128 l.acquireSharedInterruptibly(0);
1129 assertTrue(l.isSignalled());
1130 }});
1131
1132 waitForQueuedThread(l, t);
1133 assertFalse(l.isSignalled());
1134 assertThreadBlocks(t, Thread.State.WAITING);
1135 assertHasSharedQueuedThreads(l, t);
1136 assertTrue(l.releaseShared(0));
1137 assertTrue(l.isSignalled());
1138 awaitTermination(t);
1139 }
1140
1141 /**
1142 * tryAcquireSharedNanos returns after release, but not before
1143 */
1144 public void testTryAcquireSharedNanos() {
1145 final BooleanLatch l = new BooleanLatch();
1146
1147 Thread t = newStartedThread(new CheckedRunnable() {
1148 public void realRun() throws InterruptedException {
1149 assertFalse(l.isSignalled());
1150 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1151 assertTrue(l.tryAcquireSharedNanos(0, nanos));
1152 assertTrue(l.isSignalled());
1153 assertTrue(l.tryAcquireSharedNanos(0, nanos));
1154 assertTrue(l.isSignalled());
1155 }});
1156
1157 waitForQueuedThread(l, t);
1158 assertFalse(l.isSignalled());
1159 assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1160 assertTrue(l.releaseShared(0));
1161 assertTrue(l.isSignalled());
1162 awaitTermination(t);
1163 }
1164
1165 /**
1166 * acquireSharedInterruptibly is interruptible
1167 */
1168 public void testAcquireSharedInterruptibly_Interruptible() {
1169 final BooleanLatch l = new BooleanLatch();
1170 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1171 public void realRun() throws InterruptedException {
1172 assertFalse(l.isSignalled());
1173 l.acquireSharedInterruptibly(0);
1174 }});
1175
1176 waitForQueuedThread(l, t);
1177 assertFalse(l.isSignalled());
1178 t.interrupt();
1179 awaitTermination(t);
1180 assertFalse(l.isSignalled());
1181 }
1182
1183 /**
1184 * tryAcquireSharedNanos is interruptible
1185 */
1186 public void testTryAcquireSharedNanos_Interruptible() {
1187 final BooleanLatch l = new BooleanLatch();
1188 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1189 public void realRun() throws InterruptedException {
1190 assertFalse(l.isSignalled());
1191 long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1192 l.tryAcquireSharedNanos(0, nanos);
1193 }});
1194
1195 waitForQueuedThread(l, t);
1196 assertFalse(l.isSignalled());
1197 t.interrupt();
1198 awaitTermination(t);
1199 assertFalse(l.isSignalled());
1200 }
1201
1202 /**
1203 * tryAcquireSharedNanos times out if not released before timeout
1204 */
1205 public void testTryAcquireSharedNanos_Timeout() {
1206 final BooleanLatch l = new BooleanLatch();
1207 final BooleanLatch observedQueued = new BooleanLatch();
1208 Thread t = newStartedThread(new CheckedRunnable() {
1209 public void realRun() throws InterruptedException {
1210 assertFalse(l.isSignalled());
1211 for (long millis = timeoutMillis();
1212 !observedQueued.isSignalled();
1213 millis *= 2) {
1214 long nanos = MILLISECONDS.toNanos(millis);
1215 long startTime = System.nanoTime();
1216 assertFalse(l.tryAcquireSharedNanos(0, nanos));
1217 assertTrue(millisElapsedSince(startTime) >= millis);
1218 }
1219 assertFalse(l.isSignalled());
1220 }});
1221
1222 waitForQueuedThread(l, t);
1223 observedQueued.releaseShared(0);
1224 assertFalse(l.isSignalled());
1225 awaitTermination(t);
1226 assertFalse(l.isSignalled());
1227 }
1228
1229 /**
1230 * awaitNanos/timed await with 0 wait times out immediately
1231 */
1232 public void testAwait_Zero() throws InterruptedException {
1233 final Mutex sync = new Mutex();
1234 final ConditionObject c = sync.newCondition();
1235 sync.acquire();
1236 assertTrue(c.awaitNanos(0L) <= 0);
1237 assertFalse(c.await(0L, NANOSECONDS));
1238 sync.release();
1239 }
1240
1241 /**
1242 * awaitNanos/timed await with maximum negative wait times does not underflow
1243 */
1244 public void testAwait_NegativeInfinity() throws InterruptedException {
1245 final Mutex sync = new Mutex();
1246 final ConditionObject c = sync.newCondition();
1247 sync.acquire();
1248 assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1249 assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1250 sync.release();
1251 }
1252
1253 }