public void testExplicitOrdering_cycleWithUnorderedLock() { Lock myLock = CycleDetectingLockFactory.newInstance(Policies.THROW) .newReentrantLock("MyLock"); lock03.lock(); myLock.lock(); lock03.unlock(); try { lock01.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "MyLock -> OtherOrder.FIRST", "OtherOrder.THIRD -> MyLock", "OtherOrder.FIRST -> OtherOrder.THIRD"); } }
public void testExplicitOrdering_acquiringMultipleLocksWithSameRank() { CycleDetectingLockFactory.WithExplicitOrdering<OtherOrder> factory = newInstanceWithExplicitOrdering(OtherOrder.class, Policies.THROW); Lock lockA = factory.newReentrantLock(OtherOrder.FIRST); Lock lockB = factory.newReentrantReadWriteLock(OtherOrder.FIRST).readLock(); lockA.lock(); try { lockB.lock(); fail("Expected IllegalStateException"); } catch (IllegalStateException expected) { } lockA.unlock(); lockB.lock(); }
public void testDifferentLockFactories() { CycleDetectingLockFactory otherFactory = CycleDetectingLockFactory.newInstance(Policies.WARN); ReentrantLock lockD = otherFactory.newReentrantLock("LockD"); // lockA -> lockD lockA.lock(); lockD.lock(); lockA.unlock(); lockD.unlock(); // lockD -> lockA should fail even though lockD is from a different factory. lockD.lock(); try { lockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage(expected, "LockD -> LockA", "LockA -> LockD"); } }
public void testDifferentLockFactories_policyExecution() { CycleDetectingLockFactory otherFactory = CycleDetectingLockFactory.newInstance(Policies.WARN); ReentrantLock lockD = otherFactory.newReentrantLock("LockD"); // lockD -> lockA lockD.lock(); lockA.lock(); lockA.unlock(); lockD.unlock(); // lockA -> lockD should warn but otherwise succeed because lockD was // created by a factory with the WARN policy. lockA.lock(); lockD.lock(); }
public void testExplicitOrdering_cycleWithUnorderedLock() { Lock myLock = CycleDetectingLockFactory.newInstance(Policies.THROW).newReentrantLock("MyLock"); lock03.lock(); myLock.lock(); lock03.unlock(); try { lock01.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "MyLock -> OtherOrder.FIRST", "OtherOrder.THIRD -> MyLock", "OtherOrder.FIRST -> OtherOrder.THIRD"); } }
@Override protected void setUp() throws Exception { super.setUp(); CycleDetectingLockFactory factory = CycleDetectingLockFactory.newInstance(Policies.THROW); lockA = factory.newReentrantLock("LockA"); lockB = factory.newReentrantLock("LockB"); lockC = factory.newReentrantLock("LockC"); ReentrantReadWriteLock readWriteLockA = factory.newReentrantReadWriteLock("ReadWriteA"); ReentrantReadWriteLock readWriteLockB = factory.newReentrantReadWriteLock("ReadWriteB"); ReentrantReadWriteLock readWriteLockC = factory.newReentrantReadWriteLock("ReadWriteC"); readLockA = readWriteLockA.readLock(); readLockB = readWriteLockB.readLock(); readLockC = readWriteLockC.readLock(); writeLockA = readWriteLockA.writeLock(); writeLockB = readWriteLockB.writeLock(); writeLockC = readWriteLockC.writeLock(); CycleDetectingLockFactory.WithExplicitOrdering<MyOrder> factory2 = newInstanceWithExplicitOrdering(MyOrder.class, Policies.THROW); lock1 = factory2.newReentrantLock(MyOrder.FIRST); lock2 = factory2.newReentrantLock(MyOrder.SECOND); lock3 = factory2.newReentrantLock(MyOrder.THIRD); CycleDetectingLockFactory.WithExplicitOrdering<OtherOrder> factory3 = newInstanceWithExplicitOrdering(OtherOrder.class, Policies.THROW); lock01 = factory3.newReentrantLock(OtherOrder.FIRST); lock02 = factory3.newReentrantLock(OtherOrder.SECOND); lock03 = factory3.newReentrantLock(OtherOrder.THIRD); }
public void testExplicitOrdering_reentrantAcquisition() { CycleDetectingLockFactory.WithExplicitOrdering<OtherOrder> factory = newInstanceWithExplicitOrdering(OtherOrder.class, Policies.THROW); Lock lockA = factory.newReentrantReadWriteLock(OtherOrder.FIRST).readLock(); Lock lockB = factory.newReentrantLock(OtherOrder.SECOND); lockA.lock(); lockA.lock(); lockB.lock(); lockB.lock(); lockA.unlock(); lockA.unlock(); lockB.unlock(); lockB.unlock(); }