< prev index next >

test/jdk/java/util/concurrent/tck/StampedLockTest.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


 353                     }
 354                     catch (InterruptedException success) {}
 355                     catch (Throwable fail) { threadUnexpectedException(fail); }
 356                     assertFalse(Thread.interrupted());
 357                     done.countDown();
 358                 }});
 359         }
 360 
 361         await(threadsStarted);
 362         assertEquals(n, done.getCount());
 363         for (Future<?> future : futures) // Interrupt all the tasks
 364             future.cancel(true);
 365         await(done);
 366     }
 367 
 368     /**
 369      * interruptible operations throw InterruptedException when write locked and interrupted
 370      */
 371     public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
 372         final StampedLock lock = new StampedLock();
 373         long s = lock.writeLock();
 374 
 375         Action[] interruptibleLockBlockingActions = {
 376             () -> lock.writeLockInterruptibly(),
 377             () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
 378             () -> lock.readLockInterruptibly(),
 379             () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
 380             () -> lock.asWriteLock().lockInterruptibly(),
 381             () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
 382             () -> lock.asReadLock().lockInterruptibly(),
 383             () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
 384         };
 385         shuffle(interruptibleLockBlockingActions);
 386 
 387         assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);


 388     }
 389 
 390     /**
 391      * interruptible operations throw InterruptedException when read locked and interrupted
 392      */
 393     public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
 394         final StampedLock lock = new StampedLock();
 395         long s = lock.readLock();
 396 
 397         Action[] interruptibleLockBlockingActions = {
 398             () -> lock.writeLockInterruptibly(),
 399             () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
 400             () -> lock.asWriteLock().lockInterruptibly(),
 401             () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
 402         };
 403         shuffle(interruptibleLockBlockingActions);
 404 
 405         assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);


 406     }
 407 
 408     /**
 409      * Non-interruptible operations ignore and preserve interrupt status
 410      */
 411     public void testNonInterruptibleOperationsIgnoreInterrupts() {
 412         final StampedLock lock = new StampedLock();
 413         Thread.currentThread().interrupt();
 414 
 415         for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
 416             long s = assertValid(lock, lock.readLock());
 417             readUnlocker.accept(lock, s);
 418             s = assertValid(lock, lock.tryReadLock());
 419             readUnlocker.accept(lock, s);
 420         }
 421 
 422         lock.asReadLock().lock();
 423         lock.asReadLock().unlock();
 424 
 425         for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {


 709         StampedLock clone = serialClone(lock);
 710         assertTrue(lock.isWriteLocked());
 711         assertFalse(clone.isWriteLocked());
 712         long s = clone.writeLock();
 713         assertTrue(clone.isWriteLocked());
 714         clone.unlockWrite(s);
 715         assertFalse(clone.isWriteLocked());
 716     }
 717 
 718     /**
 719      * toString indicates current lock state
 720      */
 721     public void testToString() {
 722         StampedLock lock = new StampedLock();
 723         assertTrue(lock.toString().contains("Unlocked"));
 724         long s = lock.writeLock();
 725         assertTrue(lock.toString().contains("Write-locked"));
 726         lock.unlockWrite(s);
 727         s = lock.readLock();
 728         assertTrue(lock.toString().contains("Read-locks"));

 729     }
 730 
 731     /**
 732      * tryOptimisticRead succeeds and validates if unlocked, fails if
 733      * exclusively locked
 734      */
 735     public void testValidateOptimistic() throws InterruptedException {
 736         StampedLock lock = new StampedLock();
 737 
 738         assertValid(lock, lock.tryOptimisticRead());
 739 
 740         for (Function<StampedLock, Long> writeLocker : writeLockers()) {
 741             long s = assertValid(lock, writeLocker.apply(lock));
 742             assertEquals(0L, lock.tryOptimisticRead());
 743             releaseWriteLock(lock, s);
 744         }
 745 
 746         for (Function<StampedLock, Long> readLocker : readLockers()) {
 747             long s = assertValid(lock, readLocker.apply(lock));
 748             long p = assertValid(lock, lock.tryOptimisticRead());




 353                     }
 354                     catch (InterruptedException success) {}
 355                     catch (Throwable fail) { threadUnexpectedException(fail); }
 356                     assertFalse(Thread.interrupted());
 357                     done.countDown();
 358                 }});
 359         }
 360 
 361         await(threadsStarted);
 362         assertEquals(n, done.getCount());
 363         for (Future<?> future : futures) // Interrupt all the tasks
 364             future.cancel(true);
 365         await(done);
 366     }
 367 
 368     /**
 369      * interruptible operations throw InterruptedException when write locked and interrupted
 370      */
 371     public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
 372         final StampedLock lock = new StampedLock();
 373         long stamp = lock.writeLock();
 374 
 375         Action[] interruptibleLockBlockingActions = {
 376             () -> lock.writeLockInterruptibly(),
 377             () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
 378             () -> lock.readLockInterruptibly(),
 379             () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
 380             () -> lock.asWriteLock().lockInterruptibly(),
 381             () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
 382             () -> lock.asReadLock().lockInterruptibly(),
 383             () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
 384         };
 385         shuffle(interruptibleLockBlockingActions);
 386 
 387         assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
 388 
 389         releaseWriteLock(lock, stamp);
 390     }
 391 
 392     /**
 393      * interruptible operations throw InterruptedException when read locked and interrupted
 394      */
 395     public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
 396         final StampedLock lock = new StampedLock();
 397         long stamp = lock.readLock();
 398 
 399         Action[] interruptibleLockBlockingActions = {
 400             () -> lock.writeLockInterruptibly(),
 401             () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
 402             () -> lock.asWriteLock().lockInterruptibly(),
 403             () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
 404         };
 405         shuffle(interruptibleLockBlockingActions);
 406 
 407         assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
 408 
 409         releaseReadLock(lock, stamp);
 410     }
 411 
 412     /**
 413      * Non-interruptible operations ignore and preserve interrupt status
 414      */
 415     public void testNonInterruptibleOperationsIgnoreInterrupts() {
 416         final StampedLock lock = new StampedLock();
 417         Thread.currentThread().interrupt();
 418 
 419         for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
 420             long s = assertValid(lock, lock.readLock());
 421             readUnlocker.accept(lock, s);
 422             s = assertValid(lock, lock.tryReadLock());
 423             readUnlocker.accept(lock, s);
 424         }
 425 
 426         lock.asReadLock().lock();
 427         lock.asReadLock().unlock();
 428 
 429         for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {


 713         StampedLock clone = serialClone(lock);
 714         assertTrue(lock.isWriteLocked());
 715         assertFalse(clone.isWriteLocked());
 716         long s = clone.writeLock();
 717         assertTrue(clone.isWriteLocked());
 718         clone.unlockWrite(s);
 719         assertFalse(clone.isWriteLocked());
 720     }
 721 
 722     /**
 723      * toString indicates current lock state
 724      */
 725     public void testToString() {
 726         StampedLock lock = new StampedLock();
 727         assertTrue(lock.toString().contains("Unlocked"));
 728         long s = lock.writeLock();
 729         assertTrue(lock.toString().contains("Write-locked"));
 730         lock.unlockWrite(s);
 731         s = lock.readLock();
 732         assertTrue(lock.toString().contains("Read-locks"));
 733         releaseReadLock(lock, s);
 734     }
 735 
 736     /**
 737      * tryOptimisticRead succeeds and validates if unlocked, fails if
 738      * exclusively locked
 739      */
 740     public void testValidateOptimistic() throws InterruptedException {
 741         StampedLock lock = new StampedLock();
 742 
 743         assertValid(lock, lock.tryOptimisticRead());
 744 
 745         for (Function<StampedLock, Long> writeLocker : writeLockers()) {
 746             long s = assertValid(lock, writeLocker.apply(lock));
 747             assertEquals(0L, lock.tryOptimisticRead());
 748             releaseWriteLock(lock, s);
 749         }
 750 
 751         for (Function<StampedLock, Long> readLocker : readLockers()) {
 752             long s = assertValid(lock, readLocker.apply(lock));
 753             long p = assertValid(lock, lock.tryOptimisticRead());


< prev index next >