private void acquireLockAfterOtherLockStoppedSameThread( boolean firstLockShared, boolean secondLockShared ) throws Exception { AcquiredLock thisThreadsExclusiveLock = acquireExclusiveLockInThisThread(); CountDownLatch firstLockFailed = new CountDownLatch( 1 ); CountDownLatch startSecondLock = new CountDownLatch( 1 ); LockAcquisition lockAcquisition = acquireTwoLocksInAnotherThread( firstLockShared, secondLockShared, firstLockFailed, startSecondLock ); assertThreadIsWaitingForLock( lockAcquisition ); lockAcquisition.stop(); await( firstLockFailed ); thisThreadsExclusiveLock.release(); startSecondLock.countDown(); assertLockAcquisitionSucceeded( lockAcquisition ); }
@Test public void acquireExclusiveLockWhileHoldingSharedLockCanBeStopped() throws Exception { AcquiredLock thisThreadsSharedLock = acquireSharedLockInThisThread(); CountDownLatch sharedLockAcquired = new CountDownLatch( 1 ); CountDownLatch startExclusiveLock = new CountDownLatch( 1 ); LockAcquisition acquisition = acquireSharedAndExclusiveLocksInAnotherThread( sharedLockAcquired, startExclusiveLock ); await( sharedLockAcquired ); startExclusiveLock.countDown(); assertThreadIsWaitingForLock( acquisition ); acquisition.stop(); assertLockAcquisitionFailed( acquisition ); thisThreadsSharedLock.release(); assertNoLocksHeld(); }
private LockAcquisition acquireSharedAndExclusiveLocksInAnotherThread( final CountDownLatch sharedLockAcquired, final CountDownLatch startExclusiveLock ) { final LockAcquisition lockAcquisition = new LockAcquisition(); Future<Void> future = threadA.execute( state -> { try ( Locks.Client client = newLockClient( lockAcquisition ) ) { client.acquireShared( TRACER, NODE, FIRST_NODE_ID ); sharedLockAcquired.countDown(); await( startExclusiveLock ); client.acquireExclusive( TRACER, NODE, FIRST_NODE_ID ); } return null; } ); lockAcquisition.setFuture( future, threadA.get() ); return lockAcquisition; }
private void closeClientAfterLockStopped( boolean shared ) throws Exception { AcquiredLock thisThreadsExclusiveLock = acquireExclusiveLockInThisThread(); CountDownLatch firstLockAcquired = new CountDownLatch( 1 ); LockAcquisition acquisition = tryAcquireTwoLocksLockInAnotherThread( shared, firstLockAcquired ); await( firstLockAcquired ); assertThreadIsWaitingForLock( acquisition ); assertLocksHeld( FIRST_NODE_ID, SECOND_NODE_ID ); acquisition.stop(); assertLockAcquisitionFailed( acquisition ); assertLocksHeld( FIRST_NODE_ID ); thisThreadsExclusiveLock.release(); assertNoLocksHeld(); }
await( startSecondLock );