public void NoTransaction()
		{
			RecordingInterceptor interceptor = new RecordingInterceptor();
			using (sessions.OpenSession(interceptor))
			{
				Assert.IsFalse(interceptor.afterTransactionBeginCalled);
				Assert.IsFalse(interceptor.beforeTransactionCompletionCalled);
				Assert.IsFalse(interceptor.afterTransactionCompletionCalled);
			}
		}
Exemple #2
0
		public void Rollback()
		{
			var interceptor = new RecordingInterceptor();
			using (new TransactionScope())
			using (sessions.OpenSession(interceptor))
			{
			}
			Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
			Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
		}
		public void NoTransaction()
		{
			var interceptor = new RecordingInterceptor();
			using (sessions.OpenSession(interceptor))
			{
				Assert.That(interceptor.afterTransactionBeginCalled, Is.EqualTo(0));
				Assert.That(interceptor.beforeTransactionCompletionCalled, Is.EqualTo(0));
				Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(0));
			}
		}
		public void AfterBegin()
		{
			RecordingInterceptor interceptor = new RecordingInterceptor();
			using (new TransactionScope()) 
			using (sessions.OpenSession(interceptor))
			{
				Assert.AreEqual(1, interceptor.afterTransactionBeginCalled);
				Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
				Assert.AreEqual(0, interceptor.afterTransactionCompletionCalled);
			}
		}
		public void Rollback()
		{
			RecordingInterceptor interceptor = new RecordingInterceptor();
			using (ISession session = sessions.OpenSession(interceptor))
			{
				ITransaction tx = session.BeginTransaction();
				tx.Rollback();
				Assert.IsFalse(interceptor.beforeTransactionCompletionCalled);
				Assert.IsTrue(interceptor.afterTransactionCompletionCalled);
			}
		}
		public void Rollback()
		{
			var interceptor = new RecordingInterceptor();
			using (ISession session = sessions.OpenSession(interceptor))
			{
				ITransaction tx = session.BeginTransaction();
				tx.Rollback();
				Assert.That(interceptor.afterTransactionBeginCalled, Is.EqualTo(1));
				Assert.That(interceptor.beforeTransactionCompletionCalled, Is.EqualTo(0));
				Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
			}
		}
Exemple #7
0
		public void Complete()
		{
			var interceptor = new RecordingInterceptor();
			ISession session;
			using(var scope = new TransactionScope())
			{
				session = sessions.OpenSession(interceptor);
				scope.Complete();
			}
			session.Dispose();
			Assert.AreEqual(1, interceptor.beforeTransactionCompletionCalled);
			Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
			
		}
Exemple #8
0
        public void OneTransactionScopesInsideOneSession()
        {
            var interceptor = new RecordingInterceptor();

            using (var session = sessions.WithOptions().Interceptor(interceptor).OpenSession())
            {
                using (var scope = new TransactionScope())
                {
                    session.CreateCriteria <object>().List();
                    scope.Complete();
                }
            }
            Assert.AreEqual(1, interceptor.afterTransactionBeginCalled);
            Assert.AreEqual(1, interceptor.beforeTransactionCompletionCalled);
            Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
        }
