Example #1
0
 internal override void WriteTo(XmlWriter xml)
 {
     TraceHelper.WriteTraceSource(xml, traceSource);
     TraceHelper.WriteEnId(xml, enTraceId);
     xml.WriteElementString("EnlistmentType", enType.ToString());
     xml.WriteElementString("EnlistmentOptions", enOptions.ToString());
 }
        protected DurableEnlistmentNotification(Guid resourceManager,
                                                EnlistmentOptions enlistmentOptions)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, "resourceManager={0} enlistmentOptions={1}".FormatWith(resourceManager, enlistmentOptions.ToString("G")));
            Operation = new Operation(resourceManager);
            if (null == Transaction.Current)
            {
                throw new InvalidOperationException("There is no transaction scope to enlist with.");
            }

            Transaction.Current.EnlistDurable(resourceManager, this, enlistmentOptions);
            Transaction.Current.TransactionCompleted += OnTransactionCompleted;
        }
 internal void TransactionstateEnlist(EnlistmentTraceIdentifier enlistmentID, EnlistmentType enlistmentType, EnlistmentOptions enlistmentOption)
 {
     if (IsEnabled(EventLevel.Informational, ALL_KEYWORDS))
     {
         TransactionstateEnlist(enlistmentID.EnlistmentIdentifier.ToString(), enlistmentType.ToString(), enlistmentOption.ToString());
     }
 }
        private static void TestCase_VolatileEnlistments(
            int count,
            TransactionStatus expectedOutcome,
            EnlistmentOptions options = EnlistmentOptions.None,
            bool commitTx = true,
            bool votePrepared = true,
            Type expectedExceptionType = null)
        {
            string testCaseDescription = string.Format("TestCase_VolatileEnlistments; count = {0}; expectedOutcome = {1}; options = {2}; votePrepared = {3}, expectedExceptionType = {4}",
                        count,
                        expectedOutcome.ToString(),
                        options.ToString(),
                        votePrepared,
                        expectedExceptionType);

            Trace("**** " + testCaseDescription + " ****");

            AutoResetEvent[] enlistmentDoneEvts = new AutoResetEvent[count];
            MyEnlistment[] vols = new MyEnlistment[count];
            for (int i = 0; i < count; i++)
            {
                enlistmentDoneEvts[i] = new AutoResetEvent(false);
            }

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    for (int i = 0; i < count; i++)
                    {
                        vols[i] = CreateVolatileEnlistment(enlistmentDoneEvts[i], null, options, votePrepared);
                    }
                    if (commitTx)
                    {
                        ts.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() != expectedExceptionType)
                {
                    TestFailed(testCaseDescription, string.Format("Unexpected exception {0}: {1}", ex.GetType().ToString(), ex.ToString()));
                    return;
                }
            }

            for (int i = 0; i < count; i++)
            {
                if (!enlistmentDoneEvts[i].WaitOne(TimeSpan.FromSeconds(5)))
                {
                    TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes");
                    return;
                }
            }

            int passCount = 0;
            for (int i = 0; i < count; i++)
            {
                if (((expectedOutcome == TransactionStatus.Committed) && vols[i].CommittedOutcome) ||
                    ((expectedOutcome == TransactionStatus.Aborted) && vols[i].AbortedOutcome) ||
                    ((expectedOutcome == TransactionStatus.InDoubt) && vols[i].InDoubtOutcome)
                    )
                {
                    passCount++;
                }
            }
            if (passCount == count)
            {
                TestPassed();
            }
            else
            {
                TestFailed(testCaseDescription, string.Format("{0} enlistments received an outcome of {1}", passCount, expectedOutcome.ToString()));
            }
        }
        public static void TestCase_EnlistDuringPrepare(bool promote,
            bool beforePromote,
            EnlistmentOptions firstOptions = EnlistmentOptions.None,
            EnlistmentOptions secondOptions = EnlistmentOptions.None,
            bool expectSecondEnlistSuccess = true
            )
        {
            string testCaseDescription = string.Format(
                "TestCase_EnlistDuringPrepare promote={0}; beforePromote={1}, firstOptions={2}, secondOptions={3}, expectSecondEnlistSuccess={4}",
                promote,
                beforePromote,
                firstOptions.ToString(),
                secondOptions.ToString(),
                expectSecondEnlistSuccess
                );

            Trace("**** " + testCaseDescription + " ****");

            AutoResetEvent volCompleted = new AutoResetEvent(false);
            AutoResetEvent vol2Completed = new AutoResetEvent(false);
            AutoResetEvent pspeCompleted = new AutoResetEvent(false);
            MyEnlistment vol = null;
            NonMSDTCPromoterEnlistment pspe = null;

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    if (beforePromote)
                    {
                        vol = new MyEnlistment(
                            volCompleted,
                            true,
                            true,
                            secondOptions,
                            /*expectSuccessfulEnlist=*/ expectSecondEnlistSuccess,
                            vol2Completed);
                        vol.TransactionToEnlist = Transaction.Current;
                        Transaction.Current.EnlistVolatile(vol, firstOptions);
                    }

                    pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1,
                        NonMsdtcPromoterTests.PromotedToken1,
                        pspeCompleted,
                        /*nonMSDTC = */ true,
                        /*tx = */ null,
                        /*spcResponse=*/ TransactionStatus.Committed,
                        /*expectRejection=*/ false
                        );

                    if (promote)
                    {
                        Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1);

                        if (!beforePromote)
                        {
                            vol = new MyEnlistment(
                                volCompleted,
                                true,
                                true,
                                secondOptions,
                                /*expectSuccessfulEnlist=*/ expectSecondEnlistSuccess,
                                vol2Completed);
                            vol.TransactionToEnlist = Transaction.Current;
                            Transaction.Current.EnlistVolatile(vol, firstOptions);
                        }
                    }

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                TestFailed(testCaseDescription, string.Format("Unexpected exception {0}: {1}", ex.GetType().ToString(), ex.ToString()));
                return;
            }

            if (!expectSecondEnlistSuccess)
            {
                vol2Completed.Set();
            }

            if (!volCompleted.WaitOne(TimeSpan.FromSeconds(5)) || !vol2Completed.WaitOne(TimeSpan.FromSeconds(5)) ||
                !pspeCompleted.WaitOne(TimeSpan.FromSeconds(5)))
            {
                TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes");
                return;
            }

            //if (WaitHandle.WaitAll(new WaitHandle[3] { volCompleted, vol2Completed, pspeCompleted }, TimeSpan.FromSeconds(5)))
            //{
            if (vol.AbortedOutcome)
            {
                TestFailed(testCaseDescription, "The volatile enlistment aborted unexpectedly");
            }

            if (!promote && pspe.Promoted)
            {
                TestFailed(testCaseDescription, "The enlistment promoted");
                return;
            }

            if (promote && !pspe.Promoted)
            {
                TestFailed(testCaseDescription, "The enlistment was not promoted");
                return;
            }

            if (pspe.Aborted)
            {
                TestFailed(testCaseDescription, "The pspe aborted unexpectedly");
                return;
            }

            TestPassed();
            //}
            //else
            //{
            //    TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes");
            //}


        }
        public static void TestCase_AbortFromVolatile(bool promote, EnlistmentOptions enlistmentOptions = EnlistmentOptions.None)
        {
            string testCaseDescription = string.Format(
                "TestCase_AbortFromVolatile promote={0}; enlistmentOptions = {1}",
                promote,
                enlistmentOptions.ToString()
                );

            Trace("**** " + testCaseDescription + " ****");

            AutoResetEvent volCompleted = new AutoResetEvent(false);
            AutoResetEvent pspeCompleted = new AutoResetEvent(false);
            MyEnlistment vol = null;
            NonMSDTCPromoterEnlistment pspe = null;

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    vol = CreateVolatileEnlistment(volCompleted, null, enlistmentOptions, false);

                    pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1,
                        NonMsdtcPromoterTests.PromotedToken1,
                        pspeCompleted,
                        /*nonMSDTC = */ true,
                        /*tx = */ null,
                        /*spcResponse=*/ TransactionStatus.Committed,
                        /*expectRejection=*/ false
                        );

                    if (promote)
                    {
                        Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1);
                    }

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                if (ex.GetType() != typeof(TransactionAbortedException))
                {
                    TestFailed(testCaseDescription, string.Format("Unexpected exception {0}: {1}", ex.GetType().ToString(), ex.ToString()));
                    return;
                }
            }

            if (!volCompleted.WaitOne(TimeSpan.FromSeconds(5)) || !pspeCompleted.WaitOne(TimeSpan.FromSeconds(5)))
            {
                TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes");
                return;
            }

            //if (WaitHandle.WaitAll(new WaitHandle[2] { volCompleted, pspeCompleted }, TimeSpan.FromSeconds(5)))
            //{
            if (!vol.AbortedOutcome)
            {
                TestFailed(testCaseDescription, "The volatile enlistment did not abort");
            }

            if (!promote && pspe.Promoted)
            {
                TestFailed(testCaseDescription, "The enlistment promoted");
                return;
            }

            if (promote && !pspe.Promoted)
            {
                TestFailed(testCaseDescription, "The enlistment was not promoted");
                return;
            }

            if (!(pspe.Aborted))
            {
                TestFailed(testCaseDescription, "The enlistment did not abort");
                return;
            }

            TestPassed();
            //}
            //else
            //{
            //    TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes");
            //}
        }
