<commit_before>using System;
using System.Collections;
using System.Data;
using System.Transactions;
using NUnit.Framework;

namespace NHibernate.Test.SystemTransactions
{
	[TestFixture]
	public class TransactionNotificationFixture : TestCase
	{
		protected override IList Mappings
		{
			get { return new string[] {}; }
		}


		[Test]
		public void NoTransaction()
		{
			var interceptor = new RecordingInterceptor();
			using (sessions.OpenSession(interceptor))
			{
				Assert.AreEqual(0, interceptor.afterTransactionBeginCalled);
				Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
				Assert.AreEqual(0, interceptor.afterTransactionCompletionCalled);
			}
		}

		[Test]
		public void AfterBegin()
		{
			var 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);
			}
		}

		[Test]
		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);
			
		}

		[Test]
		public void Rollback()
		{
			var interceptor = new RecordingInterceptor();
			using (new TransactionScope())
			using (sessions.OpenSession(interceptor))
			{
			}
			Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
			Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
		}

		[Test]
		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);
		}

		[Test]
		public void OneTransactionScopesInsideOneSession()
		{
			var interceptor = new RecordingInterceptor();
			using (var session = sessions.OpenSession(interceptor))
			{
				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);
		}


		[Description("NH2128, NH3572")]
		[Theory]
		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));
		}


		[Description("NH2128")]
		[Theory]
		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);
				}
			}

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

	}
}<commit_msg>TransactionNotificationFixture: ShouldNotifyAfterDistributedTransactionWithOwnConnection(): Transaction completion may happen asynchronously, so allow some delay. Affects some drivers.<commit_after>using System;
using System.Collections;
using System.Data;
using System.Threading;
using System.Transactions;
using NUnit.Framework;

namespace NHibernate.Test.SystemTransactions
{
	[TestFixture]
	public class TransactionNotificationFixture : TestCase
	{
		protected override IList Mappings
		{
			get { return new string[] {}; }
		}


		[Test]
		public void NoTransaction()
		{
			var interceptor = new RecordingInterceptor();
			using (sessions.OpenSession(interceptor))
			{
				Assert.AreEqual(0, interceptor.afterTransactionBeginCalled);
				Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
				Assert.AreEqual(0, interceptor.afterTransactionCompletionCalled);
			}
		}

		[Test]
		public void AfterBegin()
		{
			var 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);
			}
		}

		[Test]
		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);
			
		}

		[Test]
		public void Rollback()
		{
			var interceptor = new RecordingInterceptor();
			using (new TransactionScope())
			using (sessions.OpenSession(interceptor))
			{
			}
			Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
			Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
		}

		[Test]
		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);
		}

		[Test]
		public void OneTransactionScopesInsideOneSession()
		{
			var interceptor = new RecordingInterceptor();
			using (var session = sessions.OpenSession(interceptor))
			{
				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);
		}


		[Description("NH2128, NH3572")]
		[Theory]
		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));
		}


		[Description("NH2128")]
		[Theory]
		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));
		}

	}
}
{'fraction_non_alphanumeric': 0.10873296314992428, 'fraction_numerical': 0.010903584048460374, 'mean_word_length': 3.739712918660287, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3404586', 'n_tokens_mistral': 3676, 'n_tokens_neox': 2897, 'n_words': 590}