public void NestedTransactionAndRollback() { var needleA = Transact.CreateNeedle(5); var needleB = Transact.CreateNeedle(1); var thread = new Thread(() => { using (var transaction = new Transact()) { needleB.Value = 2; transaction.Commit(); using (var transact = new Transact()) { needleA.Value = 9; Assert.AreEqual(9, needleA.Value); Assert.AreEqual(2, needleB.Value); transact.Commit(); } Assert.AreEqual(9, needleA.Value); Assert.AreEqual(2, needleB.Value); transaction.Rollback(); Assert.AreEqual(5, needleA.Value); Assert.AreEqual(2, needleB.Value); using (new Transact()) { needleA.Value = 13; Assert.AreEqual(13, needleA.Value); Assert.AreEqual(2, needleB.Value); transaction.Rollback(); Assert.AreEqual(5, needleA.Value); Assert.AreEqual(2, needleB.Value); } needleA.Value = 15; transaction.Commit(); } }); thread.Start(); thread.Join(); Assert.AreEqual(15, needleA.Value); Assert.AreEqual(2, needleB.Value); }
public void MultipleCommit() // TODO: Review { var needle = Transact.CreateNeedle(1); using (var autoResetEvent = new AutoResetEvent(false)) { new Thread ( () => { using (var transaction = new Transact()) { needle.Value = 2; transaction.Commit(); needle.Value = 3; transaction.Commit(); needle.Value = 5; transaction.Rollback(); autoResetEvent.Set(); } } ).Start(); autoResetEvent.WaitOne(); Assert.AreEqual(3, needle.Value); } }
public void Rollback() { var needleA = Transact.CreateNeedle(5); var needleB = Transact.CreateNeedle(5); try { using (var transact = new Transact()) { const int Movement = 2; needleA.Value += Movement; ThrowException(); // Really, it is evident this code will not run needleB.Value -= Movement; transact.Commit(); } } catch (Exception exception) { // Catch'em all GC.KeepAlive(exception); } // We did not commit Assert.AreEqual(5, needleA.Value); Assert.AreEqual(5, needleB.Value); //--- using (var transact = new Transact()) { needleA.Value = 9; Assert.AreEqual(9, needleA.Value); Assert.AreEqual(5, needleB.Value); transact.Rollback(); Assert.AreEqual(5, needleA.Value); Assert.AreEqual(5, needleB.Value); } // We did rollback Assert.AreEqual(5, needleA.Value); Assert.AreEqual(5, needleB.Value); using (var transact = new Transact()) { needleA.Value = 9; Assert.AreEqual(9, needleA.Value); Assert.AreEqual(5, needleB.Value); transact.Rollback(); Assert.AreEqual(5, needleA.Value); Assert.AreEqual(5, needleB.Value); needleA.Value = 11; Assert.AreEqual(11, needleA.Value); Assert.AreEqual(5, needleB.Value); transact.Commit(); } // We did rollback and commit again Assert.AreEqual(11, needleA.Value); Assert.AreEqual(5, needleB.Value); }