Example #7
0
        public static void TestCase_AbortFromVolatile(bool promote, EnlistmentOptions enlistmentOptions = EnlistmentOptions.None)
        {
            string testCaseDescription = string.Format(
                "TestCase_AbortFromVolatile promote={0}; enlistmentOptions = {1}",
                promote,
                enlistmentOptions.ToString()
                );

            Trace("**** " + testCaseDescription + " ****");

            AutoResetEvent volCompleted = new AutoResetEvent(false);
            AutoResetEvent pspeCompleted = new AutoResetEvent(false);
            MyEnlistment vol = null;
            NonMSDTCPromoterEnlistment pspe = null;

            Assert.Throws<TransactionAbortedException>(() =>
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    vol = CreateVolatileEnlistment(volCompleted, null, enlistmentOptions, false);

                    pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1,
                        NonMsdtcPromoterTests.PromotedToken1,
                        pspeCompleted,
                        /*nonMSDTC = */ true,
                        /*tx = */ null,
                        /*spcResponse=*/ TransactionStatus.Committed,
                        /*expectRejection=*/ false
                        );

                    if (promote)
                    {
                        Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1);
                    }

                    ts.Complete();
                }
            });

            Assert.True(volCompleted.WaitOne(TimeSpan.FromSeconds(5)) && pspeCompleted.WaitOne(TimeSpan.FromSeconds(5)));

            Assert.True(vol.AbortedOutcome);

            if (promote)
            {
                Assert.True(pspe.Promoted);
            }
            else
            {
                Assert.False(pspe.Promoted);
            }

            Assert.True(pspe.Aborted);

            TestPassed();
        }
