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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8