public void Consistent_read_sessions() { ILockManager lockManager = new LockManager(null); var session = Guid.NewGuid(); lockManager.AcquireLock(session, false, "x", "y", "z"); bool success = lockManager.CheckLock(session, false, "x"); Assert.IsTrue(success); // try with a new session (should not work) success = lockManager.CheckLock(Guid.NewGuid(), false, "x"); Assert.IsFalse(success); // read-only lock so it should not work success = lockManager.CheckLock(session, true, "x"); Assert.IsFalse(success); // different resource so it should not work success = lockManager.CheckLock(session, true, "nope"); Assert.IsFalse(success); // trying to close an inactive session throws an exception Assert.Throws <NotSupportedException>(() => lockManager.CloseSession(Guid.NewGuid())); }
public void Managing_pending_locks() { var log = A.Fake <IEventsLog>(); ILockManager lockManager = new LockManager(log); var sessionId = Guid.NewGuid(); lockManager.AcquireLock(sessionId, false, "x", "y", "z"); Assert.AreEqual(3, lockManager.GetCurrentlyHoldLocks()); Assert.IsTrue(lockManager.CheckLock(sessionId, false, "x", "y", "z")); // false because it is a read-only lock Assert.False(lockManager.CheckLock(sessionId, true, "x", "y", "z")); lockManager.CloseSession(sessionId); Assert.AreEqual(0, lockManager.GetCurrentlyHoldLocks()); // session no longer active Assert.IsFalse(lockManager.CheckLock(sessionId, false, "x", "y", "z")); lockManager.AcquireLock(sessionId, true, "tony", "tara"); Assert.AreEqual(2, lockManager.GetCurrentlyHoldLocks()); Thread.Sleep(500); Assert.AreEqual(2, lockManager.GetCurrentlyHoldLocks(100)); var locks = lockManager.ForceRemoveAllLocks(100); A.CallTo(() => log.LogEvent(EventType.LockRemoved, null, 0)).MustHaveHappened(); Assert.AreEqual(2, locks); Assert.AreEqual(0, lockManager.GetCurrentlyHoldLocks()); }