Exemple #9
0
        public async Task OneTransactionScopesInsideOneSessionAsync()
        {
            var interceptor = new RecordingInterceptor();

            using (var session = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
            {
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await(session.CreateCriteria <object>().ListAsync());
                    scope.Complete();
                }
            }
            Assert.AreEqual(1, interceptor.afterTransactionBeginCalled);
            Assert.AreEqual(1, interceptor.beforeTransactionCompletionCalled);
            Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
        }
        public void ExceptionInAfterCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            i.AfterCallAction = delegate(CallInfo ci) { throw new NullReferenceException(); };

            bool exceptionBubbledUp = false;

            try
            {
                o.SomeMethod("SomeString", 17);
            }
            catch (Exception e)
            {
                exceptionBubbledUp = true;
                Assert.IsInstanceOfType(e, typeof(InterceptionException));
                Assert.IsNotNull(e.InnerException);

                Assert.IsNotNull(((InterceptionException)e).Arguments);
                Assert.AreEqual("SomeMethod", ((InterceptionException)e).MethodName);

                Assert.AreEqual(2, ((InterceptionException)e).Arguments.Length);
                Assert.AreEqual("SomeString", ((InterceptionException)e).Arguments[0]);
                Assert.AreEqual(17, ((InterceptionException)e).Arguments[1]);

                Assert.IsFalse(((InterceptionException)e).BeforeCall);
                Assert.IsTrue(((InterceptionException)e).AfterCall);

                Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException));
                Assert.IsNull(e.InnerException.InnerException);
            }

            Assert.IsTrue(exceptionBubbledUp);

            Assert.IsNotNull(impl.SomeMethodS);
            Assert.AreEqual("SomeString", impl.SomeMethodS);
            Assert.AreEqual(17, impl.SomeMethodI);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsNull(i.AfterCallInfo.ThrownException);
        }
        public void Rollback()
        {
            var interceptor = new RecordingInterceptor();

            using (var session = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
                using (var tx = session.BeginTransaction())
                {
                    var synchronisation = new Synchronization();
                    tx.RegisterSynchronization(synchronisation);
                    tx.Rollback();
                    Assert.That(interceptor.afterTransactionBeginCalled, Is.EqualTo(1), "interceptor begin");
                    Assert.That(interceptor.beforeTransactionCompletionCalled, Is.EqualTo(0), "interceptor before");
                    Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1), "interceptor after");
                    Assert.That(synchronisation.BeforeExecutions, Is.EqualTo(0), "sync before");
                    Assert.That(synchronisation.AfterExecutions, Is.EqualTo(1), "sync after");
                }
        }
        public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doCommit)
        {
            // Note: For distributed transaction, calling Close() on the session isn't
            // supported, so we don't need to test that scenario.

            var      interceptor = new RecordingInterceptor();
            ISession s1          = null;
            ISession s2          = null;

            using (var tx = new TransactionScope())
            {
                using (IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection())
                    using (IDbConnection ownConnection2 = sessions.ConnectionProvider.GetConnection())
                    {
                        try
                        {
                            s1 = sessions.OpenSession(ownConnection1, interceptor);
                            s2 = sessions.OpenSession(ownConnection2, interceptor);

                            s1.CreateCriteria <object>().List();
                            s2.CreateCriteria <object>().List();
                        }
                        finally
                        {
                            if (s1 != null)
                            {
                                s1.Dispose();
                            }
                            if (s2 != null)
                            {
                                s2.Dispose();
                            }
                        }

                        if (doCommit)
                        {
                            tx.Complete();
                        }
                    }
            }

            Assert.That(s1.IsOpen, Is.False);
            Assert.That(s2.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(2));
        }
		public void ShouldNotifyAfterTransaction(bool usePrematureClose)
		{
			var interceptor = new RecordingInterceptor();
			ISession s;

			using (s = OpenSession(interceptor))
			using (s.BeginTransaction())
			{
				s.CreateCriteria<object>().List();

				// Call session close while still inside transaction?
				if (usePrematureClose)
					s.Close();
			}

			Assert.That(s.IsOpen, Is.False);
			Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
		}
Exemple #14
0
        public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doCommit)
        {
            // Note: For distributed transaction, calling Close() on the session isn't
            // supported, so we don't need to test that scenario.

            var      interceptor = new RecordingInterceptor();
            ISession s1          = null;

            using (var tx = new TransactionScope())
            {
                IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection();

                try
                {
                    try
                    {
                        s1 = sessions.OpenSession(ownConnection1, interceptor);

                        s1.CreateCriteria <object>().List();
                    }
                    finally
                    {
                        if (s1 != null)
                        {
                            s1.Dispose();
                        }
                    }

                    if (doCommit)
                    {
                        tx.Complete();
                    }
                }
                finally
                {
                    sessions.ConnectionProvider.CloseConnection(ownConnection1);
                }
            }

            // Transaction completion may happen asynchronously, so allow some delay.
            Assert.That(() => s1.IsOpen, Is.False.After(500, 100));

            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
        }
Exemple #15
0
        public TTarget GetRecordingTarget <TTarget>(TTarget target)
            where TTarget : class
        {
            // only public interface recording is supported
            var targetType = typeof(TTarget);

            if (!targetType.IsInterface || !targetType.IsPublic)
            {
                throw new NotSupportedException();
            }

            var recordingInterceptor = new RecordingInterceptor <TTarget>(_invocationWriter);

            return(_generator.CreateInterfaceProxyWithTarget <TTarget>(target,
#if DEBUG
                                                                       new RecordingInterceptor <TTarget>(new ConsoleWriter()),
#endif
                                                                       recordingInterceptor));
        }
Exemple #16
0
        public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doCommit)
        {
            if (!Sfi.ConnectionProvider.Driver.SupportsSystemTransactions)
            {
                Assert.Ignore("Driver does not support System.Transactions. Ignoring test.");
            }

            // Note: For system transaction, calling Close() on the session isn't
            // supported, so we don't need to test that scenario.

            var      interceptor = new RecordingInterceptor();
            ISession s1;

            var ownConnection1 = Sfi.ConnectionProvider.GetConnection();

            try
            {
                using (var tx = new TransactionScope())
                {
                    ownConnection1.EnlistTransaction(System.Transactions.Transaction.Current);
                    using (s1 = Sfi.WithOptions().Connection(ownConnection1).Interceptor(interceptor).OpenSession())
                    {
                        s1.CreateCriteria <object>().List();
                    }

                    if (doCommit)
                    {
                        tx.Complete();
                    }
                }
            }
            finally
            {
                Sfi.ConnectionProvider.CloseConnection(ownConnection1);
            }

            // Transaction completion may happen asynchronously, so allow some delay. Odbc promotes
            // this test to distributed and have that delay, by example.
            Assert.That(() => s1.IsOpen, Is.False.After(500, 100), "Session not closed.");

            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
        }
Exemple #17
0
        public async Task ShouldNotifyAfterTransactionAsync(bool usePrematureClose)
        {
            var      interceptor = new RecordingInterceptor();
            ISession s;

            using (s = OpenSession(interceptor))
                using (s.BeginTransaction())
                {
                    await(s.CreateCriteria <object>().ListAsync());

                    // Call session close while still inside transaction?
                    if (usePrematureClose)
                    {
                        s.Close();
                    }
                }

            Assert.That(s.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
        }
        public void ExceptionInAdaptedCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            bool exceptionBubbledUp = false;

            try
            {
                o.ThrowException();
            }
            catch (Exception e)
            {
                exceptionBubbledUp = true;
                Assert.IsInstanceOfType(e, typeof(AdaptedException));
                Assert.IsNotNull(e.InnerException);

                Assert.IsNotNull(((AdaptedException)e).Arguments);
                Assert.AreEqual("ThrowException", ((AdaptedException)e).MethodName);
                Assert.AreEqual(0, ((AdaptedException)e).Arguments.Length);

                Assert.IsFalse(((AdaptedException)e).BeforeCall);
                Assert.IsTrue(((AdaptedException)e).DuringCall);
                Assert.IsFalse(((AdaptedException)e).AfterCall);

                Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException));
                Assert.IsNull(e.InnerException.InnerException);
            }

            Assert.IsTrue(exceptionBubbledUp);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsNotNull(i.AfterCallInfo.ThrownException);

            Assert.IsInstanceOfType(i.AfterCallInfo.ThrownException, typeof(NullReferenceException));
        }
Exemple #19
0
        public async Task ShouldNotifyAfterDistributedTransactionAsync(bool doCommit)
        {
            // Note: For distributed transaction, calling Close() on the session isn't
            // supported, so we don't need to test that scenario.

            var      interceptor = new RecordingInterceptor();
            ISession s1          = null;
            ISession s2          = null;

            using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    s1 = OpenSession(interceptor);
                    s2 = OpenSession(interceptor);

                    await(s1.CreateCriteria <object>().ListAsync());
                    await(s2.CreateCriteria <object>().ListAsync());
                }
                finally
                {
                    if (s1 != null)
                    {
                        s1.Dispose();
                    }
                    if (s2 != null)
                    {
                        s2.Dispose();
                    }
                }

                if (doCommit)
                {
                    tx.Complete();
                }
            }

            Assert.That(s1.IsOpen, Is.False);
            Assert.That(s2.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(2));
        }
Exemple #20
0
		public void TwoTransactionScopesInsideOneSession()
		{
			var interceptor = new RecordingInterceptor();
			using (var session = sessions.OpenSession(interceptor))
			{
				using (var scope = new TransactionScope())
				{
					session.CreateCriteria<object>().List();
					scope.Complete();
				}

				using (var scope = new TransactionScope())
				{
					session.CreateCriteria<object>().List();
					scope.Complete();
				}
			}
			Assert.AreEqual(2, interceptor.afterTransactionBeginCalled);
			Assert.AreEqual(2, interceptor.beforeTransactionCompletionCalled);
			Assert.AreEqual(2, interceptor.afterTransactionCompletionCalled);
		}
