2 // Unit tests for TransactionScope and Implicit/Explicit use of
6 // Ankit Jain <JAnkit@novell.com>
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 using NUnit.Framework;
13 using System.Transactions;
15 namespace MonoTests.System.Transactions
18 public class TransactionScopeTest
21 public void TransactionScopeCommit ()
23 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
24 using (TransactionScope scope = new TransactionScope ()) {
25 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
26 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
30 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
34 public void TransactionScopeAbort ()
36 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
37 IntResourceManager irm = new IntResourceManager (1);
38 using (TransactionScope scope = new TransactionScope ()) {
39 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
40 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
43 /* Not completing scope here */
45 irm.Check ( 0, 0, 1, 0, "irm");
46 Assert.AreEqual (1, irm.Value);
47 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
51 [ExpectedException (typeof (InvalidOperationException))]
52 public void TransactionScopeCompleted1 ()
54 using (TransactionScope scope = new TransactionScope ()) {
56 /* Can't access ambient transaction after scope.Complete */
57 TransactionStatus status = Transaction.Current.TransactionInformation.Status;
62 [ExpectedException (typeof (InvalidOperationException))]
63 public void TransactionScopeCompleted2 ()
65 using (TransactionScope scope = new TransactionScope ()) {
67 Transaction.Current = Transaction.Current;
72 [ExpectedException (typeof (InvalidOperationException))]
73 public void TransactionScopeCompleted3 ()
75 using (TransactionScope scope = new TransactionScope ()) {
81 #region NestedTransactionScope tests
83 public void NestedTransactionScope1 ()
85 IntResourceManager irm = new IntResourceManager (1);
87 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
88 using (TransactionScope scope = new TransactionScope ()) {
91 /* Complete this scope */
95 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
96 /* Value = 2, got committed */
97 Assert.AreEqual (irm.Value, 2, "#1");
98 irm.Check ( 1, 1, 0, 0, "irm" );
102 public void NestedTransactionScope2 ()
104 IntResourceManager irm = new IntResourceManager (1);
105 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
106 using (TransactionScope scope = new TransactionScope ()) {
109 /* Not-Completing this scope */
112 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
113 /* Value = 2, got rolledback */
114 Assert.AreEqual (irm.Value, 1, "#2");
115 irm.Check ( 0, 0, 1, 0, "irm" );
119 public void NestedTransactionScope3 ()
121 IntResourceManager irm = new IntResourceManager (1);
122 IntResourceManager irm2 = new IntResourceManager (10);
124 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
125 using (TransactionScope scope = new TransactionScope ()) {
128 using (TransactionScope scope2 = new TransactionScope ()) {
137 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
138 /* Both got committed */
139 Assert.AreEqual (irm.Value, 2, "#3");
140 Assert.AreEqual (irm2.Value, 20, "#4");
141 irm.Check ( 1, 1, 0, 0, "irm" );
142 irm2.Check ( 1, 1, 0, 0, "irm2" );
146 public void NestedTransactionScope4 ()
148 IntResourceManager irm = new IntResourceManager (1);
149 IntResourceManager irm2 = new IntResourceManager (10);
151 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
152 using (TransactionScope scope = new TransactionScope ()) {
155 using (TransactionScope scope2 = new TransactionScope ()) {
158 /* Inner Tx not completed, Tx should get rolled back */
161 /* Both rolledback */
162 irm.Check ( 0, 0, 1, 0, "irm" );
163 irm2.Check ( 0, 0, 1, 0, "irm2" );
164 Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
168 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
170 Assert.AreEqual (irm.Value, 1, "#6");
171 Assert.AreEqual (irm2.Value, 10, "#7");
172 irm.Check ( 0, 0, 1, 0, "irm" );
176 public void NestedTransactionScope5 ()
178 IntResourceManager irm = new IntResourceManager (1);
179 IntResourceManager irm2 = new IntResourceManager (10);
181 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
182 using (TransactionScope scope = new TransactionScope ()) {
185 using (TransactionScope scope2 = new TransactionScope ()) {
190 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
191 /* Not completing outer scope
192 scope.Complete (); */
195 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
197 Assert.AreEqual (irm.Value, 1, "#9");
198 Assert.AreEqual (irm2.Value, 10, "#10");
199 irm.Check ( 0, 0, 1, 0, "irm" );
200 irm2.Check ( 0, 0, 1, 0, "irm2" );
204 public void NestedTransactionScope6 ()
206 IntResourceManager irm = new IntResourceManager (1);
207 IntResourceManager irm2 = new IntResourceManager (10);
209 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
210 using (TransactionScope scope = new TransactionScope ()) {
213 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
218 irm2.Check ( 1, 1, 0, 0, "irm2" );
219 Assert.AreEqual (irm2.Value, 20);
221 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
226 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
227 Assert.AreEqual (irm.Value, 2, "#12");
228 irm.Check ( 1, 1, 0, 0, "irm" );
232 public void NestedTransactionScope7 ()
234 IntResourceManager irm = new IntResourceManager (1);
235 IntResourceManager irm2 = new IntResourceManager (10);
237 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
238 using (TransactionScope scope = new TransactionScope ()) {
241 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
247 /* irm2, rolled back*/
248 irm2.Check ( 0, 0, 1, 0, "irm2" );
249 Assert.AreEqual (irm2.Value, 10, "#13");
251 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
256 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
257 /* ..But irm got committed */
258 Assert.AreEqual (irm.Value, 2, "#15");
259 irm.Check ( 1, 1, 0, 0, "irm" );
263 public void NestedTransactionScope8 ()
265 IntResourceManager irm = new IntResourceManager (1);
266 IntResourceManager irm2 = new IntResourceManager (10);
268 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
269 using (TransactionScope scope = new TransactionScope ()) {
272 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
273 /* Not transactional, so this WONT get committed */
277 irm2.Check ( 0, 0, 0, 0, "irm2" );
278 Assert.AreEqual (20, irm2.Value, "#16");
279 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
284 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
285 Assert.AreEqual (irm.Value, 2, "#18");
286 irm.Check ( 1, 1, 0, 0, "irm" );
290 public void NestedTransactionScope8a ()
292 IntResourceManager irm = new IntResourceManager ( 1 );
293 IntResourceManager irm2 = new IntResourceManager ( 10 );
295 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
296 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
299 using (TransactionScope scope2 = new TransactionScope ()) {
303 irm2.Check ( 1, 1, 0, 0, "irm2" );
304 Assert.AreEqual ( 20, irm2.Value, "#16a" );
309 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
310 Assert.AreEqual ( 2, irm.Value, "#18a" );
311 irm.Check ( 0, 0, 0, 0, "irm" );
315 public void NestedTransactionScope9 ()
317 IntResourceManager irm = new IntResourceManager (1);
318 IntResourceManager irm2 = new IntResourceManager (10);
320 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
321 using (TransactionScope scope = new TransactionScope ()) {
324 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
325 /* Not transactional, so this WONT get committed */
329 irm2.Check ( 0, 0, 0, 0, "irm2" );
331 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
336 /* vr's value has changed as the inner scope committed = 6 */
337 irm.Check ( 1, 1, 0, 0, "irm" );
338 Assert.AreEqual (irm.Value, 6, "#19");
339 Assert.AreEqual (irm.Actual, 6, "#20");
340 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
345 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
346 Assert.AreEqual (irm.Value, 6, "#22");
347 irm.Check ( 2, 2, 0, 0, "irm" );
351 [ExpectedException (typeof (TransactionAbortedException))]
352 public void NestedTransactionScope10 ()
354 IntResourceManager irm = new IntResourceManager (1);
357 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
358 using (TransactionScope scope = new TransactionScope ()) {
361 using (TransactionScope scope2 = new TransactionScope ()) {
363 /* Not completing this, so the transaction will
365 scope2.Complete (); */
368 using (TransactionScope scope3 = new TransactionScope ()) {
369 /* Aborted transaction cannot be used for another
372 //Assert.Fail ("Should not reach here.");
376 Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
380 public void NestedTransactionScope12 ()
382 IntResourceManager irm = new IntResourceManager (1);
384 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
385 using (TransactionScope scope = new TransactionScope ()) {
388 using (TransactionScope scope2 = new TransactionScope ()) {
390 /* Not completing this, so the transaction will
392 scope2.Complete (); */
395 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
396 /* Using RequiresNew here, so outer transaction
397 * being aborted doesn't matter
405 [ExpectedException (typeof (TransactionAbortedException))]
406 public void NestedTransactionScope13 ()
408 IntResourceManager irm = new IntResourceManager ( 1 );
410 Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
411 using ( TransactionScope scope = new TransactionScope () ) {
414 using ( TransactionScope scope2 = new TransactionScope () ) {
416 /* Not completing this, so the transaction will
418 scope2.Complete (); */
426 /* Tests using IntResourceManager */
429 public void RMFail1 ()
431 IntResourceManager irm = new IntResourceManager (1);
432 IntResourceManager irm2 = new IntResourceManager (10);
433 IntResourceManager irm3 = new IntResourceManager (12);
435 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
437 using (TransactionScope scope = new TransactionScope ()) {
442 /* Make second RM fail to prepare, this should throw
443 * TransactionAbortedException when the scope ends
445 irm2.FailPrepare = true;
448 } catch (TransactionAbortedException) {
449 irm.Check ( 1, 0, 1, 0, "irm");
450 irm2.Check ( 1, 0, 0, 0, "irm2");
451 irm3.Check ( 0, 0, 1, 0, "irm3");
453 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
457 [Category("NotWorking")]
458 [Ignore("NotWorking")]
459 public void RMFail2 ()
461 IntResourceManager irm = new IntResourceManager (1);
462 IntResourceManager irm2 = new IntResourceManager (10);
463 IntResourceManager irm3 = new IntResourceManager (12);
465 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
467 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
472 /* irm2 wont call Prepared or ForceRollback in
473 * its Prepare (), so TransactionManager will timeout
476 irm2.IgnorePrepare = true;
479 } catch (TransactionAbortedException e) {
480 /* FIXME: Not working right now.. no timeout exception thrown! */
482 Assert.IsNotNull ( e.InnerException, "innerexception is null" );
483 Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
485 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
489 Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
492 #region Explicit Transaction Tests
495 public void ExplicitTransactionCommit ()
497 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
499 CommittableTransaction ct = new CommittableTransaction ();
500 Transaction oldTransaction = Transaction.Current;
501 Transaction.Current = ct;
503 IntResourceManager irm = new IntResourceManager (1);
507 Assert.AreEqual (2, irm.Value, "#33");
508 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
509 Transaction.Current = oldTransaction;
513 public void ExplicitTransactionRollback ()
515 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
517 CommittableTransaction ct = new CommittableTransaction ();
518 Transaction oldTransaction = Transaction.Current;
519 Transaction.Current = ct;
521 IntResourceManager irm = new IntResourceManager (1);
523 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
526 Assert.AreEqual (1, irm.Value, "#36");
527 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
528 Transaction.Current = oldTransaction;
532 public void ExplicitTransaction1 ()
534 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
535 CommittableTransaction ct = new CommittableTransaction ();
536 Transaction oldTransaction = Transaction.Current;
538 Transaction.Current = ct;
540 IntResourceManager irm = new IntResourceManager (1);
544 using (TransactionScope scope = new TransactionScope ()) {
545 Assert.AreEqual (ct, Transaction.Current, "#38");
550 Assert.AreEqual (ct, Transaction.Current, "#39");
551 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
552 Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
555 Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
556 Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
557 Transaction.Current = oldTransaction;
561 public void ExplicitTransaction2 ()
563 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
564 CommittableTransaction ct = new CommittableTransaction ();
565 Transaction oldTransaction = Transaction.Current;
567 Transaction.Current = ct;
569 IntResourceManager irm = new IntResourceManager (1);
572 using (TransactionScope scope = new TransactionScope ()) {
573 Assert.AreEqual (ct, Transaction.Current, "#44");
575 /* Not calling scope.Complete
579 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
580 Assert.AreEqual (ct, Transaction.Current, "#46");
581 Assert.AreEqual (1, irm.Actual, "#47");
582 Assert.AreEqual (1, irm.NumRollback, "#48");
583 irm.Check ( 0, 0, 1, 0, "irm" );
584 Transaction.Current = oldTransaction;
588 } catch (TransactionAbortedException) {
591 Assert.Fail ("Commit on an aborted transaction should fail");
595 public void ExplicitTransaction3 ()
597 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
598 CommittableTransaction ct = new CommittableTransaction ();
599 Transaction oldTransaction = Transaction.Current;
601 Transaction.Current = ct;
603 IntResourceManager irm = new IntResourceManager (1);
605 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
606 Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
614 Assert.AreEqual (3, irm.Actual, "#50");
616 Assert.AreEqual (ct, Transaction.Current, "#51");
618 Assert.AreEqual (2, irm.Actual, "#52");
619 Transaction.Current = oldTransaction;
623 public void ExplicitTransaction4 ()
625 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
626 CommittableTransaction ct = new CommittableTransaction ();
627 Transaction oldTransaction = Transaction.Current;
629 /* Not setting ambient transaction
630 Transaction.Current = ct;
633 IntResourceManager irm = new IntResourceManager (1);
635 using (TransactionScope scope = new TransactionScope (ct)) {
636 Assert.AreEqual (ct, Transaction.Current, "#53");
642 Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
643 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
644 Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
647 Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
648 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
650 irm.Check ( 1, 1, 0, 0, "irm");
654 public void ExplicitTransaction5 ()
656 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
657 CommittableTransaction ct = new CommittableTransaction ();
658 Transaction oldTransaction = Transaction.Current;
660 /* Not setting ambient transaction
661 Transaction.Current = ct;
664 IntResourceManager irm = new IntResourceManager (1);
666 using (TransactionScope scope = new TransactionScope (ct)) {
667 Assert.AreEqual (ct, Transaction.Current, "#59");
671 /* Not completing this scope
672 scope.Complete (); */
675 Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
676 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
677 Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
679 irm.Check ( 0, 0, 1, 0, "irm");
683 [ExpectedException (typeof (InvalidOperationException))]
684 public void ExplicitTransaction6 ()
686 CommittableTransaction ct = new CommittableTransaction ();
688 IntResourceManager irm = new IntResourceManager (1);
696 [ExpectedException (typeof (InvalidOperationException))]
697 public void ExplicitTransaction6a ()
699 CommittableTransaction ct = new CommittableTransaction ();
701 IntResourceManager irm = new IntResourceManager ( 1 );
705 /* Using a already committed transaction in a new
708 TransactionScope scope = new TransactionScope ( ct );
712 public void ExplicitTransaction6b ()
714 CommittableTransaction ct = new CommittableTransaction ();
716 IntResourceManager irm = new IntResourceManager ( 1 );
718 Transaction.Current = ct;
720 TransactionScope scope1 = new TransactionScope ();
728 } catch (TransactionAbortedException) {
729 irm.Check ( 0, 0, 1, 0, "irm" );
732 Transaction.Current = null;
735 Assert.Fail ( "Commit should've failed" );
739 public void ExplicitTransaction6c ()
741 CommittableTransaction ct = new CommittableTransaction ();
743 IntResourceManager irm = new IntResourceManager ( 1 );
745 Transaction.Current = ct;
747 TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
751 TransactionScope scope2 = new TransactionScope ();
754 } catch (InvalidOperationException) {
755 /* Error: TransactionScope nested incorrectly */
756 irm.Check ( 0, 0, 1, 0, "irm" );
758 Transaction.Current = null;
762 Assert.Fail ("Commit should've failed");
766 public void ExplicitTransaction6d ()
768 CommittableTransaction ct = new CommittableTransaction ();
770 IntResourceManager irm = new IntResourceManager ( 1 );
772 Transaction.Current = ct;
774 TransactionScope scope1 = new TransactionScope ();
778 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
781 } catch (InvalidOperationException) {
782 /* Error: TransactionScope nested incorrectly */
784 Transaction.Current = null;
788 Assert.Fail ( "Commit should've failed" );
792 public void ExplicitTransaction6e ()
794 CommittableTransaction ct = new CommittableTransaction ();
796 IntResourceManager irm = new IntResourceManager ( 1 );
798 Transaction.Current = ct;
800 TransactionScope scope1 = new TransactionScope ();
804 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
807 } catch (InvalidOperationException) {
808 /* Error: TransactionScope nested incorrectly */
810 Transaction.Current = null;
814 Assert.Fail ( "Commit should've failed" );
818 [ExpectedException (typeof (TransactionException))]
819 public void ExplicitTransaction7 ()
821 CommittableTransaction ct = new CommittableTransaction ();
823 IntResourceManager irm = new IntResourceManager (1);
826 /* Cannot accept any new work now, so TransactionException */
831 public void ExplicitTransaction8 ()
833 CommittableTransaction ct = new CommittableTransaction ();
835 IntResourceManager irm = new IntResourceManager ( 1 );
836 using ( TransactionScope scope = new TransactionScope (ct) ) {
838 /* FIXME: Why TransactionAbortedException ?? */
841 } catch ( TransactionAbortedException) {
842 irm.Check ( 0, 0, 1, 0, "irm" );
845 Assert.Fail ( "Should not be reached" );
850 public void ExplicitTransaction8a ()
852 CommittableTransaction ct = new CommittableTransaction ();
854 IntResourceManager irm = new IntResourceManager ( 1 );
855 using ( TransactionScope scope = new TransactionScope ( ct ) ) {
858 /* FIXME: Why TransactionAbortedException ?? */
862 catch ( TransactionAbortedException) {
863 irm.Check ( 0, 0, 1, 0, "irm" );
866 Assert.Fail ( "Should not be reached" );
871 [ExpectedException (typeof (InvalidOperationException))]
872 public void ExplicitTransaction9 ()
874 CommittableTransaction ct = new CommittableTransaction ();
876 IntResourceManager irm = new IntResourceManager ( 1 );
877 ct.BeginCommit ( null, null );
878 ct.BeginCommit ( null, null );
882 public void ExplicitTransaction10 ()
884 CommittableTransaction ct = new CommittableTransaction ();
886 IntResourceManager irm = new IntResourceManager ( 1 );
887 Transaction.Current = ct;
890 TransactionScope scope = new TransactionScope ( ct );
891 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
896 } catch ( TransactionAbortedException) {
897 irm.Check ( 0, 0, 1, 0, "irm" );
898 Transaction.Current = null;
902 Transaction.Current = null;
903 Assert.Fail ("Expected TransactionAbortedException");
907 public void ExplicitTransaction10a ()
909 CommittableTransaction ct = new CommittableTransaction ();
911 IntResourceManager irm = new IntResourceManager ( 1 );
912 Transaction.Current = ct;
914 Transaction.Current = null;
916 TransactionScope scope = new TransactionScope ( ct );
917 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
918 Transaction.Current = null;
919 //scope2.Complete ();
924 catch ( TransactionAbortedException) {
925 irm.Check ( 0, 0, 1, 0, "irm" );
926 Transaction.Current = null;
930 Transaction.Current = null;
935 public void ExplicitTransaction10b ()
937 CommittableTransaction ct = new CommittableTransaction ();
939 IntResourceManager irm = new IntResourceManager ( 1 );
940 Transaction.Current = ct;
942 Transaction.Current = null;
944 TransactionScope scope = new TransactionScope ( ct );
945 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
946 //scope2.Complete ();
948 IAsyncResult ar = ct.BeginCommit ( null, null );
952 catch ( TransactionAbortedException) {
953 irm.Check ( 0, 0, 1, 0, "irm" );
954 Transaction.Current = null;
958 Transaction.Current = null;
963 [ExpectedException ( typeof (ArgumentException))]
964 public void ExplicitTransaction12 ()
966 CommittableTransaction ct = new CommittableTransaction ();
968 IntResourceManager irm = new IntResourceManager ( 1 );
969 irm.FailPrepare = true;
970 ct.BeginCommit ( null, null );
971 ct.EndCommit ( null );
975 public void ExplicitTransaction13 ()
977 CommittableTransaction ct = new CommittableTransaction ();
978 IntResourceManager irm = new IntResourceManager ( 1 );
980 Assert.IsNull ( Transaction.Current );
981 Transaction.Current = ct;
983 irm.FailPrepare = true;
987 } catch ( TransactionAbortedException ) {
988 Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
990 ct.BeginCommit ( null, null );
991 } catch (Exception) {
992 Transaction.Current = null;
995 Assert.Fail ( "Should not be reached(2)" );
997 Assert.Fail ("Should not be reached");
1001 public void ExplicitTransaction14 ()
1003 CommittableTransaction ct = new CommittableTransaction ();
1004 IntResourceManager irm = new IntResourceManager ( 1 );
1006 Assert.IsNull ( Transaction.Current );
1007 Transaction.Current = ct;
1012 Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
1014 ct.BeginCommit ( null, null );
1016 catch ( Exception) {
1017 Transaction.Current = null;
1020 Assert.Fail ( "Should not be reached" );
1024 public void ExplicitTransaction15 ()
1026 CommittableTransaction ct = new CommittableTransaction ();
1027 IntResourceManager irm = new IntResourceManager ( 1 );
1028 IntResourceManager irm2 = new IntResourceManager ( 3 );
1030 Assert.IsNull ( Transaction.Current );
1031 Transaction.Current = ct;
1034 using (TransactionScope scope = new TransactionScope ()) {
1036 Transaction.Current = new CommittableTransaction ();
1039 } catch (InvalidOperationException) {
1040 irm.Check ( 0, 0, 1, 0, "irm" );
1041 irm2.Check ( 0, 0, 1, 0, "irm2" );
1042 Transaction.Current = null;
1046 Assert.Fail ( "Should not be reached" );
1050 public void ExplicitTransaction16 ()
1052 CommittableTransaction ct = new CommittableTransaction ();
1053 IntResourceManager irm0 = new IntResourceManager ( 3 );
1054 IntResourceManager irm = new IntResourceManager ( 1 );
1056 Assert.IsNull ( Transaction.Current );
1058 Transaction.Current = ct;
1060 irm.FailPrepare = true;
1061 irm.FailWithException = true;
1067 } catch (TransactionAbortedException e) {
1068 Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
1069 Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
1070 irm.Check ( 1, 0, 0, 0, "irm" );
1071 irm0.Check ( 0, 0, 1, 0, "irm0" );
1072 Transaction.Current = null;
1076 Assert.Fail ( "Should not be reached" );