public byte[] Promote() { Trace("NonMSDTCPromoterEnlistment.Promote"); this.Promoted = true; _distributedTxId = Guid.NewGuid(); if (_failPromote) { Trace("NonMSDTCPromoterEnlistment.Promote - Failing based on configuration"); throw new Exception("Failing promotion based on configuration"); } // invoke this.enlistedTransaction.SetDistributedTransactionIdentifier(this, this.promoterType); via reflection if (_incorrectNotificationObjectToSetDistributedTransactionId) { NonMSDTCPromoterEnlistment incorrectNotificationObject = new NonMSDTCPromoterEnlistment(_promoterType, _promotedToken, _completionEvent); try { SetDistributedTransactionId(incorrectNotificationObject, _enlistedTransaction, _distributedTxId); throw new Exception("SetDistributedTransactionIdentifier did not throw the expected InvalidOperationException"); } catch (TargetInvocationException ex) { if (!(ex.InnerException is InvalidOperationException)) { throw new Exception("SetDistributedTransactionIdentifier did not throw the expected InvalidOperationException"); } } } else { SetDistributedTransactionId(this, _enlistedTransaction, _distributedTxId); } return _promotedToken; }
private static IPromotableSinglePhaseNotification CreatePSPEEnlistment( Guid promoterType, byte[] promotedToken, AutoResetEvent outcomeReceived, bool nonMSDTC = true, Transaction tx = null, TransactionStatus spcResponse = TransactionStatus.Committed, bool expectRejection = false, bool comparePromotedToken = false, bool failInitialize = false, bool failPromote = false, bool failSPC = false, bool failGetPromoterType = false, bool failGetId = false, bool incorrectNotificationObjectToSetDistributedTransactionId = false ) { IPromotableSinglePhaseNotification enlistment = null; Transaction txToEnlist = Transaction.Current; if (tx != null) { txToEnlist = tx; } if (nonMSDTC) { NonMSDTCPromoterEnlistment nonMSDTCEnlistment = new NonMSDTCPromoterEnlistment(promoterType, promotedToken, outcomeReceived, spcResponse, failInitialize, failPromote, failSPC, failGetPromoterType, failGetId, incorrectNotificationObjectToSetDistributedTransactionId); if (nonMSDTCEnlistment.Enlist(txToEnlist, expectRejection, comparePromotedToken)) { enlistment = nonMSDTCEnlistment; } TryProhibitedOperations(txToEnlist, promoterType); } else { throw new Exception("normal PSPE not implemented yet."); } return enlistment; }
public static void TestCase_SetDistributedIdAtWrongTime() { string testCaseDescription = "TestCase_SetDistributedIdAtWrongTime"; string failureDetail = "Was able to call SetDistributedTransacitonIdentifier outside of ITransactionPromoter.Promote call."; Trace("**** " + testCaseDescription + " ****"); AutoResetEvent pspeCompleted = new AutoResetEvent(false); NonMSDTCPromoterEnlistment pspe = null; NonMSDTCPromoterEnlistment dummyPSPE = new NonMSDTCPromoterEnlistment(NonMsdtcPromoterTests.PromoterType1, NonMsdtcPromoterTests.PromotedToken1, pspeCompleted); Guid guidToSet = new Guid("236BC646-FE3B-41F9-99F7-08BF448D8420"); try { using (TransactionScope ts = new TransactionScope()) { Trace("Before EnlistPromotable"); try { SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet); TestFailed(testCaseDescription, failureDetail); } catch (TransactionException ex) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } catch (TargetInvocationException ex) { if (ex.InnerException is TransactionException) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } else { TestFailed(testCaseDescription, string.Format("Unexpected TargetInvocationException InnerException {0}:{1}", ex.InnerException.GetType().ToString(), ex.InnerException.Message)); } } pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1, NonMsdtcPromoterTests.PromotedToken1, pspeCompleted, /*nonMSDTC = */ true, /*tx = */ null, /*spcResponse=*/ TransactionStatus.Committed, /*expectRejection=*/ false, /*comparePromotedToken=*/ false, /*failInitialize=*/ false, /*failPromote=*/ false, /*failSPC=*/ false, /*failGetPromoterType=*/ false, /*failGetId=*/ false ); Trace("After EnlistPromotable"); try { SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet); TestFailed(testCaseDescription, failureDetail); } catch (TransactionException ex) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } catch (TargetInvocationException ex) { if (ex.InnerException is TransactionException) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } else { TestFailed(testCaseDescription, string.Format("Unexpected TargetInvocationException InnerException {0}:{1}", ex.InnerException.GetType().ToString(), ex.InnerException.Message)); } } Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1); Trace("After Promotion"); try { SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet); TestFailed(testCaseDescription, failureDetail); } catch (TransactionException ex) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } catch (TargetInvocationException ex) { if (ex.InnerException is TransactionException) { Trace(string.Format("Caught expected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); } else { TestFailed(testCaseDescription, string.Format("Unexpected TargetInvocationException InnerException {0}:{1}", ex.InnerException.GetType().ToString(), ex.InnerException.Message)); } } ts.Complete(); } } catch (Exception ex) { TestFailed(testCaseDescription, string.Format("Caught unexpected exception {0}:{1}", ex.GetType().ToString(), ex.ToString())); return; } // The NonMSDTCPromoterEnlistment is coded to set "Promoted" at the beginning of Promote, before // throwing. if (pspeCompleted.WaitOne(TimeSpan.FromSeconds(5))) { if (!pspe.Promoted) { TestFailed(testCaseDescription, "The enlistment was not promoted"); return; } } else { TestFailed(testCaseDescription, "Timeout waiting for enlistment outcomes"); } TestPassed(); }
public static void TestCase_SetDistributedIdAtWrongTime() { string testCaseDescription = "TestCase_SetDistributedIdAtWrongTime"; Trace("**** " + testCaseDescription + " ****"); AutoResetEvent pspeCompleted = new AutoResetEvent(false); NonMSDTCPromoterEnlistment pspe = null; NonMSDTCPromoterEnlistment dummyPSPE = new NonMSDTCPromoterEnlistment(NonMsdtcPromoterTests.PromoterType1, NonMsdtcPromoterTests.PromotedToken1, pspeCompleted); Guid guidToSet = new Guid("236BC646-FE3B-41F9-99F7-08BF448D8420"); using (TransactionScope ts = new TransactionScope()) { Trace("Before EnlistPromotable"); Exception ex = Assert.ThrowsAny<Exception>(() => SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet)); Assert.True(ex is TransactionException || (ex is TargetInvocationException && ex.InnerException is TransactionException)); pspe = (NonMSDTCPromoterEnlistment)CreatePSPEEnlistment(NonMsdtcPromoterTests.PromoterType1, NonMsdtcPromoterTests.PromotedToken1, pspeCompleted, /*nonMSDTC = */ true, /*tx = */ null, /*spcResponse=*/ TransactionStatus.Committed, /*expectRejection=*/ false, /*comparePromotedToken=*/ false, /*failInitialize=*/ false, /*failPromote=*/ false, /*failSPC=*/ false, /*failGetPromoterType=*/ false, /*failGetId=*/ false ); Trace("After EnlistPromotable"); ex = Assert.ThrowsAny<Exception>(() => SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet)); Assert.True(ex is TransactionException || (ex is TargetInvocationException && ex.InnerException is TransactionException)); Promote(testCaseDescription, NonMsdtcPromoterTests.PromotedToken1); Trace("After Promotion"); ex = Assert.ThrowsAny<Exception>(() => SetDistributedTransactionId(dummyPSPE, Transaction.Current, guidToSet)); Assert.True(ex is TransactionException || (ex is TargetInvocationException && ex.InnerException is TransactionException)); ts.Complete(); } // The NonMSDTCPromoterEnlistment is coded to set "Promoted" at the beginning of Promote, before // throwing. Assert.True(pspeCompleted.WaitOne(TimeSpan.FromSeconds(5))); Assert.True(pspe.Promoted); TestPassed(); }