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 public void RMFail2 ()
459 IntResourceManager irm = new IntResourceManager (1);
460 IntResourceManager irm2 = new IntResourceManager (10);
461 IntResourceManager irm3 = new IntResourceManager (12);
463 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
465 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
470 /* irm2 wont call Prepared or ForceRollback in
471 * its Prepare (), so TransactionManager will timeout
474 irm2.IgnorePrepare = true;
477 } catch (TransactionAbortedException e) {
478 /* FIXME: Not working right now.. no timeout exception thrown! */
480 Assert.IsNotNull ( e.InnerException, "innerexception is null" );
481 Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
483 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
487 Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
490 #region Explicit Transaction Tests
493 public void ExplicitTransactionCommit ()
495 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
497 CommittableTransaction ct = new CommittableTransaction ();
498 Transaction oldTransaction = Transaction.Current;
499 Transaction.Current = ct;
501 IntResourceManager irm = new IntResourceManager (1);
505 Assert.AreEqual (2, irm.Value, "#33");
506 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
507 Transaction.Current = oldTransaction;
511 public void ExplicitTransactionRollback ()
513 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
515 CommittableTransaction ct = new CommittableTransaction ();
516 Transaction oldTransaction = Transaction.Current;
517 Transaction.Current = ct;
519 IntResourceManager irm = new IntResourceManager (1);
521 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
524 Assert.AreEqual (1, irm.Value, "#36");
525 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
526 Transaction.Current = oldTransaction;
530 public void ExplicitTransaction1 ()
532 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
533 CommittableTransaction ct = new CommittableTransaction ();
534 Transaction oldTransaction = Transaction.Current;
536 Transaction.Current = ct;
538 IntResourceManager irm = new IntResourceManager (1);
542 using (TransactionScope scope = new TransactionScope ()) {
543 Assert.AreEqual (ct, Transaction.Current, "#38");
548 Assert.AreEqual (ct, Transaction.Current, "#39");
549 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
550 Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
553 Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
554 Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
555 Transaction.Current = oldTransaction;
559 public void ExplicitTransaction2 ()
561 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
562 CommittableTransaction ct = new CommittableTransaction ();
563 Transaction oldTransaction = Transaction.Current;
565 Transaction.Current = ct;
567 IntResourceManager irm = new IntResourceManager (1);
570 using (TransactionScope scope = new TransactionScope ()) {
571 Assert.AreEqual (ct, Transaction.Current, "#44");
573 /* Not calling scope.Complete
577 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
578 Assert.AreEqual (ct, Transaction.Current, "#46");
579 Assert.AreEqual (1, irm.Actual, "#47");
580 Assert.AreEqual (1, irm.NumRollback, "#48");
581 irm.Check ( 0, 0, 1, 0, "irm" );
582 Transaction.Current = oldTransaction;
586 } catch (TransactionAbortedException) {
589 Assert.Fail ("Commit on an aborted transaction should fail");
593 public void ExplicitTransaction3 ()
595 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
596 CommittableTransaction ct = new CommittableTransaction ();
597 Transaction oldTransaction = Transaction.Current;
599 Transaction.Current = ct;
601 IntResourceManager irm = new IntResourceManager (1);
603 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
604 Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
612 Assert.AreEqual (3, irm.Actual, "#50");
614 Assert.AreEqual (ct, Transaction.Current, "#51");
616 Assert.AreEqual (2, irm.Actual, "#52");
617 Transaction.Current = oldTransaction;
621 public void ExplicitTransaction4 ()
623 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
624 CommittableTransaction ct = new CommittableTransaction ();
625 Transaction oldTransaction = Transaction.Current;
627 /* Not setting ambient transaction
628 Transaction.Current = ct;
631 IntResourceManager irm = new IntResourceManager (1);
633 using (TransactionScope scope = new TransactionScope (ct)) {
634 Assert.AreEqual (ct, Transaction.Current, "#53");
640 Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
641 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
642 Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
645 Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
646 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
648 irm.Check ( 1, 1, 0, 0, "irm");
652 public void ExplicitTransaction5 ()
654 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
655 CommittableTransaction ct = new CommittableTransaction ();
656 Transaction oldTransaction = Transaction.Current;
658 /* Not setting ambient transaction
659 Transaction.Current = ct;
662 IntResourceManager irm = new IntResourceManager (1);
664 using (TransactionScope scope = new TransactionScope (ct)) {
665 Assert.AreEqual (ct, Transaction.Current, "#59");
669 /* Not completing this scope
670 scope.Complete (); */
673 Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
674 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
675 Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
677 irm.Check ( 0, 0, 1, 0, "irm");
681 [ExpectedException (typeof (InvalidOperationException))]
682 public void ExplicitTransaction6 ()
684 CommittableTransaction ct = new CommittableTransaction ();
686 IntResourceManager irm = new IntResourceManager (1);
694 [ExpectedException (typeof (InvalidOperationException))]
695 public void ExplicitTransaction6a ()
697 CommittableTransaction ct = new CommittableTransaction ();
699 IntResourceManager irm = new IntResourceManager ( 1 );
703 /* Using a already committed transaction in a new
706 TransactionScope scope = new TransactionScope ( ct );
710 public void ExplicitTransaction6b ()
712 CommittableTransaction ct = new CommittableTransaction ();
714 IntResourceManager irm = new IntResourceManager ( 1 );
716 Transaction.Current = ct;
718 TransactionScope scope1 = new TransactionScope ();
726 } catch (TransactionAbortedException) {
727 irm.Check ( 0, 0, 1, 0, "irm" );
730 Transaction.Current = null;
733 Assert.Fail ( "Commit should've failed" );
737 public void ExplicitTransaction6c ()
739 CommittableTransaction ct = new CommittableTransaction ();
741 IntResourceManager irm = new IntResourceManager ( 1 );
743 Transaction.Current = ct;
745 TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
749 TransactionScope scope2 = new TransactionScope ();
752 } catch (InvalidOperationException) {
753 /* Error: TransactionScope nested incorrectly */
754 irm.Check ( 0, 0, 1, 0, "irm" );
756 Transaction.Current = null;
760 Assert.Fail ("Commit should've failed");
764 public void ExplicitTransaction6d ()
766 CommittableTransaction ct = new CommittableTransaction ();
768 IntResourceManager irm = new IntResourceManager ( 1 );
770 Transaction.Current = ct;
772 TransactionScope scope1 = new TransactionScope ();
776 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
779 } catch (InvalidOperationException) {
780 /* Error: TransactionScope nested incorrectly */
782 Transaction.Current = null;
786 Assert.Fail ( "Commit should've failed" );
790 public void ExplicitTransaction6e ()
792 CommittableTransaction ct = new CommittableTransaction ();
794 IntResourceManager irm = new IntResourceManager ( 1 );
796 Transaction.Current = ct;
798 TransactionScope scope1 = new TransactionScope ();
802 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
805 } catch (InvalidOperationException) {
806 /* Error: TransactionScope nested incorrectly */
808 Transaction.Current = null;
812 Assert.Fail ( "Commit should've failed" );
816 [ExpectedException (typeof (TransactionException))]
817 public void ExplicitTransaction7 ()
819 CommittableTransaction ct = new CommittableTransaction ();
821 IntResourceManager irm = new IntResourceManager (1);
824 /* Cannot accept any new work now, so TransactionException */
829 public void ExplicitTransaction8 ()
831 CommittableTransaction ct = new CommittableTransaction ();
833 IntResourceManager irm = new IntResourceManager ( 1 );
834 using ( TransactionScope scope = new TransactionScope (ct) ) {
836 /* FIXME: Why TransactionAbortedException ?? */
839 } catch ( TransactionAbortedException) {
840 irm.Check ( 0, 0, 1, 0, "irm" );
843 Assert.Fail ( "Should not be reached" );
848 public void ExplicitTransaction8a ()
850 CommittableTransaction ct = new CommittableTransaction ();
852 IntResourceManager irm = new IntResourceManager ( 1 );
853 using ( TransactionScope scope = new TransactionScope ( ct ) ) {
856 /* FIXME: Why TransactionAbortedException ?? */
860 catch ( TransactionAbortedException) {
861 irm.Check ( 0, 0, 1, 0, "irm" );
864 Assert.Fail ( "Should not be reached" );
869 [ExpectedException (typeof (InvalidOperationException))]
870 public void ExplicitTransaction9 ()
872 CommittableTransaction ct = new CommittableTransaction ();
874 IntResourceManager irm = new IntResourceManager ( 1 );
875 ct.BeginCommit ( null, null );
876 ct.BeginCommit ( null, null );
880 public void ExplicitTransaction10 ()
882 CommittableTransaction ct = new CommittableTransaction ();
884 IntResourceManager irm = new IntResourceManager ( 1 );
885 Transaction.Current = ct;
888 TransactionScope scope = new TransactionScope ( ct );
889 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
894 } catch ( TransactionAbortedException) {
895 irm.Check ( 0, 0, 1, 0, "irm" );
896 Transaction.Current = null;
900 Transaction.Current = null;
901 Assert.Fail ("Expected TransactionAbortedException");
905 public void ExplicitTransaction10a ()
907 CommittableTransaction ct = new CommittableTransaction ();
909 IntResourceManager irm = new IntResourceManager ( 1 );
910 Transaction.Current = ct;
912 Transaction.Current = null;
914 TransactionScope scope = new TransactionScope ( ct );
915 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
916 Transaction.Current = null;
917 //scope2.Complete ();
922 catch ( TransactionAbortedException) {
923 irm.Check ( 0, 0, 1, 0, "irm" );
924 Transaction.Current = null;
928 Transaction.Current = null;
933 public void ExplicitTransaction10b ()
935 CommittableTransaction ct = new CommittableTransaction ();
937 IntResourceManager irm = new IntResourceManager ( 1 );
938 Transaction.Current = ct;
940 Transaction.Current = null;
942 TransactionScope scope = new TransactionScope ( ct );
943 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
944 //scope2.Complete ();
946 IAsyncResult ar = ct.BeginCommit ( null, null );
950 catch ( TransactionAbortedException) {
951 irm.Check ( 0, 0, 1, 0, "irm" );
952 Transaction.Current = null;
956 Transaction.Current = null;
961 [ExpectedException ( typeof (ArgumentException))]
962 public void ExplicitTransaction12 ()
964 CommittableTransaction ct = new CommittableTransaction ();
966 IntResourceManager irm = new IntResourceManager ( 1 );
967 irm.FailPrepare = true;
968 ct.BeginCommit ( null, null );
969 ct.EndCommit ( null );
973 public void ExplicitTransaction13 ()
975 CommittableTransaction ct = new CommittableTransaction ();
976 IntResourceManager irm = new IntResourceManager ( 1 );
978 Assert.IsNull ( Transaction.Current );
979 Transaction.Current = ct;
981 irm.FailPrepare = true;
985 } catch ( TransactionAbortedException ) {
986 Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
988 ct.BeginCommit ( null, null );
989 } catch (Exception) {
990 Transaction.Current = null;
993 Assert.Fail ( "Should not be reached(2)" );
995 Assert.Fail ("Should not be reached");
999 public void ExplicitTransaction14 ()
1001 CommittableTransaction ct = new CommittableTransaction ();
1002 IntResourceManager irm = new IntResourceManager ( 1 );
1004 Assert.IsNull ( Transaction.Current );
1005 Transaction.Current = ct;
1010 Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
1012 ct.BeginCommit ( null, null );
1014 catch ( Exception) {
1015 Transaction.Current = null;
1018 Assert.Fail ( "Should not be reached" );
1022 public void ExplicitTransaction15 ()
1024 CommittableTransaction ct = new CommittableTransaction ();
1025 IntResourceManager irm = new IntResourceManager ( 1 );
1026 IntResourceManager irm2 = new IntResourceManager ( 3 );
1028 Assert.IsNull ( Transaction.Current );
1029 Transaction.Current = ct;
1032 using (TransactionScope scope = new TransactionScope ()) {
1034 Transaction.Current = new CommittableTransaction ();
1037 } catch (InvalidOperationException) {
1038 irm.Check ( 0, 0, 1, 0, "irm" );
1039 irm2.Check ( 0, 0, 1, 0, "irm2" );
1040 Transaction.Current = null;
1044 Assert.Fail ( "Should not be reached" );
1048 public void ExplicitTransaction16 ()
1050 CommittableTransaction ct = new CommittableTransaction ();
1051 IntResourceManager irm0 = new IntResourceManager ( 3 );
1052 IntResourceManager irm = new IntResourceManager ( 1 );
1054 Assert.IsNull ( Transaction.Current );
1056 Transaction.Current = ct;
1058 irm.FailPrepare = true;
1059 irm.FailWithException = true;
1065 } catch (TransactionAbortedException e) {
1066 Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
1067 Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
1068 irm.Check ( 1, 0, 0, 0, "irm" );
1069 irm0.Check ( 0, 0, 1, 0, "irm0" );
1070 Transaction.Current = null;
1074 Assert.Fail ( "Should not be reached" );