Exemplo n.º 1
0
        public void TransactionFailedOnRollbackEvent()
        {
            bool transactionCommittedEventTriggered  = false;
            bool transactionRolledBackEventTriggered = false;
            bool transactionFailedEventTriggered     = false;

            tm.TransactionCompleted  += delegate { transactionCommittedEventTriggered = true; };
            tm.TransactionRolledBack += delegate { transactionRolledBackEventTriggered = true; };
            tm.TransactionFailed     += delegate { transactionFailedEventTriggered = true; };

            ITransaction transaction = tm.CreateTransaction(
                TransactionMode.Requires, IsolationMode.Unspecified);

            ResourceImpl resource = new ThrowsExceptionResourceImpl(false, true);

            transaction.Enlist(resource);

            Assert.IsFalse(transactionCommittedEventTriggered);
            Assert.IsFalse(transactionRolledBackEventTriggered);
            Assert.IsFalse(transactionFailedEventTriggered);

            transaction.Begin();

            Assert.IsFalse(transactionCommittedEventTriggered);
            Assert.IsFalse(transactionRolledBackEventTriggered);
            Assert.IsFalse(transactionFailedEventTriggered);

            TransactionException exception = null;

            try
            {
                transaction.Rollback();
            }
            catch (TransactionException transactionError)
            {
                exception = transactionError;
            }

            Assert.IsFalse(transactionCommittedEventTriggered);
            Assert.IsFalse(transactionRolledBackEventTriggered);
            Assert.IsTrue(transactionFailedEventTriggered);

            Assert.IsNotNull(exception);
            Assert.IsInstanceOf(typeof(RollbackResourceException), exception);
        }
        public void WhenOneResourceFails_OtherResourcesAreNotCommitted()
        {
            var first = new ResourceImpl();
            var rfail = new ThrowsExceptionResourceImpl(true, false);
            var rsucc = new ResourceImpl();

            var t = tm.CreateTransaction(TransactionMode.Requires, IsolationMode.Unspecified);

            t.Enlist(first);
            t.Enlist(rfail);
            t.Enlist(rsucc);

            t.Begin();

            Assert.That(rfail.Started);
            Assert.That(rsucc.Started);

            Assert.Throws(typeof(CommitResourceException), t.Commit);

            Assert.That(first.Committed);
            Assert.That(rfail.Committed, Is.False);
            Assert.That(rsucc.Committed, Is.False);
        }
		public void WhenOneResourceFails_OtherResourcesAreNotCommitted()
		{
			var first = new ResourceImpl();
			var rfail = new ThrowsExceptionResourceImpl(true, false);
			var rsucc = new ResourceImpl();

			var t = tm.CreateTransaction(TransactionMode.Requires, IsolationMode.Unspecified);
			t.Enlist(first);
			t.Enlist(rfail);
			t.Enlist(rsucc);

			t.Begin();

			Assert.That(rfail.Started);
			Assert.That(rsucc.Started);

			Assert.Throws(typeof(CommitResourceException), t.Commit);

			Assert.That(first.Committed);
			Assert.That(rfail.Committed, Is.False);
			Assert.That(rsucc.Committed, Is.False);
		}
		public void TransactionFailedOnRollbackEvent()
		{
			bool transactionCommittedEventTriggered = false;
			bool transactionRolledBackEventTriggered = false;
			bool transactionFailedEventTriggered = false;

			tm.TransactionCompleted += delegate { transactionCommittedEventTriggered = true; };
			tm.TransactionRolledBack += delegate { transactionRolledBackEventTriggered = true; };
			tm.TransactionFailed += delegate { transactionFailedEventTriggered = true; };

			ITransaction transaction = tm.CreateTransaction(
				TransactionMode.Requires, IsolationMode.Unspecified);

			ResourceImpl resource = new ThrowsExceptionResourceImpl(false, true);

			transaction.Enlist(resource);

			Assert.IsFalse(transactionCommittedEventTriggered);
			Assert.IsFalse(transactionRolledBackEventTriggered);
			Assert.IsFalse(transactionFailedEventTriggered);

			transaction.Begin();

			Assert.IsFalse(transactionCommittedEventTriggered);
			Assert.IsFalse(transactionRolledBackEventTriggered);
			Assert.IsFalse(transactionFailedEventTriggered);

			TransactionException exception = null;

			try
			{
				transaction.Rollback();
			}
			catch (TransactionException transactionError)
			{
				exception = transactionError;
			}

			Assert.IsFalse(transactionCommittedEventTriggered);
			Assert.IsFalse(transactionRolledBackEventTriggered);
			Assert.IsTrue(transactionFailedEventTriggered);

			Assert.IsNotNull(exception);
			Assert.IsInstanceOf(typeof(RollbackResourceException), exception);
		}