Exemple #21
0
        public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync(bool usePrematureClose)
        {
            var      interceptor = new RecordingInterceptor();
            ISession s;

            using (var ownConnection = await(Sfi.ConnectionProvider.GetConnectionAsync(CancellationToken.None)))
            {
                using (s = Sfi.WithOptions().Connection(ownConnection).Interceptor(interceptor).OpenSession())
                    using (s.BeginTransaction())
                    {
                        await(s.CreateCriteria <object>().ListAsync());

                        // Call session close while still inside transaction?
                        if (usePrematureClose)
                        {
                            s.Close();
                        }
                    }
            }

            Assert.That(s.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
        }
        public void ShouldNotifyAfterTransactionWithOwnConnection(bool usePrematureClose)
        {
            var      interceptor = new RecordingInterceptor();
            ISession s;

            using (IDbConnection ownConnection = sessions.ConnectionProvider.GetConnection())
            {
                using (s = sessions.OpenSession(ownConnection, interceptor))
                    using (s.BeginTransaction())
                    {
                        s.CreateCriteria <object>().List();

                        // Call session close while still inside transaction?
                        if (usePrematureClose)
                        {
                            s.Close();
                        }
                    }
            }

            Assert.That(s.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
        }
        public void ShouldNotifyAfterTransaction(bool usePrematureClose)
        {
            var      interceptor     = new RecordingInterceptor();
            var      synchronisation = new Synchronization();
            ISession s;

            using (s = OpenSession(interceptor))
                using (var t = s.BeginTransaction())
                {
                    t.RegisterSynchronization(synchronisation);
                    s.CreateCriteria <object>().List();

                    // Call session close while still inside transaction?
                    if (usePrematureClose)
                    {
                        s.Close();
                    }
                }

            Assert.That(s.IsOpen, Is.False);
            Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1), "interceptor");
            Assert.That(synchronisation.AfterExecutions, Is.EqualTo(1), "sync");
        }
Exemple #24
0
        public void TwoTransactionScopesInsideOneSession()
        {
            IgnoreIfTransactionScopeInsideSessionIsNotSupported();

            var interceptor = new RecordingInterceptor();

            using (var session = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
            {
                using (var scope = new TransactionScope())
                {
                    session.CreateCriteria <object>().List();
                    scope.Complete();
                }

                using (var scope = new TransactionScope())
                {
                    session.CreateCriteria <object>().List();
                    scope.Complete();
                }
            }
            Assert.AreEqual(2, interceptor.afterTransactionBeginCalled);
            Assert.AreEqual(2, interceptor.beforeTransactionCompletionCalled);
            Assert.AreEqual(2, interceptor.afterTransactionCompletionCalled);
        }
Exemple #25
0
		public void ShouldNotifyAfterDistributedTransaction(bool doCommit)
		{
			// Note: For distributed transaction, calling Close() on the session isn't
			// supported, so we don't need to test that scenario.

			var interceptor = new RecordingInterceptor();
			ISession s1 = null;
			ISession s2 = null;

			using (var tx = new TransactionScope())
			{
				try
				{
					s1 = OpenSession(interceptor);
					s2 = OpenSession(interceptor);

					s1.CreateCriteria<object>().List();
					s2.CreateCriteria<object>().List();
				}
				finally
				{
					if (s1 != null)
						s1.Dispose();
					if (s2 != null)
						s2.Dispose();
				}

				if (doCommit)
					tx.Complete();
			}

			Assert.That(s1.IsOpen, Is.False);
			Assert.That(s2.IsOpen, Is.False);
			Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(2));
		}
Exemple #26
0
		public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doCommit)
		{
			// Note: For distributed transaction, calling Close() on the session isn't
			// supported, so we don't need to test that scenario.

			var interceptor = new RecordingInterceptor();
			ISession s1 = null;

			using (var tx = new TransactionScope())
			{
				IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection();

				try
				{
					try
					{
						s1 = sessions.OpenSession(ownConnection1, interceptor);

						s1.CreateCriteria<object>().List();
					}
					finally
					{
						if (s1 != null)
							s1.Dispose();
					}

					if (doCommit)
						tx.Complete();
				}
				finally
				{
					sessions.ConnectionProvider.CloseConnection(ownConnection1);
				}
			}

			// Transaction completion may happen asynchronously, so allow some delay.
			Assert.That(() => s1.IsOpen, Is.False.After(500, 100));

			Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
		}