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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.14 by dl, Sun Dec 28 22:44:59 2003 UTC vs.
Revision 1.20 by dl, Fri Jul 2 10:24:00 2004 UTC

# Line 156 | Line 156 | public class ReentrantReadWriteLockTest
156       */
157      public void testWriteLockInterruptibly_Interrupted() {
158          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159        lock.writeLock().lock();
159          Thread t = new Thread(new Runnable() {
160                  public void run() {
161                      try {
162                          lock.writeLock().lockInterruptibly();
163 <                        threadShouldThrow();
163 >                        lock.writeLock().unlock();
164 >                        lock.writeLock().lockInterruptibly();
165 >                        lock.writeLock().unlock();
166                      } catch(InterruptedException success){}
167                  }
168              });
169          try {
170 +            lock.writeLock().lock();
171              t.start();
172              t.interrupt();
173              lock.writeLock().unlock();
# Line 176 | Line 178 | public class ReentrantReadWriteLockTest
178      }
179  
180      /**
181 <     * timed write-trylock is interruptible
181 >     * timed write-tryLock is interruptible
182       */
183      public void testWriteTryLock_Interrupted() {
184          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 185 | Line 187 | public class ReentrantReadWriteLockTest
187                  public void run() {
188                      try {
189                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
188                        threadShouldThrow();
190                      } catch(InterruptedException success){}
191                  }
192              });
# Line 209 | Line 210 | public class ReentrantReadWriteLockTest
210                  public void run() {
211                      try {
212                          lock.readLock().lockInterruptibly();
212                        threadShouldThrow();
213                      } catch(InterruptedException success){}
214                  }
215              });
# Line 224 | Line 224 | public class ReentrantReadWriteLockTest
224      }
225  
226      /**
227 <     * timed read-trylock is interruptible
227 >     * timed read-tryLock is interruptible
228       */
229      public void testReadTryLock_Interrupted() {
230          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 248 | Line 248 | public class ReentrantReadWriteLockTest
248  
249      
250      /**
251 <     * write-trylock fails if locked
251 >     * write-tryLock fails if locked
252       */
253      public void testWriteTryLockWhenLocked() {
254          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 268 | Line 268 | public class ReentrantReadWriteLockTest
268      }
269  
270      /**
271 <     * read-trylock fails if locked
271 >     * read-tryLock fails if locked
272       */
273      public void testReadTryLockWhenLocked() {
274          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 376 | Line 376 | public class ReentrantReadWriteLockTest
376          }
377      }
378  
379 +    /**
380 +     * Read trylock succeeds if write locked by current thread
381 +     */
382 +    public void testReadHoldingWriteLock() {
383 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 +        lock.writeLock().lock();
385 +        assertTrue(lock.readLock().tryLock());
386 +        lock.readLock().unlock();
387 +        lock.writeLock().unlock();
388 +    }
389 +
390 +    /**
391 +     * Read lock succeeds if write locked by current thread even if
392 +     * other threads are waiting
393 +     */
394 +    public void testReadHoldingWriteLock2() {
395 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
396 +        lock.writeLock().lock();
397 +        Thread t1 = new Thread(new Runnable() {
398 +                public void run() {
399 +                    lock.readLock().lock();
400 +                    lock.readLock().unlock();
401 +                }
402 +            });
403 +        Thread t2 = new Thread(new Runnable() {
404 +                public void run() {
405 +                    lock.readLock().lock();
406 +                    lock.readLock().unlock();
407 +                }
408 +            });
409 +
410 +        try {
411 +            t1.start();
412 +            t2.start();
413 +            lock.readLock().lock();
414 +            lock.readLock().unlock();
415 +            Thread.sleep(SHORT_DELAY_MS);
416 +            lock.readLock().lock();
417 +            lock.readLock().unlock();
418 +            lock.writeLock().unlock();
419 +            t1.join(MEDIUM_DELAY_MS);
420 +            t2.join(MEDIUM_DELAY_MS);
421 +            assertTrue(!t1.isAlive());
422 +            assertTrue(!t2.isAlive());
423 +          
424 +        } catch(Exception e){
425 +            unexpectedException();
426 +        }
427 +    }
428 +
429 +    /**
430 +     * Fair Read trylock succeeds if write locked by current thread
431 +     */
432 +    public void testReadHoldingWriteLockFair() {
433 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
434 +        lock.writeLock().lock();
435 +        assertTrue(lock.readLock().tryLock());
436 +        lock.readLock().unlock();
437 +        lock.writeLock().unlock();
438 +    }
439 +
440 +    /**
441 +     * Fair Read lock succeeds if write locked by current thread even if
442 +     * other threads are waiting
443 +     */
444 +    public void testReadHoldingWriteLockFair2() {
445 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
446 +        lock.writeLock().lock();
447 +        Thread t1 = new Thread(new Runnable() {
448 +                public void run() {
449 +                    lock.readLock().lock();
450 +                    lock.readLock().unlock();
451 +                }
452 +            });
453 +        Thread t2 = new Thread(new Runnable() {
454 +                public void run() {
455 +                    lock.readLock().lock();
456 +                    lock.readLock().unlock();
457 +                }
458 +            });
459 +
460 +        try {
461 +            t1.start();
462 +            t2.start();
463 +            lock.readLock().lock();
464 +            lock.readLock().unlock();
465 +            Thread.sleep(SHORT_DELAY_MS);
466 +            lock.readLock().lock();
467 +            lock.readLock().unlock();
468 +            lock.writeLock().unlock();
469 +            t1.join(MEDIUM_DELAY_MS);
470 +            t2.join(MEDIUM_DELAY_MS);
471 +            assertTrue(!t1.isAlive());
472 +            assertTrue(!t2.isAlive());
473 +          
474 +        } catch(Exception e){
475 +            unexpectedException();
476 +        }
477 +    }
478 +
479  
480      /**
481 <     * Read trylock succeeds if readlocked but not writelocked
481 >     * Read tryLock succeeds if readlocked but not writelocked
482       */
483      public void testTryLockWhenReadLocked() {
484          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 401 | Line 501 | public class ReentrantReadWriteLockTest
501      
502  
503      /**
504 <     * write trylock fails when readlocked
504 >     * write tryLock fails when readlocked
505       */
506      public void testWriteTryLockWhenReadLocked() {
507          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 423 | Line 523 | public class ReentrantReadWriteLockTest
523      
524  
525      /**
526 <     * write timed trylock times out if locked
526 >     * write timed tryLock times out if locked
527       */
528      public void testWriteTryLock_Timeout() {
529          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 447 | Line 547 | public class ReentrantReadWriteLockTest
547      }
548  
549      /**
550 <     * read timed trylock times out if write-locked
550 >     * read timed tryLock times out if write-locked
551       */
552      public void testReadTryLock_Timeout() {
553          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 877 | Line 977 | public class ReentrantReadWriteLockTest
977      }
978  
979      /**
980 +     * hasQueuedThread(null) throws NPE
981 +     */
982 +    public void testHasQueuedThreadNPE() {
983 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
984 +        try {
985 +            sync.hasQueuedThread(null);
986 +            shouldThrow();
987 +        } catch (NullPointerException success) {
988 +        }
989 +    }
990 +
991 +    /**
992 +     * hasQueuedThread reports whether a thread is queued.
993 +     */
994 +    public void testHasQueuedThread() {
995 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
996 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
997 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
998 +        try {
999 +            assertFalse(sync.hasQueuedThread(t1));
1000 +            assertFalse(sync.hasQueuedThread(t2));
1001 +            sync.writeLock().lock();
1002 +            t1.start();
1003 +            Thread.sleep(SHORT_DELAY_MS);
1004 +            assertTrue(sync.hasQueuedThread(t1));
1005 +            t2.start();
1006 +            Thread.sleep(SHORT_DELAY_MS);
1007 +            assertTrue(sync.hasQueuedThread(t1));
1008 +            assertTrue(sync.hasQueuedThread(t2));
1009 +            t1.interrupt();
1010 +            Thread.sleep(SHORT_DELAY_MS);
1011 +            assertFalse(sync.hasQueuedThread(t1));
1012 +            assertTrue(sync.hasQueuedThread(t2));
1013 +            sync.writeLock().unlock();
1014 +            Thread.sleep(SHORT_DELAY_MS);
1015 +            assertFalse(sync.hasQueuedThread(t1));
1016 +            Thread.sleep(SHORT_DELAY_MS);
1017 +            assertFalse(sync.hasQueuedThread(t2));
1018 +            t1.join();
1019 +            t2.join();
1020 +        } catch(Exception e){
1021 +            unexpectedException();
1022 +        }
1023 +    }
1024 +
1025 +
1026 +    /**
1027       * getQueueLength reports number of waiting threads
1028       */
1029      public void testGetQueueLength() {
# Line 1224 | Line 1371 | public class ReentrantReadWriteLockTest
1371          }
1372      }
1373  
1374 +    /**
1375 +     * toString indicates current lock state
1376 +     */
1377 +    public void testToString() {
1378 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1379 +        String us = lock.toString();
1380 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1381 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1382 +        lock.writeLock().lock();
1383 +        String ws = lock.toString();
1384 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1385 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1386 +        lock.writeLock().unlock();
1387 +        lock.readLock().lock();
1388 +        lock.readLock().lock();
1389 +        String rs = lock.toString();
1390 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1391 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1392 +    }
1393 +
1394 +    /**
1395 +     * readLock.toString indicates current lock state
1396 +     */
1397 +    public void testReadLockToString() {
1398 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1399 +        String us = lock.readLock().toString();
1400 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1401 +        lock.readLock().lock();
1402 +        lock.readLock().lock();
1403 +        String rs = lock.readLock().toString();
1404 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1405 +    }
1406 +
1407 +    /**
1408 +     * writeLock.toString indicates current lock state
1409 +     */
1410 +    public void testWriteLockToString() {
1411 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1412 +        String us = lock.writeLock().toString();
1413 +        assertTrue(us.indexOf("Unlocked") >= 0);
1414 +        lock.writeLock().lock();
1415 +        String ls = lock.writeLock().toString();
1416 +        assertTrue(ls.indexOf("Locked") >= 0);
1417 +    }
1418 +
1419   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines