예제 #1
0
        public void TestAmbientKtmTransaction()
        {
            if (!bfs.Repository.Util.SystemInfo.IsAnyWindows || !EnvironmentSupportsTransactions)
            {
                Assert.Inconclusive("Cannot test in this environment");
                return;
            }
            using (var scope = CreateStandaloneKtmTransactionScope(true))
            {
                // thus allowing to use ambient storage transaction
                Repository.Settings.StorageTransactionSettings = StorageTransactionSettings.DisallowJoiningAmbientManaged;

                KtmTransaction ambientTransaction = (KtmTransaction)AmbientTransaction;

                // eager scope
                using (StorageTransactionScope target = StorageTransactionScope.Create(Repository))
                {
                    Assert.IsFalse(target.IsNullScope, "Eager scope must not produce null scope");
                    Assert.IsFalse(target.IsTransactionOwner, "There's external transaction to be used");
                    Assert.IsFalse(target.HasChangedContext);
                    Assert.IsFalse(target.NoTransaction);
                    Assert.IsFalse(target.ToBeDisposed);

                    Assert.AreSame(ambientTransaction, AmbientTransaction);
                    Assert.AreSame(ambientTransaction, target.UnderlyingTransaction);
                    Assert.AreSame(ambientTransaction, target.PreviousTransaction);
                }

                Assert.IsNotNull(AmbientTransaction);
                Assert.AreSame(ambientTransaction, AmbientTransaction);
                // lazy scope
                using (StorageTransactionScope target = StorageTransactionScope.CreateLazy(Repository, null))
                {
                    Assert.IsFalse(target.IsNullScope, "Because the \"always start new\" option is OFF");
                    Assert.IsFalse(target.IsTransactionOwner, "Lazy scope must never be transaction owner");
                    Assert.IsFalse(target.HasChangedContext, "Lazy scope can only change context to NULL and only when ambient transaction is present but its usage prohibited");
                    Assert.IsFalse(target.NoTransaction, "Lazy scope can result in no ambient transaction when ambient transaction is present but its usage prohibited or there's "
                                   + "no ambient transaction at all");
                    Assert.IsFalse(target.ToBeDisposed, "Lazy scope must not own or dispose underlying transaction");

                    Assert.AreSame(ambientTransaction, AmbientTransaction);
                    Assert.AreSame(ambientTransaction, target.UnderlyingTransaction);
                    Assert.AreSame(ambientTransaction, target.PreviousTransaction);
                }

                Assert.IsNotNull(AmbientTransaction);
                Assert.AreSame(ambientTransaction, AmbientTransaction);
            }

            Assert.IsNull(AmbientTransaction);
        }
예제 #2
0
        public void TestEagerWithAmbientKtmTransactionProhibited()
        {
            if (!bfs.Repository.Util.SystemInfo.IsAnyWindows || !EnvironmentSupportsTransactions)
            {
                Assert.Inconclusive("Cannot test in this environment");
                return;
            }

            Assume.That(AmbientTransaction == null, "Leftovers are not expected here");
            // thus not allowing to use ambient storage transaction
            Repository.Settings.StorageTransactionSettings = StorageTransactionSettings.DisallowJoiningAmbientManaged | StorageTransactionSettings.AlwaysStartNew;

            using (var scope_ = CreateStandaloneKtmTransactionScope(true))
            {
                KtmTransaction ambientTransaction = (KtmTransaction)AmbientTransaction;

                // eager scope
                using (StorageTransactionScope target = StorageTransactionScope.Create(Repository))
                {
                    Assert.IsFalse(target.IsNullScope, "Eager scope must not produce null scope");
                    Assert.IsTrue(target.IsTransactionOwner, "The use of external transaction is prohibited by Repository.Settings.StorageTransactionSettings");
                    Assert.IsTrue(target.HasChangedContext);
                    Assert.IsFalse(target.NoTransaction);
                    Assert.IsTrue(target.ToBeDisposed);

                    Assert.AreNotSame(ambientTransaction, AmbientTransaction);
                    Assert.AreNotSame(ambientTransaction, target.UnderlyingTransaction);
                    Assert.AreSame(ambientTransaction, target.PreviousTransaction);
                }

                Assert.IsNotNull(AmbientTransaction);
                Assert.AreSame(ambientTransaction, AmbientTransaction);
            }

            Assert.IsNull(AmbientTransaction);
        }