public void TestZeroCapacity()
        {
            var stack = (TransactionStack)TransactionStackFactory.Create(0);
            SimpleOperation operation;
            Assert.AreEqual(false, stack.CanRollback);
            Assert.AreEqual(false, stack.CanRollforward);
            Assert.AreEqual(0, stack.Capacity);
            Assert.AreEqual(true, stack.IsFull);
            Assert.AreEqual(true, stack.IsEmpty);

            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(operation);
            }
            Assert.AreEqual(true, operation.IsFrozen);
            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(operation);
            }
            Assert.AreEqual(true, operation.IsFrozen);
            Assert.AreEqual(false, stack.CanRollback);
            Assert.AreEqual(false, stack.CanRollforward);
            Assert.AreEqual(0, stack.Capacity);
            Assert.AreEqual(true, stack.IsFull);
            Assert.AreEqual(true, stack.IsEmpty);
        }
 public void TestOverCapacity()
 {
     var stack = (TransactionStack)TransactionStackFactory.Create(5);
     var operations = new SimpleOperation[6];
     for (var i = 0; i < 5; ++i)
     {
         Assert.AreEqual(false, stack.IsFull);
         using (stack.CreateTransaction())
         {
             operations[i] = new SimpleOperation();
             stack.PushOperation(operations[i]);
         }
     }
     Assert.AreEqual(true, stack.IsFull);
     Assert.AreEqual(5, stack.Capacity);
     for (var i = 0; i < 5; ++i)
     {
         Assert.AreEqual(operations[i], ((Transaction)stack.Transactions[i]).Operations[0]);
     }
     using (stack.CreateTransaction())
     {
         operations[5] = new SimpleOperation();
         stack.PushOperation(operations[5]);
     }
     Assert.AreEqual(5, stack.Transactions.Count);
     Assert.AreEqual(5, stack.Capacity);
     Assert.AreEqual(true, operations[0].IsFrozen);
     for (var i = 0; i < 5; ++i)
     {
         Assert.AreEqual(operations[i+1], ((Transaction)stack.Transactions[i]).Operations[0]);
     }
 }
Пример #3
0
        public void TestTransactionDiscardPurgeAll()
        {
            var stack              = TransactionStackFactory.Create(5);
            var raiseCount         = 0;
            var expectedRaiseCount = 0;
            var transactions       = new ITransaction[5];

            stack.TransactionDiscarded += (sender, e) =>
            {
                Assert.AreEqual(expectedRaiseCount, ++raiseCount);
                Assert.AreEqual(DiscardReason.StackPurged, e.Reason);
                Assert.AreEqual(5, e.Transactions.Count);
                for (int i = 0; i < 5; ++i)
                {
                    Assert.NotNull(e.Transactions[i]);
                    Assert.AreEqual(transactions[i], e.Transactions[i]);
                    Assert.AreEqual(true, ((Operation)e.Transactions[i]).IsFrozen);
                }
                ++expectedRaiseCount;
            };
            for (var j = 0; j < stack.Capacity; ++j)
            {
                using (var transaction = stack.CreateTransaction())
                {
                    for (var i = 0; i < 3; ++i)
                    {
                        var operation = new SimpleOperation();
                        stack.PushOperation(operation);
                    }
                    transactions[j] = transaction;
                }
            }
            for (var j = 0; j < stack.Capacity; ++j)
            {
                stack.Rollback();
            }
            stack.TransactionCompleted += (sender, e) => Assert.AreEqual(expectedRaiseCount, ++raiseCount);
            using (stack.CreateTransaction())
            {
                for (var i = 0; i < 3; ++i)
                {
                    var operation = new SimpleOperation();
                    stack.PushOperation(operation);
                    expectedRaiseCount = 1;
                }
            }
            Assert.AreEqual(2, expectedRaiseCount);
            Assert.AreEqual(2, raiseCount);
            Assert.AreEqual(5, stack.Capacity);
        }
Пример #4
0
 public void TestSingleOperationTransaction()
 {
     var stack = TransactionStackFactory.Create(5);
     SimpleOperation operation;
     using (stack.CreateTransaction())
     {
         operation = new SimpleOperation();
         stack.PushOperation(new SimpleOperation());
     }
     Assert.AreEqual(false, stack.IsEmpty);
     Assert.AreEqual(true, stack.CanRollback);
     Assert.AreEqual(false, stack.CanRollforward);
     Assert.AreEqual(true, operation.IsDone);
     Assert.AreEqual(0, operation.RollbackCount);
     Assert.AreEqual(0, operation.RollforwardCount);
 }
Пример #5
0
        public void TestSingleOperationTransaction()
        {
            var             stack = TransactionStackFactory.Create(5);
            SimpleOperation operation;

            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(new SimpleOperation());
            }
            Assert.AreEqual(false, stack.IsEmpty);
            Assert.AreEqual(true, stack.CanRollback);
            Assert.AreEqual(false, stack.CanRollforward);
            Assert.AreEqual(true, operation.IsDone);
            Assert.AreEqual(0, operation.RollbackCount);
            Assert.AreEqual(0, operation.RollforwardCount);
        }
