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 TransactionScopeWithInvalidTimeSpanThrows ()
25 TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, TimeSpan.FromSeconds (-1));
26 Assert.Fail ("Expected exception when passing TransactionScopeOption and an invalid TimeSpan.");
27 } catch (ArgumentOutOfRangeException ex) {
28 Assert.AreEqual (ex.ParamName, "timeout");
32 TransactionScope scope = new TransactionScope (null, TimeSpan.FromSeconds (-1));
33 Assert.Fail ("Expected exception when passing TransactionScopeOption and an invalid TimeSpan.");
34 } catch (ArgumentOutOfRangeException ex) {
35 Assert.AreEqual (ex.ParamName, "timeout");
40 public void TransactionScopeCommit ()
42 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
43 using (TransactionScope scope = new TransactionScope ()) {
44 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
45 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
49 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
53 public void TransactionScopeAbort ()
55 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
56 IntResourceManager irm = new IntResourceManager (1);
57 using (TransactionScope scope = new TransactionScope ()) {
58 Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
59 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
62 /* Not completing scope here */
64 irm.Check ( 0, 0, 1, 0, "irm");
65 Assert.AreEqual (1, irm.Value);
66 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
70 [ExpectedException (typeof (InvalidOperationException))]
71 public void TransactionScopeCompleted1 ()
73 using (TransactionScope scope = new TransactionScope ()) {
75 /* Can't access ambient transaction after scope.Complete */
76 TransactionStatus status = Transaction.Current.TransactionInformation.Status;
81 [ExpectedException (typeof (InvalidOperationException))]
82 public void TransactionScopeCompleted2 ()
84 using (TransactionScope scope = new TransactionScope ()) {
86 Transaction.Current = Transaction.Current;
91 [ExpectedException (typeof (InvalidOperationException))]
92 public void TransactionScopeCompleted3 ()
94 using (TransactionScope scope = new TransactionScope ()) {
100 #region NestedTransactionScope tests
102 public void NestedTransactionScope1 ()
104 IntResourceManager irm = new IntResourceManager (1);
106 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
107 using (TransactionScope scope = new TransactionScope ()) {
110 /* Complete this scope */
114 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
115 /* Value = 2, got committed */
116 Assert.AreEqual (irm.Value, 2, "#1");
117 irm.Check ( 1, 1, 0, 0, "irm" );
121 public void NestedTransactionScope2 ()
123 IntResourceManager irm = new IntResourceManager (1);
124 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
125 using (TransactionScope scope = new TransactionScope ()) {
128 /* Not-Completing this scope */
131 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
132 /* Value = 2, got rolledback */
133 Assert.AreEqual (irm.Value, 1, "#2");
134 irm.Check ( 0, 0, 1, 0, "irm" );
138 public void NestedTransactionScope3 ()
140 IntResourceManager irm = new IntResourceManager (1);
141 IntResourceManager irm2 = new IntResourceManager (10);
143 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
144 using (TransactionScope scope = new TransactionScope ()) {
147 using (TransactionScope scope2 = new TransactionScope ()) {
156 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
157 /* Both got committed */
158 Assert.AreEqual (irm.Value, 2, "#3");
159 Assert.AreEqual (irm2.Value, 20, "#4");
160 irm.Check ( 1, 1, 0, 0, "irm" );
161 irm2.Check ( 1, 1, 0, 0, "irm2" );
165 public void NestedTransactionScope4 ()
167 IntResourceManager irm = new IntResourceManager (1);
168 IntResourceManager irm2 = new IntResourceManager (10);
170 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
171 using (TransactionScope scope = new TransactionScope ()) {
174 using (TransactionScope scope2 = new TransactionScope ()) {
177 /* Inner Tx not completed, Tx should get rolled back */
180 /* Both rolledback */
181 irm.Check ( 0, 0, 1, 0, "irm" );
182 irm2.Check ( 0, 0, 1, 0, "irm2" );
183 Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
187 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
189 Assert.AreEqual (irm.Value, 1, "#6");
190 Assert.AreEqual (irm2.Value, 10, "#7");
191 irm.Check ( 0, 0, 1, 0, "irm" );
195 public void NestedTransactionScope5 ()
197 IntResourceManager irm = new IntResourceManager (1);
198 IntResourceManager irm2 = new IntResourceManager (10);
200 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
201 using (TransactionScope scope = new TransactionScope ()) {
204 using (TransactionScope scope2 = new TransactionScope ()) {
209 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
210 /* Not completing outer scope
211 scope.Complete (); */
214 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
216 Assert.AreEqual (irm.Value, 1, "#9");
217 Assert.AreEqual (irm2.Value, 10, "#10");
218 irm.Check ( 0, 0, 1, 0, "irm" );
219 irm2.Check ( 0, 0, 1, 0, "irm2" );
223 public void NestedTransactionScope6 ()
225 IntResourceManager irm = new IntResourceManager (1);
226 IntResourceManager irm2 = new IntResourceManager (10);
228 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
229 using (TransactionScope scope = new TransactionScope ()) {
232 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
237 irm2.Check ( 1, 1, 0, 0, "irm2" );
238 Assert.AreEqual (irm2.Value, 20);
240 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
245 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
246 Assert.AreEqual (irm.Value, 2, "#12");
247 irm.Check ( 1, 1, 0, 0, "irm" );
251 public void NestedTransactionScope7 ()
253 IntResourceManager irm = new IntResourceManager (1);
254 IntResourceManager irm2 = new IntResourceManager (10);
256 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
257 using (TransactionScope scope = new TransactionScope ()) {
260 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
266 /* irm2, rolled back*/
267 irm2.Check ( 0, 0, 1, 0, "irm2" );
268 Assert.AreEqual (irm2.Value, 10, "#13");
270 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
275 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
276 /* ..But irm got committed */
277 Assert.AreEqual (irm.Value, 2, "#15");
278 irm.Check ( 1, 1, 0, 0, "irm" );
282 public void NestedTransactionScope8 ()
284 IntResourceManager irm = new IntResourceManager (1);
285 IntResourceManager irm2 = new IntResourceManager (10);
287 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
288 using (TransactionScope scope = new TransactionScope ()) {
291 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
292 /* Not transactional, so this WONT get committed */
296 irm2.Check ( 0, 0, 0, 0, "irm2" );
297 Assert.AreEqual (20, irm2.Value, "#16");
298 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
303 Assert.IsNull (Transaction.Current, "Ambient transaction exists");
304 Assert.AreEqual (irm.Value, 2, "#18");
305 irm.Check ( 1, 1, 0, 0, "irm" );
309 public void NestedTransactionScope8a ()
311 IntResourceManager irm = new IntResourceManager ( 1 );
312 IntResourceManager irm2 = new IntResourceManager ( 10 );
314 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
315 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
318 using (TransactionScope scope2 = new TransactionScope ()) {
322 irm2.Check ( 1, 1, 0, 0, "irm2" );
323 Assert.AreEqual ( 20, irm2.Value, "#16a" );
328 Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
329 Assert.AreEqual ( 2, irm.Value, "#18a" );
330 irm.Check ( 0, 0, 0, 0, "irm" );
334 public void NestedTransactionScope9 ()
336 IntResourceManager irm = new IntResourceManager (1);
337 IntResourceManager irm2 = new IntResourceManager (10);
339 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
340 using (TransactionScope scope = new TransactionScope ()) {
343 using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
344 /* Not transactional, so this WONT get committed */
348 irm2.Check ( 0, 0, 0, 0, "irm2" );
350 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
355 /* vr's value has changed as the inner scope committed = 6 */
356 irm.Check ( 1, 1, 0, 0, "irm" );
357 Assert.AreEqual (irm.Value, 6, "#19");
358 Assert.AreEqual (irm.Actual, 6, "#20");
359 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
364 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
365 Assert.AreEqual (irm.Value, 6, "#22");
366 irm.Check ( 2, 2, 0, 0, "irm" );
370 [ExpectedException (typeof (TransactionAbortedException))]
371 public void NestedTransactionScope10 ()
373 IntResourceManager irm = new IntResourceManager (1);
376 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
377 using (TransactionScope scope = new TransactionScope ()) {
380 using (TransactionScope scope2 = new TransactionScope ()) {
382 /* Not completing this, so the transaction will
384 scope2.Complete (); */
387 using (TransactionScope scope3 = new TransactionScope ()) {
388 /* Aborted transaction cannot be used for another
391 //Assert.Fail ("Should not reach here.");
395 Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
399 public void NestedTransactionScope12 ()
401 IntResourceManager irm = new IntResourceManager (1);
403 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
404 using (TransactionScope scope = new TransactionScope ()) {
407 using (TransactionScope scope2 = new TransactionScope ()) {
409 /* Not completing this, so the transaction will
411 scope2.Complete (); */
414 using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
415 /* Using RequiresNew here, so outer transaction
416 * being aborted doesn't matter
424 [ExpectedException (typeof (TransactionAbortedException))]
425 public void NestedTransactionScope13 ()
427 IntResourceManager irm = new IntResourceManager ( 1 );
429 Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
430 using ( TransactionScope scope = new TransactionScope () ) {
433 using ( TransactionScope scope2 = new TransactionScope () ) {
435 /* Not completing this, so the transaction will
437 scope2.Complete (); */
445 /* Tests using IntResourceManager */
448 public void RMFail1 ()
450 IntResourceManager irm = new IntResourceManager (1);
451 IntResourceManager irm2 = new IntResourceManager (10);
452 IntResourceManager irm3 = new IntResourceManager (12);
454 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
456 using (TransactionScope scope = new TransactionScope ()) {
461 /* Make second RM fail to prepare, this should throw
462 * TransactionAbortedException when the scope ends
464 irm2.FailPrepare = true;
467 } catch (TransactionAbortedException) {
468 irm.Check ( 1, 0, 1, 0, "irm");
469 irm2.Check ( 1, 0, 0, 0, "irm2");
470 irm3.Check ( 0, 0, 1, 0, "irm3");
472 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
476 public void RMFail2 ()
478 IntResourceManager irm = new IntResourceManager (1);
479 IntResourceManager irm2 = new IntResourceManager (10);
480 IntResourceManager irm3 = new IntResourceManager (12);
482 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
484 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
489 /* irm2 wont call Prepared or ForceRollback in
490 * its Prepare (), so TransactionManager will timeout
493 irm2.IgnorePrepare = true;
496 } catch (TransactionAbortedException e) {
497 /* FIXME: Not working right now.. no timeout exception thrown! */
499 Assert.IsNotNull ( e.InnerException, "innerexception is null" );
500 Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
502 Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
506 Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
509 #region Explicit Transaction Tests
512 public void ExplicitTransactionCommit ()
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);
524 Assert.AreEqual (2, irm.Value, "#33");
525 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
526 Transaction.Current = oldTransaction;
530 public void ExplicitTransactionRollback ()
532 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
534 CommittableTransaction ct = new CommittableTransaction ();
535 Transaction oldTransaction = Transaction.Current;
536 Transaction.Current = ct;
538 IntResourceManager irm = new IntResourceManager (1);
540 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
543 Assert.AreEqual (1, irm.Value, "#36");
544 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
545 Transaction.Current = oldTransaction;
549 public void ExplicitTransaction1 ()
551 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
552 CommittableTransaction ct = new CommittableTransaction ();
553 Transaction oldTransaction = Transaction.Current;
555 Transaction.Current = ct;
557 IntResourceManager irm = new IntResourceManager (1);
561 using (TransactionScope scope = new TransactionScope ()) {
562 Assert.AreEqual (ct, Transaction.Current, "#38");
567 Assert.AreEqual (ct, Transaction.Current, "#39");
568 Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
569 Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
572 Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
573 Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
574 Transaction.Current = oldTransaction;
578 public void ExplicitTransaction2 ()
580 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
581 CommittableTransaction ct = new CommittableTransaction ();
582 Transaction oldTransaction = Transaction.Current;
584 Transaction.Current = ct;
586 IntResourceManager irm = new IntResourceManager (1);
589 using (TransactionScope scope = new TransactionScope ()) {
590 Assert.AreEqual (ct, Transaction.Current, "#44");
592 /* Not calling scope.Complete
596 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
597 Assert.AreEqual (ct, Transaction.Current, "#46");
598 Assert.AreEqual (1, irm.Actual, "#47");
599 Assert.AreEqual (1, irm.NumRollback, "#48");
600 irm.Check ( 0, 0, 1, 0, "irm" );
601 Transaction.Current = oldTransaction;
605 } catch (TransactionAbortedException) {
608 Assert.Fail ("Commit on an aborted transaction should fail");
612 public void ExplicitTransaction3 ()
614 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
615 CommittableTransaction ct = new CommittableTransaction ();
616 Transaction oldTransaction = Transaction.Current;
618 Transaction.Current = ct;
620 IntResourceManager irm = new IntResourceManager (1);
622 using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
623 Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
631 Assert.AreEqual (3, irm.Actual, "#50");
633 Assert.AreEqual (ct, Transaction.Current, "#51");
635 Assert.AreEqual (2, irm.Actual, "#52");
636 Transaction.Current = oldTransaction;
640 public void ExplicitTransaction4 ()
642 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
643 CommittableTransaction ct = new CommittableTransaction ();
644 Transaction oldTransaction = Transaction.Current;
646 /* Not setting ambient transaction
647 Transaction.Current = ct;
650 IntResourceManager irm = new IntResourceManager (1);
652 using (TransactionScope scope = new TransactionScope (ct)) {
653 Assert.AreEqual (ct, Transaction.Current, "#53");
659 Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
660 Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
661 Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
664 Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
665 Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
667 irm.Check ( 1, 1, 0, 0, "irm");
671 public void ExplicitTransaction5 ()
673 Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
674 CommittableTransaction ct = new CommittableTransaction ();
675 Transaction oldTransaction = Transaction.Current;
677 /* Not setting ambient transaction
678 Transaction.Current = ct;
681 IntResourceManager irm = new IntResourceManager (1);
683 using (TransactionScope scope = new TransactionScope (ct)) {
684 Assert.AreEqual (ct, Transaction.Current, "#59");
688 /* Not completing this scope
689 scope.Complete (); */
692 Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
693 Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
694 Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
696 irm.Check ( 0, 0, 1, 0, "irm");
700 [ExpectedException (typeof (InvalidOperationException))]
701 public void ExplicitTransaction6 ()
703 CommittableTransaction ct = new CommittableTransaction ();
705 IntResourceManager irm = new IntResourceManager (1);
713 [ExpectedException (typeof (InvalidOperationException))]
714 public void ExplicitTransaction6a ()
716 CommittableTransaction ct = new CommittableTransaction ();
718 IntResourceManager irm = new IntResourceManager ( 1 );
722 /* Using a already committed transaction in a new
725 TransactionScope scope = new TransactionScope ( ct );
729 public void ExplicitTransaction6b ()
731 CommittableTransaction ct = new CommittableTransaction ();
733 IntResourceManager irm = new IntResourceManager ( 1 );
735 Transaction.Current = ct;
737 TransactionScope scope1 = new TransactionScope ();
745 } catch (TransactionAbortedException) {
746 irm.Check ( 0, 0, 1, 0, "irm" );
749 Transaction.Current = null;
752 Assert.Fail ( "Commit should've failed" );
756 public void ExplicitTransaction6c ()
758 CommittableTransaction ct = new CommittableTransaction ();
760 IntResourceManager irm = new IntResourceManager ( 1 );
762 Transaction.Current = ct;
764 TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
768 TransactionScope scope2 = new TransactionScope ();
771 } catch (InvalidOperationException) {
772 /* Error: TransactionScope nested incorrectly */
773 irm.Check ( 0, 0, 1, 0, "irm" );
775 Transaction.Current = null;
779 Assert.Fail ("Commit should've failed");
783 public void ExplicitTransaction6d ()
785 CommittableTransaction ct = new CommittableTransaction ();
787 IntResourceManager irm = new IntResourceManager ( 1 );
789 Transaction.Current = ct;
791 TransactionScope scope1 = new TransactionScope ();
795 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
798 } catch (InvalidOperationException) {
799 /* Error: TransactionScope nested incorrectly */
801 Transaction.Current = null;
805 Assert.Fail ( "Commit should've failed" );
809 public void ExplicitTransaction6e ()
811 CommittableTransaction ct = new CommittableTransaction ();
813 IntResourceManager irm = new IntResourceManager ( 1 );
815 Transaction.Current = ct;
817 TransactionScope scope1 = new TransactionScope ();
821 TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
824 } catch (InvalidOperationException) {
825 /* Error: TransactionScope nested incorrectly */
827 Transaction.Current = null;
831 Assert.Fail ( "Commit should've failed" );
835 [ExpectedException (typeof (TransactionException))]
836 public void ExplicitTransaction7 ()
838 CommittableTransaction ct = new CommittableTransaction ();
840 IntResourceManager irm = new IntResourceManager (1);
843 /* Cannot accept any new work now, so TransactionException */
848 public void ExplicitTransaction8 ()
850 CommittableTransaction ct = new CommittableTransaction ();
852 IntResourceManager irm = new IntResourceManager ( 1 );
853 using ( TransactionScope scope = new TransactionScope (ct) ) {
855 /* FIXME: Why TransactionAbortedException ?? */
858 } catch ( TransactionAbortedException) {
859 irm.Check ( 0, 0, 1, 0, "irm" );
862 Assert.Fail ( "Should not be reached" );
867 public void ExplicitTransaction8a ()
869 CommittableTransaction ct = new CommittableTransaction ();
871 IntResourceManager irm = new IntResourceManager ( 1 );
872 using ( TransactionScope scope = new TransactionScope ( ct ) ) {
875 /* FIXME: Why TransactionAbortedException ?? */
879 catch ( TransactionAbortedException) {
880 irm.Check ( 0, 0, 1, 0, "irm" );
883 Assert.Fail ( "Should not be reached" );
888 [ExpectedException (typeof (InvalidOperationException))]
889 public void ExplicitTransaction9 ()
891 CommittableTransaction ct = new CommittableTransaction ();
893 IntResourceManager irm = new IntResourceManager ( 1 );
894 ct.BeginCommit ( null, null );
895 ct.BeginCommit ( null, null );
899 public void ExplicitTransaction10 ()
901 CommittableTransaction ct = new CommittableTransaction ();
903 IntResourceManager irm = new IntResourceManager ( 1 );
904 Transaction.Current = ct;
907 TransactionScope scope = new TransactionScope ( ct );
908 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
913 } catch ( TransactionAbortedException) {
914 irm.Check ( 0, 0, 1, 0, "irm" );
915 Transaction.Current = null;
919 Transaction.Current = null;
920 Assert.Fail ("Expected TransactionAbortedException");
924 public void ExplicitTransaction10a ()
926 CommittableTransaction ct = new CommittableTransaction ();
928 IntResourceManager irm = new IntResourceManager ( 1 );
929 Transaction.Current = ct;
931 Transaction.Current = null;
933 TransactionScope scope = new TransactionScope ( ct );
934 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
935 Transaction.Current = null;
936 //scope2.Complete ();
941 catch ( TransactionAbortedException) {
942 irm.Check ( 0, 0, 1, 0, "irm" );
943 Transaction.Current = null;
947 Transaction.Current = null;
952 public void ExplicitTransaction10b ()
954 CommittableTransaction ct = new CommittableTransaction ();
956 IntResourceManager irm = new IntResourceManager ( 1 );
957 Transaction.Current = ct;
959 Transaction.Current = null;
961 TransactionScope scope = new TransactionScope ( ct );
962 Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
963 //scope2.Complete ();
965 IAsyncResult ar = ct.BeginCommit ( null, null );
969 catch ( TransactionAbortedException) {
970 irm.Check ( 0, 0, 1, 0, "irm" );
971 Transaction.Current = null;
975 Transaction.Current = null;
980 [ExpectedException ( typeof (ArgumentException))]
981 public void ExplicitTransaction12 ()
983 CommittableTransaction ct = new CommittableTransaction ();
985 IntResourceManager irm = new IntResourceManager ( 1 );
986 irm.FailPrepare = true;
987 ct.BeginCommit ( null, null );
988 ct.EndCommit ( null );
992 public void ExplicitTransaction13 ()
994 CommittableTransaction ct = new CommittableTransaction ();
995 IntResourceManager irm = new IntResourceManager ( 1 );
997 Assert.IsNull ( Transaction.Current );
998 Transaction.Current = ct;
1000 irm.FailPrepare = true;
1004 } catch ( TransactionAbortedException ) {
1005 Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
1007 ct.BeginCommit ( null, null );
1008 } catch (Exception) {
1009 Transaction.Current = null;
1012 Assert.Fail ( "Should not be reached(2)" );
1014 Assert.Fail ("Should not be reached");
1018 public void ExplicitTransaction14 ()
1020 CommittableTransaction ct = new CommittableTransaction ();
1021 IntResourceManager irm = new IntResourceManager ( 1 );
1023 Assert.IsNull ( Transaction.Current );
1024 Transaction.Current = ct;
1029 Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
1031 ct.BeginCommit ( null, null );
1033 catch ( Exception) {
1034 Transaction.Current = null;
1037 Assert.Fail ( "Should not be reached" );
1041 public void ExplicitTransaction15 ()
1043 CommittableTransaction ct = new CommittableTransaction ();
1044 IntResourceManager irm = new IntResourceManager ( 1 );
1045 IntResourceManager irm2 = new IntResourceManager ( 3 );
1047 Assert.IsNull ( Transaction.Current );
1048 Transaction.Current = ct;
1051 using (TransactionScope scope = new TransactionScope ()) {
1053 Transaction.Current = new CommittableTransaction ();
1056 } catch (InvalidOperationException) {
1057 irm.Check ( 0, 0, 1, 0, "irm" );
1058 irm2.Check ( 0, 0, 1, 0, "irm2" );
1059 Transaction.Current = null;
1063 Assert.Fail ( "Should not be reached" );
1067 public void ExplicitTransaction16 ()
1069 CommittableTransaction ct = new CommittableTransaction ();
1070 IntResourceManager irm0 = new IntResourceManager ( 3 );
1071 IntResourceManager irm = new IntResourceManager ( 1 );
1073 Assert.IsNull ( Transaction.Current );
1075 Transaction.Current = ct;
1077 irm.FailPrepare = true;
1078 irm.FailWithException = true;
1084 } catch (TransactionAbortedException e) {
1085 Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
1086 Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
1087 irm.Check ( 1, 0, 0, 0, "irm" );
1088 irm0.Check ( 0, 0, 1, 0, "irm0" );
1089 Transaction.Current = null;
1093 Assert.Fail ( "Should not be reached" );