| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- //
- // Tests for combination of volatile & durable resource manangers
- //
- // Author:
- // Ankit Jain <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
- //
- using System;
- using System.Transactions;
- using NUnit.Framework;
- namespace MonoTests.System.Transactions {
- [TestFixture]
- public class EnlistTest {
- #region Vol1_Dur0
- /* Single volatile resource, SPC happens */
- [Test]
- public void Vol1_Dur0 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- irm.UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- irm.CheckSPC ("irm");
- }
- [Test]
- public void Vol1_Dur0_2PC ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- irm.Check2PC ("irm");
- }
- /* Single volatile resource, SPC happens */
- [Test]
- public void Vol1_Dur0_Fail1 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- irm.UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- /* Not completing this..
- scope.Complete ();*/
- }
- irm.Check ( 0, 0, 0, 1, 0, "irm" );
- }
- [Test]
- [ExpectedException ( typeof ( TransactionAbortedException ) )]
- public void Vol1_Dur0_Fail2 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- irm.FailPrepare = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- }
- [Test]
- [ExpectedException ( typeof ( TransactionAbortedException ) )]
- public void Vol1_Dur0_Fail3 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- irm.UseSingle = true;
- irm.FailSPC = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- }
- #endregion
- #region Vol2_Dur0
- /* >1 volatile, 2PC */
- [Test]
- public void Vol2_Dur0_SPC ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- IntResourceManager irm2 = new IntResourceManager (3);
- irm.UseSingle = true;
- irm2.UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- irm2.Value = 6;
- scope.Complete ();
- }
- irm.Check2PC ( "irm" );
- irm2.Check2PC ( "irm2" );
- }
- #endregion
- #region Vol0_Dur1
- /* 1 durable */
- [Test]
- public void Vol0_Dur1 ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- irm.Volatile = false;
- irm.UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- irm.CheckSPC ( "irm" );
- }
- /* We support only 1 durable with 2PC
- * On .net, this becomes a distributed transaction
- */
- [Test]
- [Category ("NotWorking")]
- public void Vol0_Dur1_2PC ()
- {
- IntResourceManager irm = new IntResourceManager (1);
- /* Durable resource enlisted with a IEnlistedNotification
- * object
- */
- irm.Volatile = false;
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- }
- [Test]
- public void Vol0_Dur1_Fail ()
- {
- IntResourceManager irm = new IntResourceManager ( 1 );
- /* Durable resource enlisted with a IEnlistedNotification
- * object
- */
- irm.Volatile = false;
- irm.FailSPC = true;
- irm.UseSingle = true;
- try {
- using (TransactionScope scope = new TransactionScope ()) {
- irm.Value = 2;
- scope.Complete ();
- }
- }
- catch (TransactionAbortedException) {
- irm.Check ( 1, 0, 0, 0, 0, "irm" );
- return;
- }
- Assert.Fail ();
- }
- #endregion
- #region Vol2_Dur1
- /* >1vol + 1 durable */
- [Test]
- public void Vol2_Dur1 ()
- {
- IntResourceManager [] irm = new IntResourceManager [4];
- irm [0] = new IntResourceManager ( 1 );
- irm [1] = new IntResourceManager ( 3 );
- irm [2] = new IntResourceManager ( 5 );
- irm [3] = new IntResourceManager ( 7 );
- irm [0].Volatile = false;
- for ( int i = 0; i < 4; i++ )
- irm [i].UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- irm [2].Value = 10;
- irm [3].Value = 14;
- scope.Complete ();
- }
- irm [0].CheckSPC ( "irm [0]" );
- /* Volatile RMs get 2PC */
- for (int i = 1; i < 4; i++)
- irm [i].Check2PC ( "irm [" + i + "]" );
- }
- /* >1vol + 1 durable
- * Durable fails SPC
- */
- [Test]
- public void Vol2_Dur1_Fail1 ()
- {
- IntResourceManager [] irm = new IntResourceManager [4];
- irm [0] = new IntResourceManager (1);
- irm [1] = new IntResourceManager (3);
- irm [2] = new IntResourceManager (5);
- irm [3] = new IntResourceManager (7);
- irm [0].Volatile = false;
- irm [0].FailSPC = true;
- for ( int i = 0; i < 4; i++ )
- irm [i].UseSingle = true;
- /* Durable RM irm[0] does Abort on SPC, so
- * all volatile RMs get Rollback */
- try {
- using (TransactionScope scope = new TransactionScope ()) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- irm [2].Value = 10;
- irm [3].Value = 14;
- scope.Complete ();
- }
- }
- catch (TransactionAbortedException) {
- irm [0].CheckSPC ( "irm [0]" );
- /* Volatile RMs get 2PC Prepare, and then get rolled back */
- for (int i = 1; i < 4; i++)
- irm [i].Check ( 0, 1, 0, 1, 0, "irm [" + i + "]" );
- }
- }
- /* >1vol + 1 durable
- * durable doesn't complete SPC
- */
- [Test]
- [Ignore ( "Correct this test, it should throw TimeOutException or something" )]
- public void Vol2_Dur1_Fail2 ()
- {
- IntResourceManager [] irm = new IntResourceManager [4];
- irm [0] = new IntResourceManager (1);
- irm [1] = new IntResourceManager (3);
- irm [2] = new IntResourceManager (5);
- irm [3] = new IntResourceManager (7);
- irm [0].Volatile = false;
- irm [0].IgnoreSPC = true;
- for ( int i = 0; i < 4; i++ )
- irm [i].UseSingle = true;
- /* Durable RM irm[2] does on SPC, so
- * all volatile RMs get Rollback */
- try {
- using (TransactionScope scope = new TransactionScope ( TransactionScopeOption.Required, new TimeSpan ( 0, 0, 5 ) )) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- irm [2].Value = 10;
- irm [3].Value = 14;
- scope.Complete ();
- }
- }
- catch (TransactionAbortedException) {
- irm [0].CheckSPC ( "irm [0]" );
- /* Volatile RMs get 2PC Prepare, and then get rolled back */
- for (int i = 1; i < 4; i++)
- irm [i].Check ( 0, 1, 0, 1, 0, "irm [" + i + "]" );
- }
- }
- /* >1vol + 1 durable
- * Volatile fails Prepare
- */
- [Test]
- public void Vol2_Dur1_Fail3 ()
- {
- IntResourceManager [] irm = new IntResourceManager [4];
- irm [0] = new IntResourceManager ( 1 );
- irm [1] = new IntResourceManager ( 3 );
- irm [2] = new IntResourceManager ( 5 );
- irm [3] = new IntResourceManager ( 7 );
- irm [0].Volatile = false;
- irm [2].FailPrepare = true;
- for ( int i = 0; i < 4; i++ )
- irm [i].UseSingle = true;
- /* Durable RM irm[2] does on SPC, so
- * all volatile RMs get Rollback */
- try {
- using (TransactionScope scope = new TransactionScope ()) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- irm [2].Value = 10;
- irm [3].Value = 14;
- scope.Complete ();
- }
- }
- catch (TransactionAbortedException) {
- irm [0].Check ( 0, 0, 0, 1, 0, "irm [0]");
- /* irm [1] & [2] get prepare,
- * [2] -> ForceRollback,
- * [1] & [3] get rollback,
- * [0](durable) gets rollback */
- irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
- irm [2].Check ( 0, 1, 0, 0, 0, "irm [2]" );
- irm [3].Check ( 0, 0, 0, 1, 0, "irm [3]" );
- return;
- }
- Assert.Fail ( "Expected TransactionAbortedException" );
- }
- [Test]
- public void Vol2_Dur1_Fail4 ()
- {
- IntResourceManager [] irm = new IntResourceManager [2];
- irm [0] = new IntResourceManager ( 1 );
- irm [1] = new IntResourceManager ( 3 );
- irm [0].Volatile = false;
- irm [0].FailSPC = true;
- irm [0].FailWithException = true;
- for ( int i = 0; i < 2; i++ )
- irm [i].UseSingle = true;
- /* Durable RM irm[2] does on SPC, so
- * all volatile RMs get Rollback */
- try {
- using ( TransactionScope scope = new TransactionScope () ) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- scope.Complete ();
- }
- }
- catch ( TransactionAbortedException e) {
- Assert.IsNotNull ( e.InnerException, "Expected e.InnerException == NotSupportedException, but got None");
- Assert.AreEqual ( typeof ( NotSupportedException ), e.InnerException.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e.GetType () );
- irm [0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
- irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
- return;
- }
- Assert.Fail ( "Expected TransactionAbortedException" );
- }
- [Test]
- public void Vol2_Dur1_Fail5 ()
- {
- CommittableTransaction ct = new CommittableTransaction ();
- IntResourceManager [] irm = new IntResourceManager [2];
- irm [0] = new IntResourceManager ( 1 );
- irm [1] = new IntResourceManager ( 3 );
- Transaction.Current = ct;
- irm [0].Volatile = false;
- irm [0].FailSPC = true;
- irm [0].FailWithException = true;
- for ( int i = 0; i < 2; i++ )
- irm [i].UseSingle = true;
- /* Durable RM irm[2] does on SPC, so
- * all volatile RMs get Rollback */
-
- using ( TransactionScope scope = new TransactionScope () ) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- scope.Complete ();
- }
- try {
- ct.Commit ();
- }
- catch ( TransactionAbortedException e ) {
- Assert.IsNotNull ( e.InnerException, "Expected e.InnerException == NotSupportedException, but got None" );
- Assert.AreEqual ( typeof ( NotSupportedException ), e.InnerException.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e.GetType () );
- irm [0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
- irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
- try {
- ct.Commit ();
- }
- catch (InvalidOperationException x ) {
- Assert.IsNull ( x.InnerException);
- Transaction.Current = null;
- return;
- }
- Assert.Fail ( "Should not be reached" );
- }
- Assert.Fail ( "Expected TransactionAbortedException" );
- }
- #endregion
- #region Others
- /* >1vol
- * > 1 durable, On .net this becomes a distributed transaction
- * We don't support this in mono yet.
- */
- [Test]
- [Category ("NotWorking")]
- public void Vol0_Dur2 ()
- {
- IntResourceManager [] irm = new IntResourceManager [2];
- irm [0] = new IntResourceManager ( 1 );
- irm [1] = new IntResourceManager ( 3 );
- irm [0].Volatile = false;
- irm [1].Volatile = false;
- for ( int i = 0; i < 2; i++ )
- irm [i].UseSingle = true;
- using (TransactionScope scope = new TransactionScope ()) {
- irm [0].Value = 2;
- irm [1].Value = 6;
- scope.Complete ();
- }
- }
- #endregion
- }
- }
|