Пример #6
0
 public void TestSingleOperationTransactionRollforward()
 {
     var stack = TransactionStackFactory.Create(5);
     SimpleOperation operation;
     using (stack.CreateTransaction())
     {
         operation = new SimpleOperation();
         stack.PushOperation(operation);
     }
     stack.Rollback();
     // Above code must be similar to TestSingleOperationTransactionRollback
     stack.Rollforward();
     Assert.AreEqual(false, stack.IsEmpty);
     Assert.AreEqual(true, stack.CanRollback);
     Assert.AreEqual(false, stack.CanRollforward);
     Assert.AreEqual(true, operation.IsDone);
     Assert.AreEqual(1, operation.RollbackCount);
     Assert.AreEqual(1, operation.RollforwardCount);
 }
Пример #7
0
        public void TestInterleavedTransactionThrows()
        {
            var             stack = (TransactionStack)TransactionStackFactory.Create(5);
            SimpleOperation operation;

            // Root transaction
            var transaction1 = stack.CreateTransaction();

            operation = new SimpleOperation();
            stack.PushOperation(operation);

            // Nested transaction
            stack.CreateTransaction();
            operation = new SimpleOperation();
            stack.PushOperation(operation);

            // Complete root transaction
            Assert.Throws(typeof(TransactionException), () => transaction1.Complete());
        }
Пример #8
0
        public void TestSingleOperationTransactionRollforward()
        {
            var             stack = TransactionStackFactory.Create(5);
            SimpleOperation operation;

            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(operation);
            }
            stack.Rollback();
            // Above code must be similar to TestSingleOperationTransactionRollback
            stack.Rollforward();
            Assert.AreEqual(false, stack.IsEmpty);
            Assert.AreEqual(true, stack.CanRollback);
            Assert.AreEqual(false, stack.CanRollforward);
            Assert.AreEqual(true, operation.IsDone);
            Assert.AreEqual(1, operation.RollbackCount);
            Assert.AreEqual(1, operation.RollforwardCount);
        }
Пример #9
0
        public void TestDiscardStackFull()
        {
            var stack      = TransactionStackFactory.Create(5);
            var operations = new SimpleOperation[6];

            for (var i = 0; i < operations.Length; ++i)
            {
                using (stack.CreateTransaction())
                {
                    operations[i] = new SimpleOperation();
                    stack.PushOperation(operations[i]);
                }
            }

            Assert.AreEqual(true, operations[0].IsFrozen);
            for (var i = 1; i < operations.Length; ++i)
            {
                Assert.AreEqual(false, operations[i].IsFrozen);
                Assert.AreEqual(operations[i], ((TransactionStack)stack).Transactions[i - 1].Operations[0]);
            }
        }
Пример #10
0
 public void TestTransactionCompleted()
 {
     var stack = TransactionStackFactory.Create(5);
     var raiseCount = 0;
     var expectedRaiseCount = 0;
     stack.TransactionCompleted += (sender, e) => Assert.AreEqual(expectedRaiseCount, ++raiseCount);
     for (var j = 0; j < 8; ++j)
     {
         using (stack.CreateTransaction())
         {
             for (var i = 0; i < 5; ++i)
             {
                 var operation = new SimpleOperation();
                 stack.PushOperation(operation);
             }
             ++expectedRaiseCount;
         }
     }
     Assert.AreEqual(8, expectedRaiseCount);
     Assert.AreEqual(8, raiseCount);
 }
Пример #11
0
        public void TestTransactionCompleted()
        {
            var stack              = TransactionStackFactory.Create(5);
            var raiseCount         = 0;
            var expectedRaiseCount = 0;

            stack.TransactionCompleted += (sender, e) => Assert.AreEqual(expectedRaiseCount, ++raiseCount);
            for (var j = 0; j < 8; ++j)
            {
                using (stack.CreateTransaction())
                {
                    for (var i = 0; i < 5; ++i)
                    {
                        var operation = new SimpleOperation();
                        stack.PushOperation(operation);
                    }
                    ++expectedRaiseCount;
                }
            }
            Assert.AreEqual(8, expectedRaiseCount);
            Assert.AreEqual(8, raiseCount);
        }
Пример #12
0
        public void TestClear()
        {
            var stack      = TransactionStackFactory.Create(5);
            var operations = new SimpleOperation[4];

            for (var i = 0; i < operations.Length; ++i)
            {
                using (stack.CreateTransaction())
                {
                    operations[i] = new SimpleOperation();
                    stack.PushOperation(operations[i]);
                }
            }
            stack.Clear();
            Assert.AreEqual(false, stack.CanRollback);
            Assert.AreEqual(false, stack.CanRollforward);
            Assert.AreEqual(5, stack.Capacity);
            Assert.AreEqual(true, stack.IsEmpty);
            Assert.AreEqual(false, stack.IsFull);
            foreach (var operation in operations)
            {
                Assert.AreEqual(true, operation.IsFrozen);
            }
        }
