[cvs] / jsr166 / src / test / tck / AbstractQueuedLongSynchronizerTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/AbstractQueuedLongSynchronizerTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (view) (download)

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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8