private int lockCount() { LockCountVisitor lockVisitor = new LockCountVisitor(); locks.accept( lockVisitor ); return lockVisitor.getLockCount(); } }
private static int lockCount( Locks locks ) { LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); return lockCountVisitor.getLockCount(); }
@Test public void mustReleaseUnpreparedLocksOnStop() { // Given clientA.acquireShared( TRACER, NODE, 1L ); clientA.acquireExclusive( TRACER, NODE, 2L ); // When clientA.stop(); // The client was stopped before it could enter the prepare phase, so all of its locks are released LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 0, lockCountVisitor.getLockCount() ); }
@Test public void mustNotReleaseLocksAfterPrepareOnStop() { // Given clientA.acquireShared( TRACER, NODE, 1L ); clientA.acquireExclusive( TRACER, NODE, 2L ); clientA.prepare(); // When clientA.stop(); // The client entered the prepare phase, so it gets to keep its locks LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 2, lockCountVisitor.getLockCount() ); }
@Test public void prepareMustAllowAcquiringNewLocksAfterStop() { // Given clientA.prepare(); clientA.stop(); // When clientA.acquireShared( TRACER, NODE, 1 ); clientA.acquireExclusive( TRACER, NODE, 2 ); // Stopped essentially has no effect when it comes after the client has entered the prepare phase LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 2, lockCountVisitor.getLockCount() ); }
@Test public void releaseTryLocksOnClose() { assertTrue( clientA.trySharedLock( ResourceTypes.NODE, 1L ) ); assertTrue( clientB.tryExclusiveLock( ResourceTypes.NODE, 2L ) ); clientA.close(); clientB.close(); LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 0, lockCountVisitor.getLockCount() ); } }
@Test public void mustReleaseReadLockWaitersOnStop() { // Given clientA.acquireExclusive( TRACER, NODE, 1L ); clientB.acquireExclusive( TRACER, NODE, 2L ); acquireShared( clientB, TRACER, NODE, 1L ).callAndAssertWaiting(); // When clientB.stop(); clientA.stop(); // All locks clients should be stopped at this point, and all all locks should be released because none of the // clients entered the prepare phase LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 0, lockCountVisitor.getLockCount() ); }
@Test public void mustReleaseWriteLockWaitersOnStop() { // Given clientA.acquireShared( TRACER, NODE, 1L ); clientB.acquireShared( TRACER, NODE, 2L ); clientC.acquireShared( TRACER, NODE, 3L ); acquireExclusive( clientB, TRACER, NODE, 1L ).callAndAssertWaiting(); acquireExclusive( clientC, TRACER, NODE, 1L ).callAndAssertWaiting(); // When clientC.stop(); clientB.stop(); clientA.stop(); // All locks clients should be stopped at this point, and all all locks should be released because none of the // clients entered the prepare phase LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 0, lockCountVisitor.getLockCount() ); }
@Test public void closeShouldWaitAllOperationToFinish() { // given clientA.acquireShared( LockTracer.NONE, NODE, 1L ); clientA.acquireShared( LockTracer.NONE, NODE, 3L ); clientB.acquireShared( LockTracer.NONE, NODE, 1L ); acquireShared( clientC, LockTracer.NONE, NODE, 2L ); acquireExclusive( clientB, LockTracer.NONE, NODE, 1L ).callAndAssertWaiting(); acquireExclusive( clientC, LockTracer.NONE, NODE, 1L ).callAndAssertWaiting(); // when clientB.close(); clientC.close(); clientA.close(); // all locks should be closed at this point regardless of // reader/writer waiter in any threads // those should be gracefully finish and client should be closed LockCountVisitor lockCountVisitor = new LockCountVisitor(); locks.accept( lockCountVisitor ); assertEquals( 0, lockCountVisitor.getLockCount() ); }