Пример #13
0
 public void TestTransactionRollbacked()
 {
     var stack = TransactionStackFactory.Create(5);
     var raiseCount = 0;
     var expectedRaiseCount = 0;
     stack.TransactionRollbacked += (sender, e) => Assert.AreEqual(expectedRaiseCount, ++raiseCount);
     for (var j = 0; j < stack.Capacity + 3; ++j)
     {
         using (stack.CreateTransaction())
         {
             for (var i = 0; i < 3; ++i)
             {
                 var operation = new SimpleOperation();
                 stack.PushOperation(operation);
             }
         }
     }
     for (var j = 0; j < stack.Capacity; ++j)
     {
         ++expectedRaiseCount;
         stack.Rollback();
     }
     Assert.AreEqual(stack.Capacity, expectedRaiseCount);
     Assert.AreEqual(stack.Capacity, raiseCount);
     Assert.AreEqual(5, stack.Capacity);
 }
Пример #14
0
 public void TestTransactionDiscardPurgeAll()
 {
     var stack = TransactionStackFactory.Create(5);
     var raiseCount = 0;
     var expectedRaiseCount = 0;
     var transactions = new ITransaction[5];
     stack.TransactionDiscarded += (sender, e) =>
     {
         Assert.AreEqual(expectedRaiseCount, ++raiseCount);
         Assert.AreEqual(DiscardReason.StackPurged, e.Reason);
         Assert.AreEqual(5, e.Transactions.Count);
         for (int i = 0; i < 5; ++i)
         {
             Assert.NotNull(e.Transactions[i]);
             Assert.AreEqual(transactions[i], e.Transactions[i]);
             Assert.AreEqual(true, ((Operation)e.Transactions[i]).IsFrozen);
         }
         ++expectedRaiseCount;
     };
     for (var j = 0; j < stack.Capacity; ++j)
     {
         using (var transaction = stack.CreateTransaction())
         {
             for (var i = 0; i < 3; ++i)
             {
                 var operation = new SimpleOperation();
                 stack.PushOperation(operation);
             }
             transactions[j] = transaction;
         }
     }
     for (var j = 0; j < stack.Capacity; ++j)
     {
         stack.Rollback();
     }
     stack.TransactionCompleted += (sender, e) => Assert.AreEqual(expectedRaiseCount, ++raiseCount);
     using (stack.CreateTransaction())
     {
         for (var i = 0; i < 3; ++i)
         {
             var operation = new SimpleOperation();
             stack.PushOperation(operation);
             expectedRaiseCount = 1;
         }
     }
     Assert.AreEqual(2, expectedRaiseCount);
     Assert.AreEqual(2, raiseCount);
     Assert.AreEqual(5, stack.Capacity);
 }
Пример #15
0
 public void TestClear()
 {
     var stack = TransactionStackFactory.Create(5);
     var operations = new SimpleOperation[4];
     for (var i = 0; i < operations.Length; ++i)
     {
         using (stack.CreateTransaction())
         {
             operations[i] = new SimpleOperation();
             stack.PushOperation(operations[i]);
         }
     }
     stack.Clear();
     Assert.AreEqual(false, stack.CanRollback);
     Assert.AreEqual(false, stack.CanRollforward);
     Assert.AreEqual(5, stack.Capacity);
     Assert.AreEqual(true, stack.IsEmpty);
     Assert.AreEqual(false, stack.IsFull);
     foreach (var operation in operations)
     {
         Assert.AreEqual(true, operation.IsFrozen);
     }
 }
Пример #16
0
        public void TestDiscardMultipleStackFull()
        {
            var stack = TransactionStackFactory.Create(5);
            var operations = new SimpleOperation[8];
            for (var i = 0; i < operations.Length; ++i)
            {
                using (stack.CreateTransaction())
                {
                    operations[i] = new SimpleOperation();
                    stack.PushOperation(operations[i]);
                }
            }

            for (int i = 0; i < 3; ++i)
            {
                Assert.AreEqual(true, operations[0].IsFrozen);
            }
            for (var i = 3; i < operations.Length; ++i)
            {
                Assert.AreEqual(false, operations[i].IsFrozen);
                Assert.AreEqual(operations[i], ((TransactionStack)stack).Transactions[i - 3].Operations[0]);
            }
        }
Пример #17
0
        public void TestDiscardOnePurged()
        {
            var stack = TransactionStackFactory.Create(5);
            var operations = new SimpleOperation[6];
            for (var i = 0; i < operations.Length - 1; ++i)
            {
                using (stack.CreateTransaction())
                {
                    operations[i] = new SimpleOperation();
                    stack.PushOperation(operations[i]);
                }
            }
            stack.Rollback();
            using (stack.CreateTransaction())
            {
                operations[5] = new SimpleOperation();
                stack.PushOperation(operations[5]);
            }

            for (var i = 0; i < 4; ++i)
            {
                Assert.AreEqual(false, operations[i].IsFrozen);
                Assert.AreEqual(operations[i], ((TransactionStack)stack).Transactions[i].Operations[0]);
            }
            // operations[4] is the discarded transaction
            Assert.AreEqual(true, operations[4].IsFrozen);
            Assert.AreEqual(false, operations[5].IsFrozen);
            Assert.AreEqual(operations[5], ((TransactionStack)stack).Transactions[4].Operations[0]);
        }