| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083 |
- //
- // Unit tests for TransactionScope and Implicit/Explicit use of
- // Transactions
- //
- // Author:
- // Ankit Jain <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
- //
- using System;
- using NUnit.Framework;
- using System.Transactions;
- namespace MonoTests.System.Transactions
- {
- [TestFixture]
- public class TransactionScopeTest
- {
- [Test]
- public void TransactionScopeCommit ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- using (TransactionScope scope = new TransactionScope ()) {
- Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
-
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
- }
- [Test]
- public void TransactionScopeAbort ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- IntResourceManager irm = new IntResourceManager (1);
- using (TransactionScope scope = new TransactionScope ()) {
- Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
- irm.Value = 2;
- /* Not completing scope here */
- }
- irm.Check ( 0, 0, 1, 0, "irm");
- Assert.AreEqual (1, irm.Value);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void TransactionScopeCompleted1 ()
- {
- using (TransactionScope scope = new TransactionScope ()) {
- scope.Complete ();
- /* Can't access ambient transaction after scope.Complete */
- TransactionStatus status = Transaction.Current.TransactionInformation.Status;
- }
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void TransactionScopeCompleted2 ()
- {
- using (TransactionScope scope = new TransactionScope ()) {
- scope.Complete ();
- Transaction.Current = Transaction.Current;
- }
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void TransactionScopeCompleted3 ()
- {
- using (TransactionScope scope = new TransactionScope ()) {
- scope.Complete ();
- scope.Complete ();
- }
- }
- #region NestedTransactionScope tests
- [Test]
- public void NestedTransactionScope1 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- /* Complete this scope */
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- /* Value = 2, got committed */
- Assert.AreEqual (irm.Value, 2, "#1");
- irm.Check ( 1, 1, 0, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope2 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- /* Not-Completing this scope */
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- /* Value = 2, got rolledback */
- Assert.AreEqual (irm.Value, 1, "#2");
- irm.Check ( 0, 0, 1, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope3 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm2.Value = 20;
- scope2.Complete ();
- }
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- /* Both got committed */
- Assert.AreEqual (irm.Value, 2, "#3");
- Assert.AreEqual (irm2.Value, 20, "#4");
- irm.Check ( 1, 1, 0, 0, "irm" );
- irm2.Check ( 1, 1, 0, 0, "irm2" );
- }
- [Test]
- public void NestedTransactionScope4 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm2.Value = 20;
- /* Inner Tx not completed, Tx should get rolled back */
- //scope2.Complete();
- }
- /* Both rolledback */
- irm.Check ( 0, 0, 1, 0, "irm" );
- irm2.Check ( 0, 0, 1, 0, "irm2" );
- Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
- //scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- Assert.AreEqual (irm.Value, 1, "#6");
- Assert.AreEqual (irm2.Value, 10, "#7");
- irm.Check ( 0, 0, 1, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope5 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm2.Value = 20;
- scope2.Complete ();
- }
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
- /* Not completing outer scope
- scope.Complete (); */
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- Assert.AreEqual (irm.Value, 1, "#9");
- Assert.AreEqual (irm2.Value, 10, "#10");
- irm.Check ( 0, 0, 1, 0, "irm" );
- irm2.Check ( 0, 0, 1, 0, "irm2" );
- }
- [Test]
- public void NestedTransactionScope6 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
- irm2.Value = 20;
- scope2.Complete ();
- }
- /* vr2, committed */
- irm2.Check ( 1, 1, 0, 0, "irm2" );
- Assert.AreEqual (irm2.Value, 20);
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- Assert.AreEqual (irm.Value, 2, "#12");
- irm.Check ( 1, 1, 0, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope7 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
- irm2.Value = 20;
- /* Not completing
- scope2.Complete();*/
- }
- /* irm2, rolled back*/
- irm2.Check ( 0, 0, 1, 0, "irm2" );
- Assert.AreEqual (irm2.Value, 10, "#13");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- /* ..But irm got committed */
- Assert.AreEqual (irm.Value, 2, "#15");
- irm.Check ( 1, 1, 0, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope8 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
- /* Not transactional, so this WONT get committed */
- irm2.Value = 20;
- scope2.Complete ();
- }
- irm2.Check ( 0, 0, 0, 0, "irm2" );
- Assert.AreEqual (20, irm2.Value, "#16");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists");
- Assert.AreEqual (irm.Value, 2, "#18");
- irm.Check ( 1, 1, 0, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope8a ()
- {
- IntResourceManager irm = new IntResourceManager ( 1 );
- IntResourceManager irm2 = new IntResourceManager ( 10 );
- Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
- using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm2.Value = 20;
- scope2.Complete ();
- }
- irm2.Check ( 1, 1, 0, 0, "irm2" );
- Assert.AreEqual ( 20, irm2.Value, "#16a" );
- scope.Complete ();
- }
- Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
- Assert.AreEqual ( 2, irm.Value, "#18a" );
- irm.Check ( 0, 0, 0, 0, "irm" );
- }
- [Test]
- public void NestedTransactionScope9 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
- /* Not transactional, so this WONT get committed */
- irm2.Value = 4;
- scope2.Complete ();
- }
- irm2.Check ( 0, 0, 0, 0, "irm2" );
- using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
- irm.Value = 6;
- scope3.Complete ();
- }
- /* vr's value has changed as the inner scope committed = 6 */
- irm.Check ( 1, 1, 0, 0, "irm" );
- Assert.AreEqual (irm.Value, 6, "#19");
- Assert.AreEqual (irm.Actual, 6, "#20");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
- scope.Complete ();
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
- Assert.AreEqual (irm.Value, 6, "#22");
- irm.Check ( 2, 2, 0, 0, "irm" );
- }
- [Test]
- [ExpectedException (typeof (TransactionAbortedException))]
- public void NestedTransactionScope10 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- bool failed = false;
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm.Value = 4;
- /* Not completing this, so the transaction will
- * get aborted
- scope2.Complete (); */
- }
- using (TransactionScope scope3 = new TransactionScope ()) {
- /* Aborted transaction cannot be used for another
- * TransactionScope
- */
- //Assert.Fail ("Should not reach here.");
- failed = true;
- }
- }
- Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
- }
- [Test]
- public void NestedTransactionScope12 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- using (TransactionScope scope2 = new TransactionScope ()) {
- irm.Value = 4;
- /* Not completing this, so the transaction will
- * get aborted
- scope2.Complete (); */
- }
- using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
- /* Using RequiresNew here, so outer transaction
- * being aborted doesn't matter
- */
- scope3.Complete ();
- }
- }
- }
- [Test]
- [ExpectedException (typeof (TransactionAbortedException))]
- public void NestedTransactionScope13 ()
- {
- IntResourceManager irm = new IntResourceManager ( 1 );
- Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
- using ( TransactionScope scope = new TransactionScope () ) {
- irm.Value = 2;
- using ( TransactionScope scope2 = new TransactionScope () ) {
- irm.Value = 4;
- /* Not completing this, so the transaction will
- * get aborted
- scope2.Complete (); */
- }
- scope.Complete ();
- }
- }
- #endregion
- /* Tests using IntResourceManager */
- [Test]
- public void RMFail1 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- IntResourceManager irm3 = new IntResourceManager (12);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- try {
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- irm2.Value = 20;
- irm3.Value = 24;
- /* Make second RM fail to prepare, this should throw
- * TransactionAbortedException when the scope ends
- */
- irm2.FailPrepare = true;
- scope.Complete ();
- }
- } catch (TransactionAbortedException) {
- irm.Check ( 1, 0, 1, 0, "irm");
- irm2.Check ( 1, 0, 0, 0, "irm2");
- irm3.Check ( 0, 0, 1, 0, "irm3");
- }
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
- }
- [Test]
- [Category("NotWorking")]
- [Ignore("NotWorking")]
- public void RMFail2 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (10);
- IntResourceManager irm3 = new IntResourceManager (12);
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- try {
- using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
- irm.Value = 2;
- irm2.Value = 20;
- irm3.Value = 24;
- /* irm2 wont call Prepared or ForceRollback in
- * its Prepare (), so TransactionManager will timeout
- * waiting for it
- */
- irm2.IgnorePrepare = true;
- scope.Complete ();
- }
- } catch (TransactionAbortedException e) {
- /* FIXME: Not working right now.. no timeout exception thrown! */
-
- Assert.IsNotNull ( e.InnerException, "innerexception is null" );
- Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
- return;
- }
- Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
- }
- #region Explicit Transaction Tests
- [Test]
- public void ExplicitTransactionCommit ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- Transaction.Current = ct;
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- ct.Commit ();
- Assert.AreEqual (2, irm.Value, "#33");
- Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
- Transaction.Current = oldTransaction;
- }
- [Test]
- public void ExplicitTransactionRollback ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- Transaction.Current = ct;
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
- ct.Rollback ();
- Assert.AreEqual (1, irm.Value, "#36");
- Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
- Transaction.Current = oldTransaction;
- }
- [Test]
- public void ExplicitTransaction1 ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- Transaction.Current = ct;
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- using (TransactionScope scope = new TransactionScope ()) {
- Assert.AreEqual (ct, Transaction.Current, "#38");
- irm.Value = 4;
- scope.Complete ();
- }
- Assert.AreEqual (ct, Transaction.Current, "#39");
- Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
- Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
- ct.Commit ();
- Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
- Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
- Transaction.Current = oldTransaction;
- }
- [Test]
- public void ExplicitTransaction2 ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- Transaction.Current = ct;
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- using (TransactionScope scope = new TransactionScope ()) {
- Assert.AreEqual (ct, Transaction.Current, "#44");
- /* Not calling scope.Complete
- scope.Complete ();*/
- }
- Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
- Assert.AreEqual (ct, Transaction.Current, "#46");
- Assert.AreEqual (1, irm.Actual, "#47");
- Assert.AreEqual (1, irm.NumRollback, "#48");
- irm.Check ( 0, 0, 1, 0, "irm" );
- Transaction.Current = oldTransaction;
- try {
- ct.Commit ();
- } catch (TransactionAbortedException) {
- return;
- }
- Assert.Fail ("Commit on an aborted transaction should fail");
- }
- [Test]
- public void ExplicitTransaction3 ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- Transaction.Current = ct;
- IntResourceManager irm = new IntResourceManager (1);
- using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
- Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
- irm.Value = 3;
- scope.Complete ();
- }
- irm.Value = 2;
- Assert.AreEqual (3, irm.Actual, "#50");
- Assert.AreEqual (ct, Transaction.Current, "#51");
- ct.Commit ();
- Assert.AreEqual (2, irm.Actual, "#52");
- Transaction.Current = oldTransaction;
- }
- [Test]
- public void ExplicitTransaction4 ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- /* Not setting ambient transaction
- Transaction.Current = ct;
- */
- IntResourceManager irm = new IntResourceManager (1);
- using (TransactionScope scope = new TransactionScope (ct)) {
- Assert.AreEqual (ct, Transaction.Current, "#53");
- irm.Value = 2;
- scope.Complete ();
- }
- Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
- Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
- Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
- ct.Commit ();
- Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
- Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
- irm.Check ( 1, 1, 0, 0, "irm");
- }
- [Test]
- public void ExplicitTransaction5 ()
- {
- Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
- CommittableTransaction ct = new CommittableTransaction ();
- Transaction oldTransaction = Transaction.Current;
- /* Not setting ambient transaction
- Transaction.Current = ct;
- */
- IntResourceManager irm = new IntResourceManager (1);
- using (TransactionScope scope = new TransactionScope (ct)) {
- Assert.AreEqual (ct, Transaction.Current, "#59");
- irm.Value = 2;
- /* Not completing this scope
- scope.Complete (); */
- }
- Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
- Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
- Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
- irm.Check ( 0, 0, 1, 0, "irm");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ExplicitTransaction6 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- ct.Commit ();
- ct.Commit ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ExplicitTransaction6a ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- irm.Value = 2;
- ct.Commit ();
- /* Using a already committed transaction in a new
- * TransactionScope
- */
- TransactionScope scope = new TransactionScope ( ct );
- }
- [Test]
- public void ExplicitTransaction6b ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
-
- Transaction.Current = ct;
- TransactionScope scope1 = new TransactionScope ();
- /* Enlist */
- irm.Value = 2;
- scope1.Complete ();
- try {
- ct.Commit ();
- } catch (TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
-
- scope1.Dispose ();
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Commit should've failed" );
- }
- [Test]
- public void ExplicitTransaction6c ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
- /* Enlist */
- irm.Value = 2;
- TransactionScope scope2 = new TransactionScope ();
- try {
- scope1.Dispose ();
- } catch (InvalidOperationException) {
- /* Error: TransactionScope nested incorrectly */
- irm.Check ( 0, 0, 1, 0, "irm" );
- scope2.Dispose ();
- Transaction.Current = null;
- return;
- }
- Assert.Fail ("Commit should've failed");
- }
- [Test]
- public void ExplicitTransaction6d ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- TransactionScope scope1 = new TransactionScope ();
- /* Enlist */
- irm.Value = 2;
- TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
- try {
- scope1.Dispose ();
- } catch (InvalidOperationException) {
- /* Error: TransactionScope nested incorrectly */
- scope2.Dispose ();
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Commit should've failed" );
- }
- [Test]
- public void ExplicitTransaction6e ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- TransactionScope scope1 = new TransactionScope ();
- /* Enlist */
- irm.Value = 2;
- TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
- try {
- scope1.Dispose ();
- } catch (InvalidOperationException) {
- /* Error: TransactionScope nested incorrectly */
- scope2.Dispose ();
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Commit should've failed" );
- }
- [Test]
- [ExpectedException (typeof (TransactionException))]
- public void ExplicitTransaction7 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager (1);
- irm.Value = 2;
- ct.Commit ();
- /* Cannot accept any new work now, so TransactionException */
- ct.Rollback ();
- }
- [Test]
- public void ExplicitTransaction8 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- using ( TransactionScope scope = new TransactionScope (ct) ) {
- irm.Value = 2;
- /* FIXME: Why TransactionAbortedException ?? */
- try {
- ct.Commit ();
- } catch ( TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- return;
- }
- Assert.Fail ( "Should not be reached" );
- }
- }
- [Test]
- public void ExplicitTransaction8a ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- using ( TransactionScope scope = new TransactionScope ( ct ) ) {
- irm.Value = 2;
- scope.Complete ();
- /* FIXME: Why TransactionAbortedException ?? */
- try {
- ct.Commit ();
- }
- catch ( TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- return;
- }
- Assert.Fail ( "Should not be reached" );
- }
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ExplicitTransaction9 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- ct.BeginCommit ( null, null );
- ct.BeginCommit ( null, null );
- }
- [Test]
- public void ExplicitTransaction10 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- irm.Value = 2;
- TransactionScope scope = new TransactionScope ( ct );
- Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
- //scope.Complete ();
- //scope.Dispose ();
- try {
- ct.Commit ();
- } catch ( TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- Transaction.Current = null;
- return;
- }
-
- Transaction.Current = null;
- Assert.Fail ("Expected TransactionAbortedException");
- }
- [Test]
- public void ExplicitTransaction10a ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- irm.Value = 2;
- Transaction.Current = null;
- TransactionScope scope = new TransactionScope ( ct );
- Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
- Transaction.Current = null;
- //scope2.Complete ();
- //scope2.Dispose ();
- try {
- ct.Commit ();
- }
- catch ( TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- Transaction.Current = null;
- return;
- }
- Transaction.Current = null;
- Assert.Fail ();
- }
- [Test]
- public void ExplicitTransaction10b ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Transaction.Current = ct;
- irm.Value = 2;
- Transaction.Current = null;
- TransactionScope scope = new TransactionScope ( ct );
- Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
- //scope2.Complete ();
- //scope2.Dispose ();
- IAsyncResult ar = ct.BeginCommit ( null, null );
- try {
- ct.EndCommit (ar);
- }
- catch ( TransactionAbortedException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- Transaction.Current = null;
- return;
- }
- Transaction.Current = null;
- Assert.Fail ();
- }
- [Test]
- [ExpectedException ( typeof (ArgumentException))]
- public void ExplicitTransaction12 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- irm.FailPrepare = true;
- ct.BeginCommit ( null, null );
- ct.EndCommit ( null );
- }
- [Test]
- public void ExplicitTransaction13 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Assert.IsNull ( Transaction.Current );
- Transaction.Current = ct;
- irm.Value = 2;
- irm.FailPrepare = true;
- try {
- ct.Commit ();
- } catch ( TransactionAbortedException ) {
- Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
- try {
- ct.BeginCommit ( null, null );
- } catch (Exception) {
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Should not be reached(2)" );
- }
- Assert.Fail ("Should not be reached");
- }
- [Test]
- public void ExplicitTransaction14 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- Assert.IsNull ( Transaction.Current );
- Transaction.Current = ct;
- irm.Value = 2;
- ct.Commit ();
- Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
- try {
- ct.BeginCommit ( null, null );
- }
- catch ( Exception) {
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Should not be reached" );
- }
- [Test]
- public void ExplicitTransaction15 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm = new IntResourceManager ( 1 );
- IntResourceManager irm2 = new IntResourceManager ( 3 );
- Assert.IsNull ( Transaction.Current );
- Transaction.Current = ct;
- try {
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- Transaction.Current = new CommittableTransaction ();
- irm2.Value = 6;
- }
- } catch (InvalidOperationException) {
- irm.Check ( 0, 0, 1, 0, "irm" );
- irm2.Check ( 0, 0, 1, 0, "irm2" );
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Should not be reached" );
- }
- [Test]
- public void ExplicitTransaction16 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager irm0 = new IntResourceManager ( 3 );
- IntResourceManager irm = new IntResourceManager ( 1 );
- Assert.IsNull ( Transaction.Current );
- Transaction.Current = ct;
- irm.FailPrepare = true;
- irm.FailWithException = true;
- irm.Value = 2;
- irm0.Value = 6;
- try {
- ct.Commit ();
- } catch (TransactionAbortedException e) {
- Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
- Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
- irm.Check ( 1, 0, 0, 0, "irm" );
- irm0.Check ( 0, 0, 1, 0, "irm0" );
- Transaction.Current = null;
- return;
- }
-
- Assert.Fail ( "Should not be reached" );
- }
- #endregion
- }
- }
|