Exemplo n.º 1
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.Equal(expectedRaiseCount, ++raiseCount);
                Assert.Equal(DiscardReason.StackPurged, e.Reason);
                Assert.Equal(5, e.Transactions.Count);
                for (int i = 0; i < 5; ++i)
                {
                    Assert.NotNull(e.Transactions[i]);
                    Assert.Equal((object)transactions[i], e.Transactions[i]);
                    Assert.Equal(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.Equal(expectedRaiseCount, ++raiseCount);
            using (stack.CreateTransaction())
            {
                for (var i = 0; i < 3; ++i)
                {
                    var operation = new SimpleOperation();
                    stack.PushOperation(operation);
                    expectedRaiseCount = 1;
                }
            }
            Assert.Equal(2, expectedRaiseCount);
            Assert.Equal(2, raiseCount);
            Assert.Equal(5, stack.Capacity);
        }
Exemplo n.º 2
0
        public void TestSingleOperationTransaction()
        {
            var             stack = TransactionStackFactory.Create(5);
            SimpleOperation operation;

            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(new SimpleOperation());
            }
            Assert.Equal(false, stack.IsEmpty);
            Assert.Equal(true, stack.CanRollback);
            Assert.Equal(false, stack.CanRollforward);
            Assert.Equal(true, operation.IsDone);
            Assert.Equal(0, operation.RollbackCount);
            Assert.Equal(0, operation.RollforwardCount);
        }
Exemplo n.º 3
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 <TransactionException>(() => transaction1.Complete());
        }
Exemplo n.º 4
0
        public void TestSingleOperationTransactionRollback()
        {
            var             stack = TransactionStackFactory.Create(5);
            SimpleOperation operation;

            using (stack.CreateTransaction())
            {
                operation = new SimpleOperation();
                stack.PushOperation(operation);
            }
            // Above code must be similar to TestSingleOperationTransaction
            stack.Rollback();
            Assert.False(stack.IsEmpty);
            Assert.False(stack.CanRollback);
            Assert.True(stack.CanRollforward);
            Assert.False(operation.IsDone);
            Assert.Equal(1, operation.RollbackCount);
            Assert.Equal(0, operation.RollforwardCount);
        }
Exemplo n.º 5
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.Equal(true, operations[0].IsFrozen);
            for (var i = 1; i < operations.Length; ++i)
            {
                Assert.Equal(false, operations[i].IsFrozen);
                Assert.Equal(operations[i], ((TransactionStack)stack).Transactions[i - 1].Operations[0]);
            }
        }
Exemplo n.º 6
0
        public void TestTransactionCompleted()
        {
            var stack              = TransactionStackFactory.Create(5);
            var raiseCount         = 0;
            var expectedRaiseCount = 0;

            stack.TransactionCompleted += (sender, e) => Assert.Equal(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.Equal(8, expectedRaiseCount);
            Assert.Equal(8, raiseCount);
        }
Exemplo n.º 7
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.Equal(false, stack.CanRollback);
            Assert.Equal(false, stack.CanRollforward);
            Assert.Equal(5, stack.Capacity);
            Assert.Equal(true, stack.IsEmpty);
            Assert.Equal(false, stack.IsFull);
            foreach (var operation in operations)
            {
                Assert.Equal(true, operation.IsFrozen);
            }
        }
Exemplo n.º 8
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.True(operations[0].IsFrozen);
            }
            for (var i = 3; i < operations.Length; ++i)
            {
                Assert.False(operations[i].IsFrozen);
                Assert.Equal(operations[i], ((TransactionStack)stack).Transactions[i - 3].Operations[0]);
            }
        }