public void TestMVCC() { Site target = new Site(1); // Setup the site target.AddMessage(new Operation(Enumerations.OperationMode.Write, 1, 2, 5)); target.AddMessage(new Operation(Enumerations.OperationMode.Commit, 1, 2, ts:1)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 2, 2, ro: true, ts: 0)); List<Result> actual = target.Tick(); Result ROResult = actual.Find(matching => matching.IssuingTransaction == 2); // RO should have read the original value of data item 2 Assert.AreEqual(20, ROResult.Val); target.AddMessage(new Operation(Enumerations.OperationMode.Write, 3, 2, 50)); target.AddMessage(new Operation(Enumerations.OperationMode.Commit, 3, 2, ts: 2)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 4, 2, ro: true, ts: 2)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 5, 2, ro: true, ts: 0)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 6, 2, ro: true, ts: 3)); actual = target.Tick(); // transaction ID 4 has issued a read command with ts = 2, so it should read 5 (value committed at time 0, before ts=1) ROResult = actual.Find(matching => matching.IssuingTransaction == 4); Assert.AreEqual(5, ROResult.Val); // transaction ID 5 has issued a read command with ts = 0, so it should read 20 (original value) ROResult = actual.Find(matching => matching.IssuingTransaction == 5); Assert.AreEqual(20, ROResult.Val); // Transaction ID 6 has issued a read command with ts = 3, so it should read 50 ROResult = actual.Find(matching => matching.IssuingTransaction == 6); Assert.AreEqual(50, ROResult.Val); // a transction should read values written BEFORE its timestamp target.AddMessage(new Operation(Enumerations.OperationMode.Write, 7, 4, 4)); target.AddMessage(new Operation(Enumerations.OperationMode.Commit, 7, ts: 1)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 8, 4, ro: true, ts: 1)); actual = target.Tick(); ROResult = actual.Find(matching => matching.IssuingTransaction == 8); Assert.AreEqual(40, ROResult.Val); }
public void TickTest() { Site target = new Site(1); // setup the site target.AddMessage(new Operation(Enumerations.OperationMode.Begin, 1)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 1, 2)); target.AddMessage(new Operation(Enumerations.OperationMode.Write, 1, 2, 40)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 2, 2)); target.AddMessage(new Operation(Enumerations.OperationMode.Write, 2, 4, 30)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 2, 4)); List<Result> actual = target.Tick(); List<Result> expected = new List<Result>(); expected.Add(new Result(1, Enumerations.OperationMode.Begin, 1, Enumerations.ResultStatus.Success, new List<int>() {1})); expected.Add(new Result(1, Enumerations.OperationMode.Read, 1, Enumerations.ResultStatus.Success, new List<int>() {1}, 20, 2)); expected.Add(new Result(1, Enumerations.OperationMode.Write, 1, Enumerations.ResultStatus.Success, new List<int>() {1}, dataItem: 2)); expected.Add(new Result(2, Enumerations.OperationMode.Read, 1, Enumerations.ResultStatus.Failure, new List<int>() {1}, 0, 2)); expected.Add(new Result(2, Enumerations.OperationMode.Write, 1, Enumerations.ResultStatus.Success, new List<int>() {2}, 0, 4)); expected.Add(new Result(2, Enumerations.OperationMode.Read, 1, Enumerations.ResultStatus.Success, new List<int>() {2}, 30, 4)); for (int i = 0; i < Math.Max(actual.Count, expected.Count); i++) { Assert.AreEqual(expected[i].DataItem, actual[i].DataItem); Assert.AreEqual(expected[i].Status, actual[i].Status); Assert.AreEqual(expected[i].TransId[0], actual[i].TransId[0]); Assert.AreEqual(expected[i].Val, actual[i].Val); Assert.AreEqual(1, actual[i].SiteNumber); Assert.AreEqual(expected[i].OpMode, actual[i].OpMode); } // Nothing should be returned because: // a. no new operations were enqueued and // b. no data items became free because of operations actual = target.Tick(); Assert.AreEqual(0, actual.Count); // make (1) release all locks and commit all values // thus (2) can read data item 1 and should read the value committed by (1) target.AddMessage(new Operation(Enumerations.OperationMode.Commit, 1)); target.AddMessage(new Operation(Enumerations.OperationMode.Read, 2, 2)); actual = target.Tick(); expected = new List<Result>(); expected.Add(new Result(1, Enumerations.OperationMode.Commit, 1, Enumerations.ResultStatus.Success, new List<int>() {1}, dataItem:-1)); expected.Add(new Result(1, Enumerations.OperationMode.Read, 1, Enumerations.ResultStatus.Success, new List<int>() {2}, 40, 2)); for (int i = 0; i < Math.Max(actual.Count, expected.Count); i++) { Assert.AreEqual(expected[i].DataItem, actual[i].DataItem); Assert.AreEqual(expected[i].Status, actual[i].Status); Assert.AreEqual(expected[i].TransId[0], actual[i].TransId[0]); Assert.AreEqual(expected[i].Val, actual[i].Val); Assert.AreEqual(1, actual[i].SiteNumber); Assert.AreEqual(expected[i].OpMode, actual[i].OpMode); } }
/// <summary> /// Date: 12/3/2011 /// A wrapper for site.AddMessage /// </summary> /// <param name = "site">The site.</param> /// <param name = "op">The op.</param> private void SendOpToSite(Site site, Operation op) { site.AddMessage(op); }