ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.16 by dl, Sun Jan 11 16:02:33 2004 UTC vs.
Revision 1.20 by dl, Fri Feb 24 00:03:16 2006 UTC

# Line 44 | Line 44 | public class AbstractQueuedSynchronizerT
44          }
45          
46          public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
47        
48        public void lock() {
49            acquire(1);
50        }
47  
48      }
49  
# Line 71 | Line 67 | public class AbstractQueuedSynchronizerT
67      /**
68       * A runnable calling acquireInterruptibly
69       */
70 <    class InterruptibleLockRunnable implements Runnable {
71 <        final Mutex lock;
72 <        InterruptibleLockRunnable(Mutex l) { lock = l; }
70 >    class InterruptibleSyncRunnable implements Runnable {
71 >        final Mutex sync;
72 >        InterruptibleSyncRunnable(Mutex l) { sync = l; }
73          public void run() {
74              try {
75 <                lock.acquireInterruptibly(1);
75 >                sync.acquireInterruptibly(1);
76              } catch(InterruptedException success){}
77          }
78      }
# Line 86 | Line 82 | public class AbstractQueuedSynchronizerT
82       * A runnable calling acquireInterruptibly that expects to be
83       * interrupted
84       */
85 <    class InterruptedLockRunnable implements Runnable {
86 <        final Mutex lock;
87 <        InterruptedLockRunnable(Mutex l) { lock = l; }
85 >    class InterruptedSyncRunnable implements Runnable {
86 >        final Mutex sync;
87 >        InterruptedSyncRunnable(Mutex l) { sync = l; }
88          public void run() {
89              try {
90 <                lock.acquireInterruptibly(1);
90 >                sync.acquireInterruptibly(1);
91                  threadShouldThrow();
92              } catch(InterruptedException success){}
93          }
# Line 106 | Line 102 | public class AbstractQueuedSynchronizerT
102      }
103      
104      /**
105 <     * acquiring released lock succeeds
105 >     * acquiring released sync succeeds
106       */
107      public void testAcquire() {
108          Mutex rl = new Mutex();
109          rl.acquire(1);
110          assertTrue(rl.isHeldExclusively());
111          rl.release(1);
112 +        assertFalse(rl.isHeldExclusively());
113      }
114  
115      /**
116 <     * tryAcquire on an released lock succeeds
116 >     * tryAcquire on an released sync succeeds
117       */
118      public void testTryAcquire() {
119          Mutex rl = new Mutex();
# Line 129 | Line 126 | public class AbstractQueuedSynchronizerT
126       * hasQueuedThreads reports whether there are waiting threads
127       */
128      public void testhasQueuedThreads() {
129 <        final Mutex lock = new Mutex();
130 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
129 >        final Mutex sync = new Mutex();
130 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
131 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
132          try {
133 <            assertFalse(lock.hasQueuedThreads());
134 <            lock.acquire(1);
133 >            assertFalse(sync.hasQueuedThreads());
134 >            sync.acquire(1);
135              t1.start();
136              Thread.sleep(SHORT_DELAY_MS);
137 <            assertTrue(lock.hasQueuedThreads());
137 >            assertTrue(sync.hasQueuedThreads());
138              t2.start();
139              Thread.sleep(SHORT_DELAY_MS);
140 <            assertTrue(lock.hasQueuedThreads());
140 >            assertTrue(sync.hasQueuedThreads());
141              t1.interrupt();
142              Thread.sleep(SHORT_DELAY_MS);
143 <            assertTrue(lock.hasQueuedThreads());
144 <            lock.release(1);
143 >            assertTrue(sync.hasQueuedThreads());
144 >            sync.release(1);
145              Thread.sleep(SHORT_DELAY_MS);
146 <            assertFalse(lock.hasQueuedThreads());
146 >            assertFalse(sync.hasQueuedThreads());
147              t1.join();
148              t2.join();
149          } catch(Exception e){
# Line 158 | Line 155 | public class AbstractQueuedSynchronizerT
155       * isQueued(null) throws NPE
156       */
157      public void testIsQueuedNPE() {
158 <        final Mutex lock = new Mutex();
158 >        final Mutex sync = new Mutex();
159          try {
160 <            lock.isQueued(null);
160 >            sync.isQueued(null);
161              shouldThrow();
162          } catch (NullPointerException success) {
163          }
# Line 170 | Line 167 | public class AbstractQueuedSynchronizerT
167       * isQueued reports whether a thread is queued.
168       */
169      public void testIsQueued() {
170 <        final Mutex lock = new Mutex();
171 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
172 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
173 <        try {
174 <            assertFalse(lock.isQueued(t1));
175 <            assertFalse(lock.isQueued(t2));
176 <            lock.acquire(1);
170 >        final Mutex sync = new Mutex();
171 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
172 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
173 >        try {
174 >            assertFalse(sync.isQueued(t1));
175 >            assertFalse(sync.isQueued(t2));
176 >            sync.acquire(1);
177              t1.start();
178              Thread.sleep(SHORT_DELAY_MS);
179 <            assertTrue(lock.isQueued(t1));
179 >            assertTrue(sync.isQueued(t1));
180              t2.start();
181              Thread.sleep(SHORT_DELAY_MS);
182 <            assertTrue(lock.isQueued(t1));
183 <            assertTrue(lock.isQueued(t2));
182 >            assertTrue(sync.isQueued(t1));
183 >            assertTrue(sync.isQueued(t2));
184              t1.interrupt();
185              Thread.sleep(SHORT_DELAY_MS);
186 <            assertFalse(lock.isQueued(t1));
187 <            assertTrue(lock.isQueued(t2));
188 <            lock.release(1);
186 >            assertFalse(sync.isQueued(t1));
187 >            assertTrue(sync.isQueued(t2));
188 >            sync.release(1);
189 >            Thread.sleep(SHORT_DELAY_MS);
190 >            assertFalse(sync.isQueued(t1));
191              Thread.sleep(SHORT_DELAY_MS);
192 <            assertFalse(lock.isQueued(t1));
194 <            assertFalse(lock.isQueued(t2));
192 >            assertFalse(sync.isQueued(t2));
193              t1.join();
194              t2.join();
195          } catch(Exception e){
# Line 200 | Line 198 | public class AbstractQueuedSynchronizerT
198      }
199  
200      /**
201 <     * getFirstQueuedThread returns first waiting thread or null is none
201 >     * getFirstQueuedThread returns first waiting thread or null if none
202       */
203      public void testGetFirstQueuedThread() {
204 <        final Mutex lock = new Mutex();
205 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
206 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
204 >        final Mutex sync = new Mutex();
205 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
206 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
207          try {
208 <            assertNull(lock.getFirstQueuedThread());
209 <            lock.acquire(1);
208 >            assertNull(sync.getFirstQueuedThread());
209 >            sync.acquire(1);
210              t1.start();
211              Thread.sleep(SHORT_DELAY_MS);
212 <            assertEquals(t1, lock.getFirstQueuedThread());
212 >            assertEquals(t1, sync.getFirstQueuedThread());
213              t2.start();
214              Thread.sleep(SHORT_DELAY_MS);
215 <            assertEquals(t1, lock.getFirstQueuedThread());
215 >            assertEquals(t1, sync.getFirstQueuedThread());
216              t1.interrupt();
217              Thread.sleep(SHORT_DELAY_MS);
220            assertEquals(t2, lock.getFirstQueuedThread());
221            lock.release(1);
218              Thread.sleep(SHORT_DELAY_MS);
219 <            assertNull(lock.getFirstQueuedThread());
219 >            assertEquals(t2, sync.getFirstQueuedThread());
220 >            sync.release(1);
221 >            Thread.sleep(SHORT_DELAY_MS);
222 >            assertNull(sync.getFirstQueuedThread());
223              t1.join();
224              t2.join();
225          } catch(Exception e){
# Line 233 | Line 232 | public class AbstractQueuedSynchronizerT
232       * hasContended reports false if no thread has ever blocked, else true
233       */
234      public void testHasContended() {
235 <        final Mutex lock = new Mutex();
236 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
237 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
235 >        final Mutex sync = new Mutex();
236 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
237 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
238          try {
239 <            assertFalse(lock.hasContended());
240 <            lock.acquire(1);
239 >            assertFalse(sync.hasContended());
240 >            sync.acquire(1);
241 >            t1.start();
242 >            Thread.sleep(SHORT_DELAY_MS);
243 >            assertTrue(sync.hasContended());
244 >            t2.start();
245 >            Thread.sleep(SHORT_DELAY_MS);
246 >            assertTrue(sync.hasContended());
247 >            t1.interrupt();
248 >            Thread.sleep(SHORT_DELAY_MS);
249 >            assertTrue(sync.hasContended());
250 >            sync.release(1);
251 >            Thread.sleep(SHORT_DELAY_MS);
252 >            assertTrue(sync.hasContended());
253 >            t1.join();
254 >            t2.join();
255 >        } catch(Exception e){
256 >            unexpectedException();
257 >        }
258 >    }
259 >
260 >    /**
261 >     * getQueuedThreads includes waiting threads
262 >     */
263 >    public void testGetQueuedThreads() {
264 >        final Mutex sync = new Mutex();
265 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
266 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
267 >        try {
268 >            assertTrue(sync.getQueuedThreads().isEmpty());
269 >            sync.acquire(1);
270 >            assertTrue(sync.getQueuedThreads().isEmpty());
271 >            t1.start();
272 >            Thread.sleep(SHORT_DELAY_MS);
273 >            assertTrue(sync.getQueuedThreads().contains(t1));
274 >            t2.start();
275 >            Thread.sleep(SHORT_DELAY_MS);
276 >            assertTrue(sync.getQueuedThreads().contains(t1));
277 >            assertTrue(sync.getQueuedThreads().contains(t2));
278 >            t1.interrupt();
279 >            Thread.sleep(SHORT_DELAY_MS);
280 >            assertFalse(sync.getQueuedThreads().contains(t1));
281 >            assertTrue(sync.getQueuedThreads().contains(t2));
282 >            sync.release(1);
283 >            Thread.sleep(SHORT_DELAY_MS);
284 >            assertTrue(sync.getQueuedThreads().isEmpty());
285 >            t1.join();
286 >            t2.join();
287 >        } catch(Exception e){
288 >            unexpectedException();
289 >        }
290 >    }
291 >
292 >    /**
293 >     * getExclusiveQueuedThreads includes waiting threads
294 >     */
295 >    public void testGetExclusiveQueuedThreads() {
296 >        final Mutex sync = new Mutex();
297 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
298 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
299 >        try {
300 >            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
301 >            sync.acquire(1);
302 >            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
303              t1.start();
304              Thread.sleep(SHORT_DELAY_MS);
305 <            assertTrue(lock.hasContended());
305 >            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
306              t2.start();
307              Thread.sleep(SHORT_DELAY_MS);
308 <            assertTrue(lock.hasContended());
308 >            assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
309 >            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
310              t1.interrupt();
311              Thread.sleep(SHORT_DELAY_MS);
312 <            assertTrue(lock.hasContended());
313 <            lock.release(1);
312 >            assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
313 >            assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
314 >            sync.release(1);
315              Thread.sleep(SHORT_DELAY_MS);
316 <            assertTrue(lock.hasContended());
316 >            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
317 >            t1.join();
318 >            t2.join();
319 >        } catch(Exception e){
320 >            unexpectedException();
321 >        }
322 >    }
323 >
324 >    /**
325 >     * getSharedQueuedThreads does not include exclusively waiting threads
326 >     */
327 >    public void testGetSharedQueuedThreads() {
328 >        final Mutex sync = new Mutex();
329 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
330 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
331 >        try {
332 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
333 >            sync.acquire(1);
334 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
335 >            t1.start();
336 >            Thread.sleep(SHORT_DELAY_MS);
337 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
338 >            t2.start();
339 >            Thread.sleep(SHORT_DELAY_MS);
340 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
341 >            t1.interrupt();
342 >            Thread.sleep(SHORT_DELAY_MS);
343 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
344 >            sync.release(1);
345 >            Thread.sleep(SHORT_DELAY_MS);
346 >            assertTrue(sync.getSharedQueuedThreads().isEmpty());
347              t1.join();
348              t2.join();
349          } catch(Exception e){
# Line 262 | Line 355 | public class AbstractQueuedSynchronizerT
355       * tryAcquireNanos is interruptible.
356       */
357      public void testInterruptedException2() {
358 <        final Mutex lock = new Mutex();
359 <        lock.acquire(1);
358 >        final Mutex sync = new Mutex();
359 >        sync.acquire(1);
360          Thread t = new Thread(new Runnable() {
361                  public void run() {
362                      try {
363 <                        lock.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
363 >                        sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
364                          threadShouldThrow();
365                      } catch(InterruptedException success){}
366                  }
# Line 282 | Line 375 | public class AbstractQueuedSynchronizerT
375  
376  
377      /**
378 <     * TryAcquire on a locked lock fails
378 >     * TryAcquire on exclusively held sync fails
379       */
380 <    public void testTryAcquireWhenLocked() {
381 <        final Mutex lock = new Mutex();
382 <        lock.acquire(1);
380 >    public void testTryAcquireWhenSynced() {
381 >        final Mutex sync = new Mutex();
382 >        sync.acquire(1);
383          Thread t = new Thread(new Runnable() {
384                  public void run() {
385 <                    threadAssertFalse(lock.tryAcquire(1));
385 >                    threadAssertFalse(sync.tryAcquire(1));
386                  }
387              });
388          try {
389              t.start();
390              t.join();
391 <            lock.release(1);
391 >            sync.release(1);
392          } catch(Exception e){
393              unexpectedException();
394          }
395      }
396  
397      /**
398 <     * tryAcquireNanos on a locked lock times out
398 >     * tryAcquireNanos on an exclusively held sync times out
399       */
400      public void testAcquireNanos_Timeout() {
401 <        final Mutex lock = new Mutex();
402 <        lock.acquire(1);
401 >        final Mutex sync = new Mutex();
402 >        sync.acquire(1);
403          Thread t = new Thread(new Runnable() {
404                  public void run() {
405                      try {
406 <                        threadAssertFalse(lock.tryAcquireNanos(1, 1000 * 1000));
406 >                        threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
407                      } catch (Exception ex) {
408                          threadUnexpectedException();
409                      }
# Line 319 | Line 412 | public class AbstractQueuedSynchronizerT
412          try {
413              t.start();
414              t.join();
415 <            lock.release(1);
415 >            sync.release(1);
416          } catch(Exception e){
417              unexpectedException();
418          }
# Line 330 | Line 423 | public class AbstractQueuedSynchronizerT
423       * getState is true when acquired and false when not
424       */
425      public void testGetState() {
426 <        final Mutex lock = new Mutex();
427 <        lock.acquire(1);
428 <        assertTrue(lock.isHeldExclusively());
429 <        lock.release(1);
430 <        assertFalse(lock.isHeldExclusively());
426 >        final Mutex sync = new Mutex();
427 >        sync.acquire(1);
428 >        assertTrue(sync.isHeldExclusively());
429 >        sync.release(1);
430 >        assertFalse(sync.isHeldExclusively());
431          Thread t = new Thread(new Runnable() {
432                  public void run() {
433 <                    lock.acquire(1);
433 >                    sync.acquire(1);
434                      try {
435                          Thread.sleep(SMALL_DELAY_MS);
436                      }
437                      catch(Exception e) {
438                          threadUnexpectedException();
439                      }
440 <                    lock.release(1);
440 >                    sync.release(1);
441                  }
442              });
443          try {
444              t.start();
445              Thread.sleep(SHORT_DELAY_MS);
446 <            assertTrue(lock.isHeldExclusively());
446 >            assertTrue(sync.isHeldExclusively());
447              t.join();
448 <            assertFalse(lock.isHeldExclusively());
448 >            assertFalse(sync.isHeldExclusively());
449          } catch(Exception e){
450              unexpectedException();
451          }
# Line 363 | Line 456 | public class AbstractQueuedSynchronizerT
456       * acquireInterruptibly is interruptible.
457       */
458      public void testAcquireInterruptibly1() {
459 <        final Mutex lock = new Mutex();
460 <        lock.acquire(1);
461 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
459 >        final Mutex sync = new Mutex();
460 >        sync.acquire(1);
461 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
462          try {
463              t.start();
464 +            Thread.sleep(SHORT_DELAY_MS);
465              t.interrupt();
466 <            lock.release(1);
466 >            sync.release(1);
467              t.join();
468          } catch(Exception e){
469              unexpectedException();
# Line 380 | Line 474 | public class AbstractQueuedSynchronizerT
474       * acquireInterruptibly succeeds when released, else is interruptible
475       */
476      public void testAcquireInterruptibly2() {
477 <        final Mutex lock = new Mutex();
477 >        final Mutex sync = new Mutex();
478          try {
479 <            lock.acquireInterruptibly(1);
479 >            sync.acquireInterruptibly(1);
480          } catch(Exception e) {
481              unexpectedException();
482          }
483 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
483 >        Thread t = new Thread(new InterruptedSyncRunnable(sync));
484          try {
485              t.start();
486              t.interrupt();
487 <            assertTrue(lock.isHeldExclusively());
487 >            assertTrue(sync.isHeldExclusively());
488              t.join();
489          } catch(Exception e){
490              unexpectedException();
# Line 398 | Line 492 | public class AbstractQueuedSynchronizerT
492      }
493  
494      /**
495 <     * owns is true for a condition created by lock else false
495 >     * owns is true for a condition created by sync else false
496       */
497      public void testOwns() {
498 <        final Mutex lock = new Mutex();
499 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
500 <        final Mutex lock2 = new Mutex();
501 <        assertTrue(lock.owns(c));
502 <        assertFalse(lock2.owns(c));
498 >        final Mutex sync = new Mutex();
499 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
500 >        final Mutex sync2 = new Mutex();
501 >        assertTrue(sync.owns(c));
502 >        assertFalse(sync2.owns(c));
503      }
504  
505      /**
506 <     * Calling await without holding lock throws IllegalMonitorStateException
506 >     * Calling await without holding sync throws IllegalMonitorStateException
507       */
508      public void testAwait_IllegalMonitor() {
509 <        final Mutex lock = new Mutex();
510 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
509 >        final Mutex sync = new Mutex();
510 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
511          try {
512              c.await();
513              shouldThrow();
# Line 426 | Line 520 | public class AbstractQueuedSynchronizerT
520      }
521  
522      /**
523 <     * Calling signal without holding lock throws IllegalMonitorStateException
523 >     * Calling signal without holding sync throws IllegalMonitorStateException
524       */
525      public void testSignal_IllegalMonitor() {
526 <        final Mutex lock = new Mutex();
527 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
526 >        final Mutex sync = new Mutex();
527 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
528          try {
529              c.signal();
530              shouldThrow();
# Line 446 | Line 540 | public class AbstractQueuedSynchronizerT
540       * awaitNanos without a signal times out
541       */
542      public void testAwaitNanos_Timeout() {
543 <        final Mutex lock = new Mutex();
544 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
543 >        final Mutex sync = new Mutex();
544 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
545          try {
546 <            lock.acquire(1);
546 >            sync.acquire(1);
547              long t = c.awaitNanos(100);
548              assertTrue(t <= 0);
549 <            lock.release(1);
549 >            sync.release(1);
550          }
551          catch (Exception ex) {
552              unexpectedException();
# Line 460 | Line 554 | public class AbstractQueuedSynchronizerT
554      }
555  
556      /**
557 <     *  timed await without a signal times out
557 >     *  Timed await without a signal times out
558       */
559      public void testAwait_Timeout() {
560 <        final Mutex lock = new Mutex();
561 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
560 >        final Mutex sync = new Mutex();
561 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
562          try {
563 <            lock.acquire(1);
563 >            sync.acquire(1);
564              assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
565 <            lock.release(1);
565 >            sync.release(1);
566          }
567          catch (Exception ex) {
568              unexpectedException();
# Line 479 | Line 573 | public class AbstractQueuedSynchronizerT
573       * awaitUntil without a signal times out
574       */
575      public void testAwaitUntil_Timeout() {
576 <        final Mutex lock = new Mutex();
577 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
576 >        final Mutex sync = new Mutex();
577 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
578          try {
579 <            lock.acquire(1);
579 >            sync.acquire(1);
580              java.util.Date d = new java.util.Date();
581              assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
582 <            lock.release(1);
582 >            sync.release(1);
583          }
584          catch (Exception ex) {
585              unexpectedException();
# Line 496 | Line 590 | public class AbstractQueuedSynchronizerT
590       * await returns when signalled
591       */
592      public void testAwait() {
593 <        final Mutex lock = new Mutex();
594 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
593 >        final Mutex sync = new Mutex();
594 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
595          Thread t = new Thread(new Runnable() {
596                  public void run() {
597                      try {
598 <                        lock.acquire(1);
598 >                        sync.acquire(1);
599                          c.await();
600 <                        lock.release(1);
600 >                        sync.release(1);
601                      }
602                      catch(InterruptedException e) {
603                          threadUnexpectedException();
# Line 514 | Line 608 | public class AbstractQueuedSynchronizerT
608          try {
609              t.start();
610              Thread.sleep(SHORT_DELAY_MS);
611 <            lock.acquire(1);
611 >            sync.acquire(1);
612              c.signal();
613 <            lock.release(1);
613 >            sync.release(1);
614              t.join(SHORT_DELAY_MS);
615              assertFalse(t.isAlive());
616          }
# Line 531 | Line 625 | public class AbstractQueuedSynchronizerT
625       * hasWaiters throws NPE if null
626       */
627      public void testHasWaitersNPE() {
628 <        final Mutex lock = new Mutex();
628 >        final Mutex sync = new Mutex();
629          try {
630 <            lock.hasWaiters(null);
630 >            sync.hasWaiters(null);
631              shouldThrow();
632          } catch (NullPointerException success) {
633          } catch (Exception ex) {
# Line 545 | Line 639 | public class AbstractQueuedSynchronizerT
639       * getWaitQueueLength throws NPE if null
640       */
641      public void testGetWaitQueueLengthNPE() {
642 <        final Mutex lock = new Mutex();
642 >        final Mutex sync = new Mutex();
643          try {
644 <            lock.getWaitQueueLength(null);
644 >            sync.getWaitQueueLength(null);
645              shouldThrow();
646          } catch (NullPointerException success) {
647          } catch (Exception ex) {
# Line 560 | Line 654 | public class AbstractQueuedSynchronizerT
654       * getWaitingThreads throws NPE if null
655       */
656      public void testGetWaitingThreadsNPE() {
657 <        final Mutex lock = new Mutex();
657 >        final Mutex sync = new Mutex();
658          try {
659 <            lock.getWaitingThreads(null);
659 >            sync.getWaitingThreads(null);
660              shouldThrow();
661          } catch (NullPointerException success) {
662          } catch (Exception ex) {
# Line 575 | Line 669 | public class AbstractQueuedSynchronizerT
669       * hasWaiters throws IAE if not owned
670       */
671      public void testHasWaitersIAE() {
672 <        final Mutex lock = new Mutex();
673 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
674 <        final Mutex lock2 = new Mutex();
672 >        final Mutex sync = new Mutex();
673 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
674 >        final Mutex sync2 = new Mutex();
675          try {
676 <            lock2.hasWaiters(c);
676 >            sync2.hasWaiters(c);
677              shouldThrow();
678          } catch (IllegalArgumentException success) {
679          } catch (Exception ex) {
# Line 588 | Line 682 | public class AbstractQueuedSynchronizerT
682      }
683  
684      /**
685 <     * hasWaiters throws IMSE if not locked
685 >     * hasWaiters throws IMSE if not synced
686       */
687      public void testHasWaitersIMSE() {
688 <        final Mutex lock = new Mutex();
689 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
688 >        final Mutex sync = new Mutex();
689 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
690          try {
691 <            lock.hasWaiters(c);
691 >            sync.hasWaiters(c);
692              shouldThrow();
693          } catch (IllegalMonitorStateException success) {
694          } catch (Exception ex) {
# Line 607 | Line 701 | public class AbstractQueuedSynchronizerT
701       * getWaitQueueLength throws IAE if not owned
702       */
703      public void testGetWaitQueueLengthIAE() {
704 <        final Mutex lock = new Mutex();
705 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
706 <        final Mutex lock2 = new Mutex();
704 >        final Mutex sync = new Mutex();
705 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
706 >        final Mutex sync2 = new Mutex();
707          try {
708 <            lock2.getWaitQueueLength(c);
708 >            sync2.getWaitQueueLength(c);
709              shouldThrow();
710          } catch (IllegalArgumentException success) {
711          } catch (Exception ex) {
# Line 620 | Line 714 | public class AbstractQueuedSynchronizerT
714      }
715  
716      /**
717 <     * getWaitQueueLength throws IMSE if not locked
717 >     * getWaitQueueLength throws IMSE if not synced
718       */
719      public void testGetWaitQueueLengthIMSE() {
720 <        final Mutex lock = new Mutex();
721 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
720 >        final Mutex sync = new Mutex();
721 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
722          try {
723 <            lock.getWaitQueueLength(c);
723 >            sync.getWaitQueueLength(c);
724              shouldThrow();
725          } catch (IllegalMonitorStateException success) {
726          } catch (Exception ex) {
# Line 639 | Line 733 | public class AbstractQueuedSynchronizerT
733       * getWaitingThreads throws IAE if not owned
734       */
735      public void testGetWaitingThreadsIAE() {
736 <        final Mutex lock = new Mutex();
737 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
738 <        final Mutex lock2 = new Mutex();        
736 >        final Mutex sync = new Mutex();
737 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
738 >        final Mutex sync2 = new Mutex();        
739          try {
740 <            lock2.getWaitingThreads(c);
740 >            sync2.getWaitingThreads(c);
741              shouldThrow();
742          } catch (IllegalArgumentException success) {
743          } catch (Exception ex) {
# Line 652 | Line 746 | public class AbstractQueuedSynchronizerT
746      }
747  
748      /**
749 <     * getWaitingThreads throws IMSE if not locked
749 >     * getWaitingThreads throws IMSE if not synced
750       */
751      public void testGetWaitingThreadsIMSE() {
752 <        final Mutex lock = new Mutex();
753 <        final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition());
752 >        final Mutex sync = new Mutex();
753 >        final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition());
754          try {
755 <            lock.getWaitingThreads(c);
755 >            sync.getWaitingThreads(c);
756              shouldThrow();
757          } catch (IllegalMonitorStateException success) {
758          } catch (Exception ex) {
# Line 672 | Line 766 | public class AbstractQueuedSynchronizerT
766       * hasWaiters returns true when a thread is waiting, else false
767       */
768      public void testHasWaiters() {
769 <        final Mutex lock = new Mutex();
770 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
769 >        final Mutex sync = new Mutex();
770 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
771          Thread t = new Thread(new Runnable() {
772                  public void run() {
773                      try {
774 <                        lock.acquire(1);
775 <                        threadAssertFalse(lock.hasWaiters(c));
776 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
774 >                        sync.acquire(1);
775 >                        threadAssertFalse(sync.hasWaiters(c));
776 >                        threadAssertEquals(0, sync.getWaitQueueLength(c));
777                          c.await();
778 <                        lock.release(1);
778 >                        sync.release(1);
779                      }
780                      catch(InterruptedException e) {
781                          threadUnexpectedException();
# Line 692 | Line 786 | public class AbstractQueuedSynchronizerT
786          try {
787              t.start();
788              Thread.sleep(SHORT_DELAY_MS);
789 <            lock.acquire(1);
790 <            assertTrue(lock.hasWaiters(c));
791 <            assertEquals(1, lock.getWaitQueueLength(c));
789 >            sync.acquire(1);
790 >            assertTrue(sync.hasWaiters(c));
791 >            assertEquals(1, sync.getWaitQueueLength(c));
792              c.signal();
793 <            lock.release(1);
793 >            sync.release(1);
794              Thread.sleep(SHORT_DELAY_MS);
795 <            lock.acquire(1);
796 <            assertFalse(lock.hasWaiters(c));
797 <            assertEquals(0, lock.getWaitQueueLength(c));
798 <            lock.release(1);
795 >            sync.acquire(1);
796 >            assertFalse(sync.hasWaiters(c));
797 >            assertEquals(0, sync.getWaitQueueLength(c));
798 >            sync.release(1);
799              t.join(SHORT_DELAY_MS);
800              assertFalse(t.isAlive());
801          }
# Line 714 | Line 808 | public class AbstractQueuedSynchronizerT
808       * getWaitQueueLength returns number of waiting threads
809       */
810      public void testGetWaitQueueLength() {
811 <        final Mutex lock = new Mutex();
812 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
811 >        final Mutex sync = new Mutex();
812 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
813          Thread t1 = new Thread(new Runnable() {
814                  public void run() {
815                      try {
816 <                        lock.acquire(1);
817 <                        threadAssertFalse(lock.hasWaiters(c));
818 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
816 >                        sync.acquire(1);
817 >                        threadAssertFalse(sync.hasWaiters(c));
818 >                        threadAssertEquals(0, sync.getWaitQueueLength(c));
819                          c.await();
820 <                        lock.release(1);
820 >                        sync.release(1);
821                      }
822                      catch(InterruptedException e) {
823                          threadUnexpectedException();
# Line 734 | Line 828 | public class AbstractQueuedSynchronizerT
828          Thread t2 = new Thread(new Runnable() {
829                  public void run() {
830                      try {
831 <                        lock.acquire(1);
832 <                        threadAssertTrue(lock.hasWaiters(c));
833 <                        threadAssertEquals(1, lock.getWaitQueueLength(c));
831 >                        sync.acquire(1);
832 >                        threadAssertTrue(sync.hasWaiters(c));
833 >                        threadAssertEquals(1, sync.getWaitQueueLength(c));
834                          c.await();
835 <                        lock.release(1);
835 >                        sync.release(1);
836                      }
837                      catch(InterruptedException e) {
838                          threadUnexpectedException();
# Line 751 | Line 845 | public class AbstractQueuedSynchronizerT
845              Thread.sleep(SHORT_DELAY_MS);
846              t2.start();
847              Thread.sleep(SHORT_DELAY_MS);
848 <            lock.acquire(1);
849 <            assertTrue(lock.hasWaiters(c));
850 <            assertEquals(2, lock.getWaitQueueLength(c));
848 >            sync.acquire(1);
849 >            assertTrue(sync.hasWaiters(c));
850 >            assertEquals(2, sync.getWaitQueueLength(c));
851              c.signalAll();
852 <            lock.release(1);
852 >            sync.release(1);
853              Thread.sleep(SHORT_DELAY_MS);
854 <            lock.acquire(1);
855 <            assertFalse(lock.hasWaiters(c));
856 <            assertEquals(0, lock.getWaitQueueLength(c));
857 <            lock.release(1);
854 >            sync.acquire(1);
855 >            assertFalse(sync.hasWaiters(c));
856 >            assertEquals(0, sync.getWaitQueueLength(c));
857 >            sync.release(1);
858              t1.join(SHORT_DELAY_MS);
859              t2.join(SHORT_DELAY_MS);
860              assertFalse(t1.isAlive());
# Line 775 | Line 869 | public class AbstractQueuedSynchronizerT
869       * getWaitingThreads returns only and all waiting threads
870       */
871      public void testGetWaitingThreads() {
872 <        final Mutex lock = new Mutex();
873 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
872 >        final Mutex sync = new Mutex();
873 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
874          Thread t1 = new Thread(new Runnable() {
875                  public void run() {
876                      try {
877 <                        lock.acquire(1);
878 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
877 >                        sync.acquire(1);
878 >                        threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
879                          c.await();
880 <                        lock.release(1);
880 >                        sync.release(1);
881                      }
882                      catch(InterruptedException e) {
883                          threadUnexpectedException();
# Line 794 | Line 888 | public class AbstractQueuedSynchronizerT
888          Thread t2 = new Thread(new Runnable() {
889                  public void run() {
890                      try {
891 <                        lock.acquire(1);
892 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
891 >                        sync.acquire(1);
892 >                        threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
893                          c.await();
894 <                        lock.release(1);
894 >                        sync.release(1);
895                      }
896                      catch(InterruptedException e) {
897                          threadUnexpectedException();
# Line 806 | Line 900 | public class AbstractQueuedSynchronizerT
900              });
901  
902          try {
903 <            lock.acquire(1);
904 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
905 <            lock.release(1);
903 >            sync.acquire(1);
904 >            assertTrue(sync.getWaitingThreads(c).isEmpty());
905 >            sync.release(1);
906              t1.start();
907              Thread.sleep(SHORT_DELAY_MS);
908              t2.start();
909              Thread.sleep(SHORT_DELAY_MS);
910 <            lock.acquire(1);
911 <            assertTrue(lock.hasWaiters(c));
912 <            assertTrue(lock.getWaitingThreads(c).contains(t1));
913 <            assertTrue(lock.getWaitingThreads(c).contains(t2));
910 >            sync.acquire(1);
911 >            assertTrue(sync.hasWaiters(c));
912 >            assertTrue(sync.getWaitingThreads(c).contains(t1));
913 >            assertTrue(sync.getWaitingThreads(c).contains(t2));
914              c.signalAll();
915 <            lock.release(1);
915 >            sync.release(1);
916              Thread.sleep(SHORT_DELAY_MS);
917 <            lock.acquire(1);
918 <            assertFalse(lock.hasWaiters(c));
919 <            assertTrue(lock.getWaitingThreads(c).isEmpty());
920 <            lock.release(1);
917 >            sync.acquire(1);
918 >            assertFalse(sync.hasWaiters(c));
919 >            assertTrue(sync.getWaitingThreads(c).isEmpty());
920 >            sync.release(1);
921              t1.join(SHORT_DELAY_MS);
922              t2.join(SHORT_DELAY_MS);
923              assertFalse(t1.isAlive());
# Line 840 | Line 934 | public class AbstractQueuedSynchronizerT
934       * awaitUninterruptibly doesn't abort on interrupt
935       */
936      public void testAwaitUninterruptibly() {
937 <        final Mutex lock = new Mutex();
938 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
937 >        final Mutex sync = new Mutex();
938 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
939          Thread t = new Thread(new Runnable() {
940                  public void run() {
941 <                    lock.acquire(1);
941 >                    sync.acquire(1);
942                      c.awaitUninterruptibly();
943 <                    lock.release(1);
943 >                    sync.release(1);
944                  }
945              });
946  
# Line 854 | Line 948 | public class AbstractQueuedSynchronizerT
948              t.start();
949              Thread.sleep(SHORT_DELAY_MS);
950              t.interrupt();
951 <            lock.acquire(1);
951 >            sync.acquire(1);
952              c.signal();
953 <            lock.release(1);
860 <            assert(t.isInterrupted());
953 >            sync.release(1);
954              t.join(SHORT_DELAY_MS);
955              assertFalse(t.isAlive());
956          }
# Line 870 | Line 963 | public class AbstractQueuedSynchronizerT
963       * await is interruptible
964       */
965      public void testAwait_Interrupt() {
966 <        final Mutex lock = new Mutex();
967 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
966 >        final Mutex sync = new Mutex();
967 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
968          Thread t = new Thread(new Runnable() {
969                  public void run() {
970                      try {
971 <                        lock.acquire(1);
971 >                        sync.acquire(1);
972                          c.await();
973 <                        lock.release(1);
973 >                        sync.release(1);
974                          threadShouldThrow();
975                      }
976                      catch(InterruptedException success) {
# Line 901 | Line 994 | public class AbstractQueuedSynchronizerT
994       * awaitNanos is interruptible
995       */
996      public void testAwaitNanos_Interrupt() {
997 <        final Mutex lock = new Mutex();
998 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
997 >        final Mutex sync = new Mutex();
998 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
999          Thread t = new Thread(new Runnable() {
1000                  public void run() {
1001                      try {
1002 <                        lock.acquire(1);
1002 >                        sync.acquire(1);
1003                          c.awaitNanos(1000 * 1000 * 1000); // 1 sec
1004 <                        lock.release(1);
1004 >                        sync.release(1);
1005                          threadShouldThrow();
1006                      }
1007                      catch(InterruptedException success) {
# Line 932 | Line 1025 | public class AbstractQueuedSynchronizerT
1025       * awaitUntil is interruptible
1026       */
1027      public void testAwaitUntil_Interrupt() {
1028 <        final Mutex lock = new Mutex();
1029 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
1028 >        final Mutex sync = new Mutex();
1029 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
1030          Thread t = new Thread(new Runnable() {
1031                  public void run() {
1032                      try {
1033 <                        lock.acquire(1);
1033 >                        sync.acquire(1);
1034                          java.util.Date d = new java.util.Date();
1035                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1036 <                        lock.release(1);
1036 >                        sync.release(1);
1037                          threadShouldThrow();
1038                      }
1039                      catch(InterruptedException success) {
# Line 964 | Line 1057 | public class AbstractQueuedSynchronizerT
1057       * signalAll wakes up all threads
1058       */
1059      public void testSignalAll() {
1060 <        final Mutex lock = new Mutex();
1061 <        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
1060 >        final Mutex sync = new Mutex();
1061 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
1062          Thread t1 = new Thread(new Runnable() {
1063                  public void run() {
1064                      try {
1065 <                        lock.acquire(1);
1065 >                        sync.acquire(1);
1066                          c.await();
1067 <                        lock.release(1);
1067 >                        sync.release(1);
1068                      }
1069                      catch(InterruptedException e) {
1070                          threadUnexpectedException();
# Line 982 | Line 1075 | public class AbstractQueuedSynchronizerT
1075          Thread t2 = new Thread(new Runnable() {
1076                  public void run() {
1077                      try {
1078 <                        lock.acquire(1);
1078 >                        sync.acquire(1);
1079                          c.await();
1080 <                        lock.release(1);
1080 >                        sync.release(1);
1081                      }
1082                      catch(InterruptedException e) {
1083                          threadUnexpectedException();
# Line 996 | Line 1089 | public class AbstractQueuedSynchronizerT
1089              t1.start();
1090              t2.start();
1091              Thread.sleep(SHORT_DELAY_MS);
1092 <            lock.acquire(1);
1092 >            sync.acquire(1);
1093              c.signalAll();
1094 <            lock.release(1);
1094 >            sync.release(1);
1095              t1.join(SHORT_DELAY_MS);
1096              t2.join(SHORT_DELAY_MS);
1097              assertFalse(t1.isAlive());
# Line 1014 | Line 1107 | public class AbstractQueuedSynchronizerT
1107       * toString indicates current state
1108       */
1109      public void testToString() {
1110 <        Mutex lock = new Mutex();
1111 <        String us = lock.toString();
1110 >        Mutex sync = new Mutex();
1111 >        String us = sync.toString();
1112          assertTrue(us.indexOf("State = 0") >= 0);
1113 <        lock.acquire(1);
1114 <        String ls = lock.toString();
1113 >        sync.acquire(1);
1114 >        String ls = sync.toString();
1115          assertTrue(ls.indexOf("State = 1") >= 0);
1116      }
1117  
# Line 1058 | Line 1151 | public class AbstractQueuedSynchronizerT
1151      }
1152  
1153      /**
1154 <     * release and has no effect when already signalled
1154 >     * releaseShared has no effect when already signalled
1155       */
1156      public void testReleaseShared() {
1157          final BooleanLatch l = new BooleanLatch();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines