public void LockManagerLockTest() { LockManager target = new LockManager(); int transId = 1; int dataItem = 1; List<int> actual = target.Lock(transId, dataItem, Enumerations.OperationMode.Read); Assert.IsNull(actual); transId = 2; actual = target.Lock(transId, dataItem, Enumerations.OperationMode.Read); Assert.IsNull(actual); actual = target.Lock(transId, dataItem, Enumerations.OperationMode.Write); Assert.AreEqual(1, actual[0]); Assert.AreEqual(2, actual[1]); dataItem = 2; actual = target.Lock(transId, dataItem, Enumerations.OperationMode.Write); Assert.IsNull(actual); transId = 1; actual = target.Lock(transId, dataItem, Enumerations.OperationMode.Read); Assert.AreEqual(2, actual[0]); }
public void LockConflictsTest() { LockManager target = new LockManager(); int transId = 1; int dataItem = 1; target.Lock(transId, dataItem, Enumerations.OperationMode.Read); Lock test = new Lock(transId, Enumerations.OperationMode.Read); // lock never conflicts with a lock from its own transaction Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); test = new Lock(transId, Enumerations.OperationMode.Write); Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); test = new Lock(2,Enumerations.OperationMode.Read); // two read locks do not conflict Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); test = new Lock(2, Enumerations.OperationMode.Write); // A read lock conflicts with a write lock Assert.IsTrue(target.LockConflicts(test, target.lockEntries[dataItem])); transId = dataItem = 2; target.Lock(transId, dataItem, Enumerations.OperationMode.Write); // lock never conflicts with a lock from its own transaction Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); test = new Lock(transId, Enumerations.OperationMode.Read); Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); // a write lock or read lock from another transaction always conflicts with an active write lock transId = 1; test = new Lock(transId, Enumerations.OperationMode.Read); Assert.IsTrue(target.LockConflicts(test, target.lockEntries[dataItem])); test = new Lock(transId, Enumerations.OperationMode.Write); Assert.IsTrue(target.LockConflicts(test, target.lockEntries[dataItem])); // reset everything and just test that a recovered lockmanager denies read locks until a write op comes in target = new LockManager(); // mock up what happens in a recovery - this is covered in a unit test in Site for (int i = 2; i <= 20; i += 2) { target.lockEntries[i] = new LockManagerTableEntry(); target.lockEntries[i].ActiveLocks.Add(new Lock(int.MinValue, Enumerations.OperationMode.Write)); } // try to issue a read op transId = 1; dataItem = 2; test = new Lock(transId, Enumerations.OperationMode.Read); Assert.IsTrue(target.LockConflicts(test, target.lockEntries[dataItem])); // issue a write op which will work test = new Lock(transId, Enumerations.OperationMode.Write); Assert.IsFalse(target.LockConflicts(test, target.lockEntries[dataItem])); }