public void TransactionManagerConstructorTest() { TransactionManager target = new TransactionManager(); Assert.AreEqual(0, target.CurrentTimeStamp); Assert.AreEqual(0, target.activeTransactions.Count); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(10, target.sites.Count); }
public void ProcessNewInputTest() { using (StreamWriter writer = new StreamWriter("outputFromUnitTestInstance.txt")) { TransactionManager target = new TransactionManager(writer); string inputString = "begin(T1)"; List<Operation> actual = target.ProcessNewInput(inputString); Assert.AreEqual(1, actual.Count); Assert.AreEqual(Enumerations.OperationMode.Begin, actual[0].OpMode); Assert.AreEqual(1, actual[0].TransactionNumber); Assert.AreEqual(-1, actual[0].NewValue); Assert.AreEqual(-1, actual[0].DataItem); Assert.AreEqual(false, actual[0].ReadOnly); Assert.AreEqual(0, actual[0].TimeStamp); // test parsing out begin statements target.IncrementTimeStamp(); inputString = "begin(T2);beginRO(T3)"; actual = target.ProcessNewInput(inputString); Assert.AreEqual(2, actual.Count); Operation expectedOp = actual[0]; Assert.AreEqual(Enumerations.OperationMode.Begin, expectedOp.OpMode); Assert.AreEqual(2, expectedOp.TransactionNumber); Assert.AreEqual(-1, expectedOp.NewValue); Assert.AreEqual(-1, expectedOp.DataItem); Assert.AreEqual(false, expectedOp.ReadOnly); Assert.AreEqual(0, expectedOp.TimeStamp); expectedOp = actual[1]; Assert.AreEqual(Enumerations.OperationMode.Begin, expectedOp.OpMode); Assert.AreEqual(3, expectedOp.TransactionNumber); Assert.AreEqual(-1, expectedOp.NewValue); Assert.AreEqual(-1, expectedOp.DataItem); Assert.AreEqual(true, expectedOp.ReadOnly); Assert.AreEqual(1, expectedOp.TimeStamp); // because read only transactions have a timestamp set when they begin // setup for next test - do not remove because otherwise read/write ops will fail to parse foreach (Operation op in actual) if (op.OpMode == Enumerations.OperationMode.Begin) target.activeTransactions[op.TransactionNumber] = new Transaction(op.TransactionNumber, op.TimeStamp, op.ReadOnly); // test parsing out read statements by a regular transaction target.IncrementTimeStamp(); inputString = "begin(T4);beginRO(T5);R(T2, x4)"; actual = target.ProcessNewInput(inputString); Assert.AreEqual(3, actual.Count); expectedOp = actual[2]; Assert.AreEqual(Enumerations.OperationMode.Read, expectedOp.OpMode); Assert.AreEqual(2, expectedOp.TransactionNumber); Assert.AreEqual(-1, expectedOp.NewValue); Assert.AreEqual(4, expectedOp.DataItem); Assert.AreEqual(false, expectedOp.ReadOnly); Assert.AreEqual(2, expectedOp.TimeStamp); // setup for next test - do not remove because otherwise read/write ops will fail to parse foreach (Operation op in actual) if (op.OpMode == Enumerations.OperationMode.Begin) target.activeTransactions[op.TransactionNumber] = new Transaction(op.TransactionNumber, op.TimeStamp, op.ReadOnly); // test parsing out read statements by a RO transaction target.IncrementTimeStamp(); inputString = "begin(T6);beginRO(T7);R(T3, x4)"; actual = target.ProcessNewInput(inputString); Assert.AreEqual(3, actual.Count); expectedOp = actual[2]; Assert.AreEqual(Enumerations.OperationMode.Read, expectedOp.OpMode); Assert.AreEqual(3, expectedOp.TransactionNumber); Assert.AreEqual(-1, expectedOp.NewValue); Assert.AreEqual(4, expectedOp.DataItem); Assert.AreEqual(true, expectedOp.ReadOnly); Assert.AreEqual(1, expectedOp.TimeStamp); // because read only transactions have a timestamp set when they begin // setup for next test - do not remove because otherwise read/write ops will fail to parse foreach (Operation op in actual) if (op.OpMode == Enumerations.OperationMode.Begin) target.activeTransactions[op.TransactionNumber] = new Transaction(op.TransactionNumber, op.TimeStamp, op.ReadOnly); // test parsing out write statements target.IncrementTimeStamp(); inputString = "begin(T8);R(T2, x4);W(T3, x5, 6)"; actual = target.ProcessNewInput(inputString); Assert.AreEqual(3, actual.Count); expectedOp = actual[2]; Assert.AreEqual(Enumerations.OperationMode.Write, expectedOp.OpMode); Assert.AreEqual(3, expectedOp.TransactionNumber); Assert.AreEqual(6, expectedOp.NewValue); Assert.AreEqual(5, expectedOp.DataItem); Assert.AreEqual(false, expectedOp.ReadOnly); Assert.AreEqual(4, expectedOp.TimeStamp); inputString = "dump()"; actual = target.ProcessNewInput(inputString); inputString = "dump(5)"; actual = target.ProcessNewInput(inputString); inputString = "dump(x5)"; actual = target.ProcessNewInput(inputString); } }
public void TestGetIssuingTransaction() { TransactionManager target = new TransactionManager(); target.ProcessNewInput("R(T1,x4)"); }
public void TickTest() { TransactionManager target = new TransactionManager(); string inputString = "begin(T1);beginRO(T2)"; target.Tick(inputString); Assert.AreEqual(1, target.CurrentTimeStamp); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(2, target.activeTransactions.Count); // test ticking one read op that succeeds inputString = "R(T1, x4)"; target.Tick(inputString); Assert.AreEqual(2, target.CurrentTimeStamp); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(2, target.activeTransactions.Count); // test ticking one write op that succeeds inputString = "W(T1, x6,5)"; target.Tick(inputString); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(2, target.activeTransactions.Count); //foreach (Site site in target.sites.Values) //{ // DataItem item = site.GetDataItemForDebugging(6); // Assert.AreEqual(5, item.DirtyValue); // //Assert.AreEqual(3, item.NewValueTimeStamp); -- we dont really use newValueTimeStamp // Assert.AreEqual(1, item.NewValueTransId); //} // test ticking one read op that succeeds because transaction is read only // same time, enter another r/w transaction inputString = "begin(T3);R(T2, x6)"; List<Result> results = target.Tick(inputString); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(3, target.activeTransactions.Count); Result ROResult = results.Find(matching => matching.IssuingTransaction == 2); Assert.AreEqual(Enumerations.ResultStatus.Success, ROResult.Status); Assert.AreEqual(60, ROResult.Val); // test ticking one write op that fails due to lock conflict // Since this transaction came in later than T1 which currently holds the lock, this transaction is aborted inputString = "W(T3, x6,10)"; results = target.Tick(inputString); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(2, target.activeTransactions.Count); Assert.AreEqual(1, target.abortedTransactions.Count); foreach (Site site in target.sites.Values) { LockManagerTableEntry entry = site.GetCopyOfEntry(6); //Assert.AreEqual(3, entry.WaitingLocks.Peek().TransactionId); Assert.AreEqual(1, entry.ActiveLocks[0].TransactionId); } // enter another transaction for later inputString = "begin(T4)"; target.Tick(inputString); Assert.AreEqual(3, target.activeTransactions.Count); // 3 bacuse T3 should have aborted this tick // lock up x8 inputString = "W(T4, x8,10)"; target.Tick(inputString); // test a read that fails because its waiting for T4 inputString = "R(T1, x8)"; results = target.Tick(inputString); Assert.AreEqual(1, target.waitingTransactions.Count); Assert.AreEqual(2, target.activeTransactions.Count); ROResult = results[0]; Assert.AreEqual(Enumerations.ResultStatus.Failure, ROResult.Status); Assert.AreEqual(1, ROResult.IssuingTransaction); Assert.AreEqual(8, ROResult.DataItem); Assert.AreEqual(0, ROResult.Val); // test ticking a commit by an RO transaction inputString = "end(T2)"; results = target.Tick(inputString); Assert.AreEqual(1, target.waitingTransactions.Count); Assert.AreEqual(1, target.activeTransactions.Count); Assert.AreEqual(1, target.committedTransactions.Count); // test ticking a commit by a regular transaction inputString = "end(T4)"; results = target.Tick(inputString); Assert.AreEqual(2, target.committedTransactions.Count); Assert.AreEqual(1, target.waitingTransactions.Count); // because T1 cannot be freed yet, it needs another tick to try the request again Assert.AreEqual(0, target.activeTransactions.Count); // test ticking a commit that will fail because the site has failed since first access // by failing and recovering each site inputString = "end(T1)"; foreach (Site site in target.sites.Values) site.Fail(target.CurrentTimeStamp); foreach (Site site in target.sites.Values) site.Recover(); target.Tick(inputString); Assert.AreEqual(2, target.committedTransactions.Count); Assert.AreEqual(1, target.waitingTransactions.Count); // because we failed the sites T1 still cannot read Assert.AreEqual(0, target.activeTransactions.Count); // start another transaction, have it write into x8 and commit, this way T1 will be freed and be able to continue inputString = "begin(T6)"; target.Tick(inputString); inputString = "W(T6,x8,4)"; target.Tick(inputString); inputString = "end(T6)"; target.Tick(inputString); // tick again so T1 can be freed up target.Tick(""); Assert.AreEqual(3, target.committedTransactions.Count); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(1, target.activeTransactions.Count); inputString = "end(T1)"; target.Tick(inputString); Assert.AreEqual(3, target.committedTransactions.Count); Assert.AreEqual(0, target.waitingTransactions.Count); Assert.AreEqual(0, target.activeTransactions.Count); Assert.AreEqual(2, target.abortedTransactions.Count); inputString = "fail(1)"; target.Tick(inputString); Assert.AreEqual(Enumerations.SiteStatus.Failed, target.sites[1].Status); int siteFailedTS = target.sites[1].LastFailed[target.sites[1].LastFailed.Count - 1]; Assert.AreEqual(17, siteFailedTS); inputString = "recover(1)"; target.Tick(inputString); Assert.AreEqual(Enumerations.SiteStatus.Active, target.sites[1].Status); }
public void SetTransactionsCurrentOpTest() { TransactionManager target = new TransactionManager(); List<Operation> newOps = new List<Operation>(); target.activeTransactions[1] = new Transaction(1, 1); target.activeTransactions[2] = new Transaction(2, 1); newOps.Add(new Operation(Enumerations.OperationMode.Write, 1)); // should work fine List<Operation> actual = target.SetTransactionsCurrentOp(newOps); Assert.AreEqual(0, actual.Count); newOps.Clear(); newOps.Add(new Operation(Enumerations.OperationMode.Write, 1, 1, 1)); // should be returned back newOps.Add(new Operation(Enumerations.OperationMode.Read, 2, 1)); // should be added to transaction correctly actual = target.SetTransactionsCurrentOp(newOps); Assert.AreEqual(1, actual.Count); Assert.AreEqual(Enumerations.OperationMode.Write, actual[0].OpMode); Assert.AreEqual(1, actual[0].TransactionNumber); Assert.AreEqual(1, actual[0].DataItem); actual = target.SetTransactionsCurrentOp(newOps); // this should return 2 ops Assert.AreEqual(2, actual.Count); Assert.AreEqual(Enumerations.OperationMode.Write, actual[0].OpMode); Assert.AreEqual(1, actual[0].TransactionNumber); Assert.AreEqual(1, actual[0].DataItem); Assert.AreEqual(Enumerations.OperationMode.Read, actual[1].OpMode); Assert.AreEqual(2, actual[1].TransactionNumber); Assert.AreEqual(1, actual[0].DataItem); }
public void SendOpToSiteTest() { TransactionManager target = new TransactionManager(); Operation op = new Operation(Enumerations.OperationMode.Read, 1, 2); // sent to any site target.SendOpToSites(op); Assert.AreEqual(1, target.sites[1].NumberOfMessages()); op = new Operation(Enumerations.OperationMode.Read, 1, 1); // sent to only site 2 target.SendOpToSites(op); Assert.AreEqual(1, target.sites[2].NumberOfMessages()); target.sites[1].Fail(target.CurrentTimeStamp); op = new Operation(Enumerations.OperationMode.Read, 1, 2); target.SendOpToSites(op); Assert.AreEqual(2, target.sites[2].NumberOfMessages()); // because 1 failed this was added to site 2 target.sites[4].Fail(target.CurrentTimeStamp); try { op = new Operation(Enumerations.OperationMode.Read, 1, 3); target.SendOpToSites(op); // will generate an exception } catch (Exception) { Assert.Inconclusive("Should not have hit an error anymore - change in logic of the program"); } // in this point, we continue // start by resetting all the sites buffers); foreach (Site site in target.sites.Values) { site.Fail(0); site.Recover(); } op = new Operation(Enumerations.OperationMode.Write, 1, 2, 5); target.SendOpToSites(op); // check that each non-failed site has this write op in its messageBuffQueue foreach (Site site in target.sites.Values) { if (!site.Failed()) { Assert.AreEqual(1, site.NumberOfMessages()); } else { Assert.AreEqual(0, site.NumberOfMessages()); } } return; }
public void ProcessWaitingOpsTest() { TransactionManager target = new TransactionManager(); target.ProcessWaitingOps(); Assert.AreEqual(0, target.waitingOps.Count); Assert.AreEqual(0, target.waitingTransactions.Count); target.activeTransactions[1] = new Transaction(1, 1); target.activeTransactions[2] = new Transaction(2, 1); target.activeTransactions[2].CurrentOp = new Operation(Enumerations.OperationMode.Read, 2, 4); Assert.IsNull(target.activeTransactions[1].CurrentOp); Assert.IsNotNull(target.activeTransactions[2].CurrentOp); target.waitingOps.Add(new Operation(Enumerations.OperationMode.Write, 1, 2, 2)); target.waitingOps.Add(new Operation(Enumerations.OperationMode.Read, 2, 2)); target.ProcessWaitingOps(); Assert.AreEqual(1, target.waitingOps.Count); Assert.AreEqual(Enumerations.OperationMode.Write, target.activeTransactions[1].CurrentOp.OpMode); Assert.AreEqual(2, target.waitingOps[0].TransactionNumber); Assert.AreEqual(1, target.activeTransactions[1].CurrentOp.TransactionNumber); Assert.AreEqual(2, target.activeTransactions[1].CurrentOp.DataItem); Assert.AreEqual(2, target.activeTransactions[1].CurrentOp.NewValue); target.activeTransactions[2].CurrentOp = null; target.ProcessWaitingOps(); Assert.AreEqual(0, target.waitingOps.Count); Assert.AreEqual(2, target.activeTransactions[2].CurrentOp.TransactionNumber); Assert.AreEqual(2, target.activeTransactions[2].CurrentOp.DataItem); Assert.AreEqual(Enumerations.OperationMode.Read, target.activeTransactions[2].CurrentOp.OpMode); }
public void ProcessNewOpsTest() { TransactionManager target = new TransactionManager(); target.IncrementTimeStamp(); string inputString = "begin(T1);beginRO(T2)"; target.ProcessNewOps(inputString); Assert.AreEqual(2, target.activeTransactions.Keys.Count); Assert.AreEqual(1, target.activeTransactions[1].Id); Assert.IsNull(target.activeTransactions[1].CurrentOp); Assert.AreEqual(Enumerations.Status.Active, target.activeTransactions[1].CurrentStatus); Assert.AreEqual(1, target.activeTransactions[1].TimeStamp); Assert.AreEqual(false, target.activeTransactions[1].ReadOnly); Assert.IsNull(target.activeTransactions[1].CurrentOp); Assert.AreEqual(2, target.activeTransactions[2].Id); Assert.IsNull(target.activeTransactions[2].CurrentOp); Assert.AreEqual(Enumerations.Status.Active, target.activeTransactions[2].CurrentStatus); Assert.AreEqual(1, target.activeTransactions[2].TimeStamp); Assert.AreEqual(true, target.activeTransactions[2].ReadOnly); Assert.IsNull(target.activeTransactions[2].CurrentOp); inputString = "W(T1, x5,6);R(T2, x4)"; target.ProcessNewOps(inputString); Assert.AreEqual(2, target.activeTransactions.Keys.Count); Assert.AreEqual(Enumerations.OperationMode.Write, target.activeTransactions[1].CurrentOp.OpMode); Assert.AreEqual(1, target.activeTransactions[1].CurrentOp.TransactionNumber); Assert.AreEqual(1, target.activeTransactions[1].CurrentOp.TimeStamp); Assert.AreEqual(false, target.activeTransactions[1].CurrentOp.ReadOnly); Assert.AreEqual(6, target.activeTransactions[1].CurrentOp.NewValue); Assert.AreEqual(Enumerations.OperationMode.Read, target.activeTransactions[2].CurrentOp.OpMode); Assert.AreEqual(2, target.activeTransactions[2].CurrentOp.TransactionNumber); Assert.AreEqual(1, target.activeTransactions[2].CurrentOp.TimeStamp); Assert.AreEqual(true, target.activeTransactions[2].CurrentOp.ReadOnly); Assert.AreEqual(-1, target.activeTransactions[2].CurrentOp.NewValue); target.ProcessNewOps(inputString); Assert.AreEqual(2, target.waitingOps.Count); }
private static void Main() { bool fromFile = false; string outputFilePath = string.Empty; try { // Choose whether input comes from a file or a cmd OpenFileDialog inputFile = GetOpenFileDialog(); if (inputFile.ShowDialog() == DialogResult.OK) { fromFile = true; } // Initialize both the output stream writer and the TM outputFilePath = InitializeStreamWriter(); tm = new TransactionManager(file); if (fromFile) ProcessFromFile(inputFile.OpenFile()); else ProcessFromTerminal(); file.WriteLine("End of output"); } catch (Exception e) { PrintErrorMessage(e.Message); Console.In.ReadLine(); } finally { file.Close(); file.Dispose(); // open the output file in the default text editor so the user can view results if (outputFilePath != string.Empty) Process.Start(outputFilePath); } }