public void TestWriteLock()
        {
            var systemClock = new SystemClock();

            var site = new Site(1, new SiteList(systemClock), systemClock);
            var tempVariable = new Variable(1, site, systemClock);

            var transactionOne = new Transaction("T1", systemClock);
            var transactionTwo = new Transaction("T2", systemClock);

            var result = tempVariable.GetWriteLock(transactionOne);

            Assert.IsTrue(result.Contains(transactionOne));
            Assert.IsTrue(tempVariable.WriteLockHolder == transactionOne);

            var resultTwo = tempVariable.GetReadLock(transactionTwo);
            Assert.IsFalse(resultTwo.Contains(transactionTwo));
            Assert.IsTrue(resultTwo.Contains(transactionOne));

            var resultThree = tempVariable.GetWriteLock(transactionTwo);
            Assert.IsFalse(resultThree.Contains(transactionTwo));
            Assert.IsTrue(resultThree.Contains(transactionOne));

            tempVariable.RemoveWriteLock(transactionOne);

            Assert.IsTrue(tempVariable.WriteLockHolder == null);
        }
示例#2
0
        /// <summary>
        /// Initializes a copy of a variable class.
        /// </summary>
        /// <param name="tempVariable">The temp variable.</param>
        public Variable(Variable tempVariable)
        {
            throw new NotImplementedException();
            SystemClock = tempVariable.SystemClock;
            VariableHistory = new List<VariableValue>();
            Id = tempVariable.Id;

            foreach (VariableValue tempValue in tempVariable.VariableHistory)
                VariableHistory.Add(tempValue);
        }
示例#3
0
        /// <summary>
        /// If the transaction should abort, if not, it waits.
        /// </summary>
        /// <param name="variableToAccess">The variable to access.</param>
        /// <param name="transactionSeekingLock">The transaction seeking lock.</param>
        public static bool ShouldAbort(Variable variableToAccess, Transaction transactionSeekingLock)
        {
            bool shouldAbort = false;

            //if we try to access a lock held by an older transaction
            if (variableToAccess.WriteLockHolder != null && variableToAccess.WriteLockHolder.StartTime < transactionSeekingLock.StartTime)
                return true; //we abort
            else
            {
                //if we are waiting on a younger transaction to finish
                if (variableToAccess.WriteLockHolder != null && variableToAccess.WriteLockHolder.StartTime > transactionSeekingLock.StartTime)
                    shouldAbort = false;

                //check the read locks
                foreach (Transaction tempTrans in variableToAccess.ReadLockHolders)
                {
                    if (tempTrans.StartTime < transactionSeekingLock.StartTime)
                        shouldAbort = true;
                }
            }
            return shouldAbort;
        }
        public void TestVariableResetOnFailure()
        {
            var systemClock = new SystemClock();
            var site = new Site(1, new SiteList(systemClock), systemClock);
            var variable = new Variable(1, site, systemClock);

            var transactionOne = new Transaction("T1", systemClock);

            variable.GetReadLock(transactionOne);
            variable.GetWriteLock(transactionOne);

            Assert.IsTrue(variable.IsReadLocked);
            Assert.IsTrue(variable.IsWriteLocked);
            Assert.IsTrue(variable.ReadLockHolders.Contains(transactionOne));
            Assert.IsTrue(variable.WriteLockHolder == transactionOne);

            variable.ResetToComitted();

            Assert.IsFalse(variable.IsReadLocked);
            Assert.IsFalse(variable.IsWriteLocked);
            Assert.IsFalse(variable.ReadLockHolders.Contains(transactionOne));
            Assert.IsFalse(variable.WriteLockHolder == transactionOne);
        }
示例#5
0
        public void TestAcquireAndRemoveReadLock()
        {
            var systemClock = new SystemClock();

            var site = new Site(1, new SiteList(systemClock), systemClock);
            var tempVariable = new Variable(1, site, systemClock);

            var transactionOne = new Transaction("T1", systemClock);
            var transactionTwo = new Transaction("T2", systemClock);

            var result = tempVariable.GetReadLock(transactionOne);
            Assert.IsTrue(result.Contains(transactionOne));

            var result2 = tempVariable.GetReadLock(transactionTwo);
            Assert.IsTrue(result.Contains(transactionOne));
            Assert.IsTrue(result.Contains(transactionTwo));

            tempVariable.RemoveReadLock(transactionOne);
            Assert.IsFalse(tempVariable.ReadLockHolders.Contains(transactionOne));

            tempVariable.RemoveReadLock(transactionTwo);
            Assert.IsFalse(tempVariable.ReadLockHolders.Contains(transactionTwo));
        }
        //[TestMethod]
        public void TestMethod1()
        {
            var systemClock = new SystemClock();
            var siteList = new SiteList(systemClock);

            var siteOne = new Site(1, siteList, systemClock);
            var siteTwo = new Site(2, siteList, systemClock);

            siteList.AddSite(siteOne);
            siteList.AddSite(siteTwo);

            var variableOne = new Variable(2, siteOne, systemClock);
            var variableTwo = new Variable(2, siteTwo, systemClock);

            var transaction = new Transaction("test", systemClock);

            siteOne.VariableList.Add(variableOne);
            siteTwo.VariableList.Add(variableTwo);

            Assert.IsTrue(variableOne.GetWriteLock(transaction).Contains(transaction));
            Assert.IsTrue(variableTwo.GetWriteLock(transaction).Contains(transaction));
            variableOne.Set("5");
            variableOne.CommitValue();

            variableOne.Set("4");

            var affectedTranasactions = siteOne.Fail();
            Assert.IsTrue(siteOne.IsFailed);

            siteOne.Recover();

            Assert.IsTrue(affectedTranasactions.Contains(transaction));
            Assert.IsTrue(
                transaction.AwaitingReReplication.Contains(new ValueSitePair() { Site = siteOne, Variable = variableOne }));
            Assert.IsFalse(siteOne.IsFailed);
        }
示例#7
0
 public bool Equals(Variable obj)
 {
     return obj.Id == this.Id;
 }