public void TransactionExceedingTimeoutTest( )
        {
            var testCache = new TestCache( );

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                // Create a notification manager with a time out interval of 1 seconds.
                var cacheManager = new TransactionEventNotificationManager(testCache, 0);

                // Add an items to the cache while inside a transaction
                testCache.AddItem("A");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("B");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("C");
                cacheManager.EnlistTransaction(Transaction.Current);

                scope.Complete( );

                // As the transaction exceeded the timeout, its data will not
                // be committed
            }

            Assert.IsFalse(testCache.Contains("A"));
            Assert.IsFalse(testCache.Contains("B"));
            Assert.IsFalse(testCache.Contains("C"));
        }
        public void EnlistRolledbackTransactionTest( )
        {
            var testCache = new TestCache( );

            using (new TransactionScope(TransactionScopeOption.Required))
            {
                var cacheManager = new TransactionEventNotificationManager(testCache);

                // Add some items to the cache while inside a transaction
                testCache.AddItem("A");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("B");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("C");
                cacheManager.EnlistTransaction(Transaction.Current);

                // Roll back transaction
            }

            Assert.IsFalse(testCache.Contains("A"));
            Assert.IsFalse(testCache.Contains("B"));
            Assert.IsFalse(testCache.Contains("C"));
        }
        public void EnlistCommitTransactionTest( )
        {
            var testCache = new TestCache( );

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                var cacheManager = new TransactionEventNotificationManager(testCache);

                // Add some items to the cache while inside a transaction
                testCache.AddItem("A");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("B");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("C");
                cacheManager.EnlistTransaction(Transaction.Current);

                // Commit the transaction
                scope.Complete( );
            }

            Assert.IsTrue(testCache.Contains("A"));
            Assert.IsTrue(testCache.Contains("B"));
            Assert.IsTrue(testCache.Contains("C"));
        }
        public void ClearTransactionEventNotifiersTest( )
        {
            var testCache = new TestCache( );

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                var cacheManager = new TransactionEventNotificationManager(testCache);

                // Add some items to the cache while inside a transaction
                testCache.AddItem("A");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("B");
                cacheManager.EnlistTransaction(Transaction.Current);

                testCache.AddItem("C");
                cacheManager.EnlistTransaction(Transaction.Current);

                // Clear the event notifiers
                cacheManager.ClearTransactionEventNotifiers( );

                // Commit the transaction
                // as the. As the event notifiers have been removed
                // the items will not be commited
                scope.Complete( );
            }

            Assert.IsFalse(testCache.Contains("A"));
            Assert.IsFalse(testCache.Contains("B"));
            Assert.IsFalse(testCache.Contains("C"));
        }
Esempio n. 5
0
        /// <summary>
        ///     Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="cache">The cache.</param>
        /// <param name="enlistTran">
        ///     if set to <c>true</c> [enlist tran].
        /// </param>
        private bool Add(TKey key, TValue value, ICache <TKey, TValue> cache, bool enlistTran)
        {
            /////
            // ReSharper disable CompareNonConstrainedGenericWithNull
            /////

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (cache == null)
            {
                throw new ArgumentNullException(nameof(cache));
            }

            if (enlistTran)
            {
                // Enlist in the current transaction so that a commit will
                // add all the new entries to the public cache.
                // Ensure this is called outside any locks to prevent
                // deadlocks.
                _transactionEventNotificationManager.EnlistTransaction(Transaction.Current);
            }

            return(cache.Add(key, value));

            /////
            // ReSharper restore CompareNonConstrainedGenericWithNull
            /////
        }
        public void EnlistNullTransactionTest( )
        {
            var testCache = new TestCache( );

            var cacheManager = new TransactionEventNotificationManager(testCache);

            cacheManager.EnlistTransaction(null);
        }