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
22 public void TransactionScopeCommit ()
24 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
25 using (TransactionScope scope = new TransactionScope ()) {
26 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
27 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
31 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
35 public void TransactionScopeAbort ()
37 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
38 IntResourceManager irm = new IntResourceManager (1);
39 using (TransactionScope scope = new TransactionScope ()) {
40 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
41 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
44 /* Not completing scope here */
46 irm.Check ( 0, 0, 1, 0, "irm");
47 Assert.AreEqual (1, irm.Value);
48 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
52 [ExpectedException (typeof (InvalidOperationException))]
53 public void TransactionScopeCompleted1 ()
55 using (TransactionScope scope = new TransactionScope ()) {
57 /* Can't access ambient transaction after scope.Complete */
58 TransactionStatus status = Transaction.Current.TransactionInformation.Status;
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void TransactionScopeCompleted2 ()
66 using (TransactionScope scope = new TransactionScope ()) {
68 Transaction.Current = Transaction.Current;
73 [ExpectedException (typeof (InvalidOperationException))]
74 public void TransactionScopeCompleted3 ()
76 using (TransactionScope scope = new TransactionScope ()) {
82 #region NestedTransactionScope tests
84 public void NestedTransactionScope1 ()
86 IntResourceManager irm = new IntResourceManager (1);
88 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
89 using (TransactionScope scope = new TransactionScope ()) {
92 /* Complete this scope */
96 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
97 /* Value = 2, got committed */
98 Assert.AreEqual (irm.Value, 2, "#1");
99 irm.Check ( 1, 1, 0, 0, "irm" );
103 public void NestedTransactionScope2 ()
105 IntResourceManager irm = new IntResourceManager (1);
106 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
107 using (TransactionScope scope = new TransactionScope ()) {
110 /* Not-Completing this scope */
113 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
114 /* Value = 2, got rolledback */
115 Assert.AreEqual (irm.Value, 1, "#2");
116 irm.Check ( 0, 0, 1, 0, "irm" );
120 public void NestedTransactionScope3 ()
122 IntResourceManager irm = new IntResourceManager (1);
123 IntResourceManager irm2 = new IntResourceManager (10);
125 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
126 using (TransactionScope scope = new TransactionScope ()) {
129 using (TransactionScope scope2 = new TransactionScope ()) {
138 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
139 /* Both got committed */
140 Assert.AreEqual (irm.Value, 2, "#3");
141 Assert.AreEqual (irm2.Value, 20, "#4");
142 irm.Check ( 1, 1, 0, 0, "irm" );
143 irm2.Check ( 1, 1, 0, 0, "irm2" );
147 public void NestedTransactionScope4 ()
149 IntResourceManager irm = new IntResourceManager (1);
150 IntResourceManager irm2 = new IntResourceManager (10);
152 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
153 using (TransactionScope scope = new TransactionScope ()) {
156 using (TransactionScope scope2 = new TransactionScope ()) {
159 /* Inner Tx not completed, Tx should get rolled back */
162 /* Both rolledback */
163 irm.Check ( 0, 0, 1, 0, "irm" );
164 irm2.Check ( 0, 0, 1, 0, "irm2" );
165 Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
169 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
171 Assert.AreEqual (irm.Value, 1, "#6");
172 Assert.AreEqual (irm2.Value, 10, "#7");
173 irm.Check ( 0, 0, 1, 0, "irm" );
177 public void NestedTransactionScope5 ()
179 IntResourceManager irm = new IntResourceManager (1);
180 IntResourceManager irm2 = new IntResourceManager (10);
182 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
183 using (TransactionScope scope = new TransactionScope ()) {
186 using (TransactionScope scope2 = new TransactionScope ()) {
191 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
192 /* Not completing outer scope
193 scope.Complete (); */
196 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
198 Assert.AreEqual (irm.Value, 1, "#9");
199 Assert.AreEqual (irm2.Value, 10, "#10");
200 irm.Check ( 0, 0, 1, 0, "irm" );
201 irm2.Check ( 0, 0, 1, 0, "irm2" );
205 public void NestedTransactionScope6 ()
207 IntResourceManager irm = new IntResourceManager (1);
208 IntResourceManager irm2 = new IntResourceManager (10);
210 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
211 using (TransactionScope scope = new TransactionScope ()) {
214 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
219 irm2.Check ( 1, 1, 0, 0, "irm2" );
220 Assert.AreEqual (irm2.Value, 20);
222 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
227 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
228 Assert.AreEqual (irm.Value, 2, "#12");
229 irm.Check ( 1, 1, 0, 0, "irm" );
233 public void NestedTransactionScope7 ()
235 IntResourceManager irm = new IntResourceManager (1);
236 IntResourceManager irm2 = new IntResourceManager (10);
238 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
239 using (TransactionScope scope = new TransactionScope ()) {
242 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
248 /* irm2, rolled back*/
249 irm2.Check ( 0, 0, 1, 0, "irm2" );
250 Assert.AreEqual (irm2.Value, 10, "#13");
252 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
257 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
258 /* ..But irm got committed */
259 Assert.AreEqual (irm.Value, 2, "#15");
260 irm.Check ( 1, 1, 0, 0, "irm" );
264 public void NestedTransactionScope8 ()
266 IntResourceManager irm = new IntResourceManager (1);
267 IntResourceManager irm2 = new IntResourceManager (10);
269 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
270 using (TransactionScope scope = new TransactionScope ()) {
273 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
274 /* Not transactional, so this WONT get committed */
278 irm2.Check ( 0, 0, 0, 0, "irm2" );
279 Assert.AreEqual (20, irm2.Value, "#16");
280 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
285 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
286 Assert.AreEqual (irm.Value, 2, "#18");
287 irm.Check ( 1, 1, 0, 0, "irm" );
291 public void NestedTransactionScope8a ()
293 IntResourceManager irm = new IntResourceManager ( 1 );
294 IntResourceManager irm2 = new IntResourceManager ( 10 );
296 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
297 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
300 using (TransactionScope scope2 = new TransactionScope ()) {
304 irm2.Check ( 1, 1, 0, 0, "irm2" );
305 Assert.AreEqual ( 20, irm2.Value, "#16a" );
310 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
311 Assert.AreEqual ( 2, irm.Value, "#18a" );
312 irm.Check ( 0, 0, 0, 0, "irm" );
316 public void NestedTransactionScope9 ()
318 IntResourceManager irm = new IntResourceManager (1);
319 IntResourceManager irm2 = new IntResourceManager (10);
321 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
322 using (TransactionScope scope = new TransactionScope ()) {
325 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
326 /* Not transactional, so this WONT get committed */
330 irm2.Check ( 0, 0, 0, 0, "irm2" );
332 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
337 /* vr's value has changed as the inner scope committed = 6 */
338 irm.Check ( 1, 1, 0, 0, "irm" );
339 Assert.AreEqual (irm.Value, 6, "#19");
340 Assert.AreEqual (irm.Actual, 6, "#20");
341 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
346 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
347 Assert.AreEqual (irm.Value, 6, "#22");
348 irm.Check ( 2, 2, 0, 0, "irm" );
352 [ExpectedException (typeof (TransactionAbortedException))]
353 public void NestedTransactionScope10 ()
355 IntResourceManager irm = new IntResourceManager (1);
358 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
359 using (TransactionScope scope = new TransactionScope ()) {
362 using (TransactionScope scope2 = new TransactionScope ()) {
364 /* Not completing this, so the transaction will
366 scope2.Complete (); */
369 using (TransactionScope scope3 = new TransactionScope ()) {
370 /* Aborted transaction cannot be used for another
373 //Assert.Fail ("Should not reach here.");
377 Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
381 public void NestedTransactionScope12 ()
383 IntResourceManager irm = new IntResourceManager (1);
385 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
386 using (TransactionScope scope = new TransactionScope ()) {
389 using (TransactionScope scope2 = new TransactionScope ()) {
391 /* Not completing this, so the transaction will
393 scope2.Complete (); */
396 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
397 /* Using RequiresNew here, so outer transaction
398 * being aborted doesn't matter
406 [ExpectedException (typeof (TransactionAbortedException))]
407 public void NestedTransactionScope13 ()
409 IntResourceManager irm = new IntResourceManager ( 1 );
411 Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
412 using ( TransactionScope scope = new TransactionScope () ) {
415 using ( TransactionScope scope2 = new TransactionScope () ) {
417 /* Not completing this, so the transaction will
419 scope2.Complete (); */
427 /* Tests using IntResourceManager */
430 public void RMFail1 ()
432 IntResourceManager irm = new IntResourceManager (1);
433 IntResourceManager irm2 = new IntResourceManager (10);
434 IntResourceManager irm3 = new IntResourceManager (12);
436 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
438 using (TransactionScope scope = new TransactionScope ()) {
443 /* Make second RM fail to prepare, this should throw
444 * TransactionAbortedException when the scope ends
446 irm2.FailPrepare = true;
449 } catch (TransactionAbortedException) {
450 irm.Check ( 1, 0, 1, 0, "irm");
451 irm2.Check ( 1, 0, 0, 0, "irm2");
452 irm3.Check ( 0, 0, 1, 0, "irm3");
454 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
458 public void RMFail2 ()
460 IntResourceManager irm = new IntResourceManager (1);
461 IntResourceManager irm2 = new IntResourceManager (10);
462 IntResourceManager irm3 = new IntResourceManager (12);
464 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
466 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
471 /* irm2 wont call Prepared or ForceRollback in
472 * its Prepare (), so TransactionManager will timeout
475 irm2.IgnorePrepare = true;
478 } catch (TransactionAbortedException e) {
479 /* FIXME: Not working right now.. no timeout exception thrown! */
481 Assert.IsNotNull ( e.InnerException, "innerexception is null" );
482 Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
484 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
488 Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
491 #region Explicit Transaction Tests
494 public void ExplicitTransactionCommit ()
496 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
498 CommittableTransaction ct = new CommittableTransaction ();
499 Transaction oldTransaction = Transaction.Current;
500 Transaction.Current = ct;
502 IntResourceManager irm = new IntResourceManager (1);
506 Assert.AreEqual (2, irm.Value, "#33");
507 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
508 Transaction.Current = oldTransaction;
512 public void ExplicitTransactionRollback ()
514 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
516 CommittableTransaction ct = new CommittableTransaction ();
517 Transaction oldTransaction = Transaction.Current;
518 Transaction.Current = ct;
520 IntResourceManager irm = new IntResourceManager (1);
522 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
525 Assert.AreEqual (1, irm.Value, "#36");
526 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
527 Transaction.Current = oldTransaction;
531 public void ExplicitTransaction1 ()
533 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
534 CommittableTransaction ct = new CommittableTransaction ();
535 Transaction oldTransaction = Transaction.Current;
537 Transaction.Current = ct;
539 IntResourceManager irm = new IntResourceManager (1);
543 using (TransactionScope scope = new TransactionScope ()) {
544 Assert.AreEqual (ct, Transaction.Current, "#38");
549 Assert.AreEqual (ct, Transaction.Current, "#39");
550 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
551 Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
554 Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
555 Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
556 Transaction.Current = oldTransaction;
560 public void ExplicitTransaction2 ()
562 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
563 CommittableTransaction ct = new CommittableTransaction ();
564 Transaction oldTransaction = Transaction.Current;
566 Transaction.Current = ct;
568 IntResourceManager irm = new IntResourceManager (1);
571 using (TransactionScope scope = new TransactionScope ()) {
572 Assert.AreEqual (ct, Transaction.Current, "#44");
574 /* Not calling scope.Complete
578 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
579 Assert.AreEqual (ct, Transaction.Current, "#46");
580 Assert.AreEqual (1, irm.Actual, "#47");
581 Assert.AreEqual (1, irm.NumRollback, "#48");
582 irm.Check ( 0, 0, 1, 0, "irm" );
583 Transaction.Current = oldTransaction;
587 } catch (TransactionAbortedException) {
590 Assert.Fail ("Commit on an aborted transaction should fail");
594 public void ExplicitTransaction3 ()
596 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
597 CommittableTransaction ct = new CommittableTransaction ();
598 Transaction oldTransaction = Transaction.Current;
600 Transaction.Current = ct;
602 IntResourceManager irm = new IntResourceManager (1);
604 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
605 Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
613 Assert.AreEqual (3, irm.Actual, "#50");
615 Assert.AreEqual (ct, Transaction.Current, "#51");
617 Assert.AreEqual (2, irm.Actual, "#52");
618 Transaction.Current = oldTransaction;
622 public void ExplicitTransaction4 ()
624 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
625 CommittableTransaction ct = new CommittableTransaction ();
626 Transaction oldTransaction = Transaction.Current;
628 /* Not setting ambient transaction
629 Transaction.Current = ct;
632 IntResourceManager irm = new IntResourceManager (1);
634 using (TransactionScope scope = new TransactionScope (ct)) {
635 Assert.AreEqual (ct, Transaction.Current, "#53");
641 Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
642 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
643 Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
646 Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
647 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
649 irm.Check ( 1, 1, 0, 0, "irm");
653 public void ExplicitTransaction5 ()
655 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
656 CommittableTransaction ct = new CommittableTransaction ();
657 Transaction oldTransaction = Transaction.Current;
659 /* Not setting ambient transaction
660 Transaction.Current = ct;
663 IntResourceManager irm = new IntResourceManager (1);
665 using (TransactionScope scope = new TransactionScope (ct)) {
666 Assert.AreEqual (ct, Transaction.Current, "#59");
670 /* Not completing this scope
671 scope.Complete (); */
674 Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
675 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
676 Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
678 irm.Check ( 0, 0, 1, 0, "irm");
682 [ExpectedException (typeof (InvalidOperationException))]
683 public void ExplicitTransaction6 ()
685 CommittableTransaction ct = new CommittableTransaction ();
687 IntResourceManager irm = new IntResourceManager (1);
695 [ExpectedException (typeof (InvalidOperationException))]
696 public void ExplicitTransaction6a ()
698 CommittableTransaction ct = new CommittableTransaction ();
700 IntResourceManager irm = new IntResourceManager ( 1 );
704 /* Using a already committed transaction in a new
707 TransactionScope scope = new TransactionScope ( ct );
711 public void ExplicitTransaction6b ()
713 CommittableTransaction ct = new CommittableTransaction ();
715 IntResourceManager irm = new IntResourceManager ( 1 );
717 Transaction.Current = ct;
719 TransactionScope scope1 = new TransactionScope ();
727 } catch (TransactionAbortedException) {
728 irm.Check ( 0, 0, 1, 0, "irm" );
731 Transaction.Current = null;
734 Assert.Fail ( "Commit should've failed" );
738 public void ExplicitTransaction6c ()
740 CommittableTransaction ct = new CommittableTransaction ();
742 IntResourceManager irm = new IntResourceManager ( 1 );
744 Transaction.Current = ct;
746 TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
750 TransactionScope scope2 = new TransactionScope ();
753 } catch (InvalidOperationException) {
754 /* Error: TransactionScope nested incorrectly */
755 irm.Check ( 0, 0, 1, 0, "irm" );
757 Transaction.Current = null;
761 Assert.Fail ("Commit should've failed");
765 public void ExplicitTransaction6d ()
767 CommittableTransaction ct = new CommittableTransaction ();
769 IntResourceManager irm = new IntResourceManager ( 1 );
771 Transaction.Current = ct;
773 TransactionScope scope1 = new TransactionScope ();
777 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
780 } catch (InvalidOperationException) {
781 /* Error: TransactionScope nested incorrectly */
783 Transaction.Current = null;
787 Assert.Fail ( "Commit should've failed" );
791 public void ExplicitTransaction6e ()
793 CommittableTransaction ct = new CommittableTransaction ();
795 IntResourceManager irm = new IntResourceManager ( 1 );
797 Transaction.Current = ct;
799 TransactionScope scope1 = new TransactionScope ();
803 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
806 } catch (InvalidOperationException) {
807 /* Error: TransactionScope nested incorrectly */
809 Transaction.Current = null;
813 Assert.Fail ( "Commit should've failed" );
817 [ExpectedException (typeof (TransactionException))]
818 public void ExplicitTransaction7 ()
820 CommittableTransaction ct = new CommittableTransaction ();
822 IntResourceManager irm = new IntResourceManager (1);
825 /* Cannot accept any new work now, so TransactionException */
830 public void ExplicitTransaction8 ()
832 CommittableTransaction ct = new CommittableTransaction ();
834 IntResourceManager irm = new IntResourceManager ( 1 );
835 using ( TransactionScope scope = new TransactionScope (ct) ) {
837 /* FIXME: Why TransactionAbortedException ?? */
840 } catch ( TransactionAbortedException) {
841 irm.Check ( 0, 0, 1, 0, "irm" );
844 Assert.Fail ( "Should not be reached" );
849 public void ExplicitTransaction8a ()
851 CommittableTransaction ct = new CommittableTransaction ();
853 IntResourceManager irm = new IntResourceManager ( 1 );
854 using ( TransactionScope scope = new TransactionScope ( ct ) ) {
857 /* FIXME: Why TransactionAbortedException ?? */
861 catch ( TransactionAbortedException) {
862 irm.Check ( 0, 0, 1, 0, "irm" );
865 Assert.Fail ( "Should not be reached" );
870 [ExpectedException (typeof (InvalidOperationException))]
871 public void ExplicitTransaction9 ()
873 CommittableTransaction ct = new CommittableTransaction ();
875 IntResourceManager irm = new IntResourceManager ( 1 );
876 ct.BeginCommit ( null, null );
877 ct.BeginCommit ( null, null );
881 public void ExplicitTransaction10 ()
883 CommittableTransaction ct = new CommittableTransaction ();
885 IntResourceManager irm = new IntResourceManager ( 1 );
886 Transaction.Current = ct;
889 TransactionScope scope = new TransactionScope ( ct );
890 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
895 } catch ( TransactionAbortedException) {
896 irm.Check ( 0, 0, 1, 0, "irm" );
897 Transaction.Current = null;
901 Transaction.Current = null;
902 Assert.Fail ("Expected TransactionAbortedException");
906 public void ExplicitTransaction10a ()
908 CommittableTransaction ct = new CommittableTransaction ();
910 IntResourceManager irm = new IntResourceManager ( 1 );
911 Transaction.Current = ct;
913 Transaction.Current = null;
915 TransactionScope scope = new TransactionScope ( ct );
916 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
917 Transaction.Current = null;
918 //scope2.Complete ();
923 catch ( TransactionAbortedException) {
924 irm.Check ( 0, 0, 1, 0, "irm" );
925 Transaction.Current = null;
929 Transaction.Current = null;
934 public void ExplicitTransaction10b ()
936 CommittableTransaction ct = new CommittableTransaction ();
938 IntResourceManager irm = new IntResourceManager ( 1 );
939 Transaction.Current = ct;
941 Transaction.Current = null;
943 TransactionScope scope = new TransactionScope ( ct );
944 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
945 //scope2.Complete ();
947 IAsyncResult ar = ct.BeginCommit ( null, null );
951 catch ( TransactionAbortedException) {
952 irm.Check ( 0, 0, 1, 0, "irm" );
953 Transaction.Current = null;
957 Transaction.Current = null;
962 [ExpectedException ( typeof (ArgumentException))]
963 public void ExplicitTransaction12 ()
965 CommittableTransaction ct = new CommittableTransaction ();
967 IntResourceManager irm = new IntResourceManager ( 1 );
968 irm.FailPrepare = true;
969 ct.BeginCommit ( null, null );
970 ct.EndCommit ( null );
974 public void ExplicitTransaction13 ()
976 CommittableTransaction ct = new CommittableTransaction ();
977 IntResourceManager irm = new IntResourceManager ( 1 );
979 Assert.IsNull ( Transaction.Current );
980 Transaction.Current = ct;
982 irm.FailPrepare = true;
986 } catch ( TransactionAbortedException ) {
987 Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
989 ct.BeginCommit ( null, null );
990 } catch (Exception) {
991 Transaction.Current = null;
994 Assert.Fail ( "Should not be reached(2)" );
996 Assert.Fail ("Should not be reached");
1000 public void ExplicitTransaction14 ()
1002 CommittableTransaction ct = new CommittableTransaction ();
1003 IntResourceManager irm = new IntResourceManager ( 1 );
1005 Assert.IsNull ( Transaction.Current );
1006 Transaction.Current = ct;
1011 Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
1013 ct.BeginCommit ( null, null );
1015 catch ( Exception) {
1016 Transaction.Current = null;
1019 Assert.Fail ( "Should not be reached" );
1023 public void ExplicitTransaction15 ()
1025 CommittableTransaction ct = new CommittableTransaction ();
1026 IntResourceManager irm = new IntResourceManager ( 1 );
1027 IntResourceManager irm2 = new IntResourceManager ( 3 );
1029 Assert.IsNull ( Transaction.Current );
1030 Transaction.Current = ct;
1033 using (TransactionScope scope = new TransactionScope ()) {
1035 Transaction.Current = new CommittableTransaction ();
1038 } catch (InvalidOperationException) {
1039 irm.Check ( 0, 0, 1, 0, "irm" );
1040 irm2.Check ( 0, 0, 1, 0, "irm2" );
1041 Transaction.Current = null;
1045 Assert.Fail ( "Should not be reached" );
1049 public void ExplicitTransaction16 ()
1051 CommittableTransaction ct = new CommittableTransaction ();
1052 IntResourceManager irm0 = new IntResourceManager ( 3 );
1053 IntResourceManager irm = new IntResourceManager ( 1 );
1055 Assert.IsNull ( Transaction.Current );
1057 Transaction.Current = ct;
1059 irm.FailPrepare = true;
1060 irm.FailWithException = true;
1066 } catch (TransactionAbortedException e) {
1067 Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
1068 Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
1069 irm.Check ( 1, 0, 0, 0, "irm" );
1070 irm0.Check ( 0, 0, 1, 0, "irm0" );
1071 Transaction.Current = null;
1075 Assert.Fail ( "Should not be reached" );