Example #8
0
        public static void TestCase_EnlistDuringPrepare(bool promote,
            bool beforePromote,
            EnlistmentOptions firstOptions = EnlistmentOptions.None,
            EnlistmentOptions secondOptions = EnlistmentOptions.None,
            bool expectSecondEnlistSuccess = true
            )
        {
            string testCaseDescription = string.Format(
                "TestCase_EnlistDuringPrepare promote={0}; beforePromote={1}, firstOptions={2}, secondOptions={3}, expectSecondEnlistSuccess={4}",
                promote,
                beforePromote,
                firstOptions.ToString(),
                secondOptions.ToString(),
                expectSecondEnlistSuccess
                );

            Trace("**** " + testCaseDescription + " ****");

            AutoResetEvent volCompleted = new AutoResetEvent(false);
            AutoResetEvent vol2Completed = new AutoResetEvent(false);
            AutoResetEvent pspeCompleted = new AutoResetEvent(false);
            MyEnlistment vol = null;
            NonMSDTCPromoterEnlistment pspe = null;

            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    if (beforePromote)
                    {
                        vol = new MyEnlistment(
                            volCompleted,
                            true,
                            true,
                            secondOptions,
                            /*expectSuccessfulEnlist=*/ expectSecondEnlistSuccess,
                            vol2Completed);
                        vol.TransactionToEnlist = Transaction.Current;
                        Transaction.Current.EnlistVolatile(vol, firstOptions);
                    }

                    pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1,
                        NonMsdtcPromoterTests.PromotedToken1,
                        pspeCompleted,
                        /*nonMSDTC = */ true,
                        /*tx = */ null,
                        /*spcResponse=*/ TransactionStatus.Committed,
                        /*expectRejection=*/ false
                        );

                    if (promote)
                    {
                        Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1);

                        if (!beforePromote)
                        {
                            vol = new MyEnlistment(
                                volCompleted,
                                true,
                                true,
                                secondOptions,
                                /*expectSuccessfulEnlist=*/ expectSecondEnlistSuccess,
                                vol2Completed);
                            vol.TransactionToEnlist = Transaction.Current;
                            Transaction.Current.EnlistVolatile(vol, firstOptions);
                        }
                    }

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                Assert.Null(ex);
            }

            if (!expectSecondEnlistSuccess)
            {
                vol2Completed.Set();
            }

            Assert.True(volCompleted.WaitOne(TimeSpan.FromSeconds(5)) && vol2Completed.WaitOne(TimeSpan.FromSeconds(5)) &&
                pspeCompleted.WaitOne(TimeSpan.FromSeconds(5)));

            Assert.False(vol.AbortedOutcome);

            if (promote)
            {
                Assert.True(pspe.Promoted);
            }
            else
            {
                Assert.False(pspe.Promoted);
            }

            Assert.False(pspe.Aborted);

            TestPassed();
        }
Example #9
0
        protected DurableEnlistmentNotification(Guid resourceManager,
                                                EnlistmentOptions enlistmentOptions)
        {
            Trace.WriteIf(Tracing.Is.TraceVerbose, "resourceManager={0} enlistmentOptions={1}".FormatWith(resourceManager, enlistmentOptions.ToString("G")));
            Operation = new Operation(resourceManager);
            if (null == Transaction.Current)
            {
                throw new InvalidOperationException("There is no transaction scope to enlist with.");
            }

            Transaction.Current.EnlistDurable(resourceManager, this, enlistmentOptions);
            Transaction.Current.TransactionCompleted += OnTransactionCompleted;
        }