public void testDeadlock_threeLocks() { // Establish an ordering from lockA -> lockB. lockA.lock(); lockB.lock(); lockB.unlock(); lockA.unlock(); // Establish an ordering from lockB -> lockC. lockB.lock(); lockC.lock(); lockB.unlock(); // lockC -> lockA should fail. try { lockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "LockC -> LockA", "LockB -> LockC", "LockA -> LockB"); } }
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 testReadLock_transitive() { readLockA.lock(); // Establish an ordering from readLockA -> lockB. lockB.lock(); lockB.unlock(); readLockA.unlock(); // Establish an ordering from lockB -> readLockC. lockB.lock(); readLockC.lock(); lockB.unlock(); readLockC.unlock(); // readLockC -> readLockA readLockC.lock(); try { readLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "ReadWriteC -> ReadWriteA", "LockB -> ReadWriteC", "ReadWriteA -> LockB"); } }
public void testWriteLock_threeLockDeadLock() { // Establish an ordering from writeLockA -> writeLockB. writeLockA.lock(); writeLockB.lock(); writeLockB.unlock(); writeLockA.unlock(); // Establish an ordering from writeLockB -> writeLockC. writeLockB.lock(); writeLockC.lock(); writeLockB.unlock(); // writeLockC -> writeLockA should fail. try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "ReadWriteC -> ReadWriteA", "ReadWriteB -> ReadWriteC", "ReadWriteA -> ReadWriteB"); } }
public void testWriteToReadLockDowngrading() { writeLockA.lock(); // writeLockA downgrades to readLockA readLockA.lock(); writeLockA.unlock(); lockB.lock(); // readLockA -> lockB readLockA.unlock(); // lockB -> writeLockA should fail try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "LockB -> ReadWriteA", "ReadWriteA -> LockB"); } }
public void testReadWriteLockDeadlock() { writeLockA.lock(); // Establish an ordering from writeLockA -> lockB lockB.lock(); writeLockA.unlock(); lockB.unlock(); // lockB -> readLockA should fail. lockB.lock(); try { readLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "LockB -> ReadWriteA", "ReadWriteA -> LockB"); } }
public void testReadWriteLockDeadlock_transitive() { readLockA.lock(); // Establish an ordering from readLockA -> lockB lockB.lock(); readLockA.unlock(); lockB.unlock(); // Establish an ordering from lockB -> lockC lockB.lock(); lockC.lock(); lockB.unlock(); lockC.unlock(); // lockC -> writeLockA should fail. lockC.lock(); try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "LockC -> ReadWriteA", "LockB -> LockC", "ReadWriteA -> LockB"); } }
public void testReadWriteLockDeadlock_treatedEquivalently() { readLockA.lock(); // readLockA -> writeLockB writeLockB.lock(); readLockA.unlock(); writeLockB.unlock(); // readLockB -> writeLockA should fail. readLockB.lock(); try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage( expected, "ReadWriteB -> ReadWriteA", "ReadWriteA -> ReadWriteB"); } }
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 testDeadlock_threeLocks() { // Establish an ordering from lockA -> lockB. lockA.lock(); lockB.lock(); lockB.unlock(); lockA.unlock(); // Establish an ordering from lockB -> lockC. lockB.lock(); lockC.lock(); lockB.unlock(); // lockC -> lockA should fail. try { lockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage(expected, "LockC -> LockA", "LockB -> LockC", "LockA -> LockB"); } }
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 testWriteToReadLockDowngrading() { writeLockA.lock(); // writeLockA downgrades to readLockA readLockA.lock(); writeLockA.unlock(); lockB.lock(); // readLockA -> lockB readLockA.unlock(); // lockB -> writeLockA should fail try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage(expected, "LockB -> ReadWriteA", "ReadWriteA -> LockB"); } }
public void testReadWriteLockDeadlock_transitive() { readLockA.lock(); // Establish an ordering from readLockA -> lockB lockB.lock(); readLockA.unlock(); lockB.unlock(); // Establish an ordering from lockB -> lockC lockB.lock(); lockC.lock(); lockB.unlock(); lockC.unlock(); // lockC -> writeLockA should fail. lockC.lock(); try { writeLockA.lock(); fail("Expected PotentialDeadlockException"); } catch (PotentialDeadlockException expected) { checkMessage(expected, "LockC -> ReadWriteA", "LockB -> LockC", "ReadWriteA -> LockB"); } }