From 25ec045f383600d2333e58ec85c93a1c0699611e Mon Sep 17 00:00:00 2001 From: "mihail.iwanow" Date: Thu, 7 Mar 2019 10:12:20 +0100 Subject: [PATCH 1/2] Add transaction scope and distributed transaction tests --- .../Basic/TransactionScopeDistributed.cs | 123 ++++++++++++++++++ .../Integration/Basic/TransactionScopeTest.cs | 56 ++++++++ 2 files changed, 179 insertions(+) create mode 100644 Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs create mode 100644 Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs diff --git a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs new file mode 100644 index 00000000..88279683 --- /dev/null +++ b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs @@ -0,0 +1,123 @@ +using System.Collections.Generic; +using System.Linq; +using System.Transactions; +using NHibernate.Envers.Configuration; +using NHibernate.Envers.Tests.Entities; +using NUnit.Framework; + +namespace NHibernate.Envers.Tests.Integration.Basic +{ + public class TransactionScopeDistributed : TestBase + { + public TransactionScopeDistributed(AuditStrategyForTest strategyType) : base(strategyType) + { + } + +#if NETCOREAPP2_0 + [Ignore("Distributed transaction is not supported in Core.")] +#endif + [Test] + public void ShouldNotThrowWhenDistributedTransaction() + { + var entity = new StrTestEntity { Str = "data" }; + + Assert.DoesNotThrow(() => + { + using (var distrTx = new TransactionScope()) + { + // Simulation of durable resource enlistment + using (var newSession = Session.SessionFactory.OpenSession()) + { + newSession.FlushMode = FlushMode.Manual; + newSession.Query().FirstOrDefault(); + } + + using (var newSession2 = Session.SessionFactory.OpenSession()) + { + newSession2.FlushMode = FlushMode.Manual; + newSession2.Save(entity); + newSession2.Flush(); + } + + distrTx.Complete(); + } + }); + } + +#if NETCOREAPP2_0 + [Ignore("Distributed transaction is not supported in Core.")] +#endif + [Test] + public void ShouldNotThrowWhenDistributedTransactionWithAutoFlushMode() + { + var entity = new StrTestEntity { Str = "data" }; + + Assert.DoesNotThrow(() => + { + using (var distrTx = new TransactionScope()) + { + using (var newSession = Session.SessionFactory.OpenSession()) + { + newSession.FlushMode = FlushMode.Auto; + newSession.Query().FirstOrDefault(); + } + + using (var newSession2 = Session.SessionFactory.OpenSession()) + { + newSession2.FlushMode = FlushMode.Auto; + newSession2.Save(entity); + newSession2.Flush(); + } + + distrTx.Complete(); + } + }); + } + +#if NETCOREAPP2_0 + [Ignore("Distributed transaction is not supported in Core.")] +#endif + [Test] + public void ShouldNotThrowWhenDistributedTransactionWithNhTransaction() + { + var entity = new StrTestEntity { Str = "data" }; + + Assert.DoesNotThrow(() => + { + using (var distrTx = new TransactionScope()) + { + using (var newSession = Session.SessionFactory.OpenSession()) + { + newSession.Query().FirstOrDefault(); + } + + using (var newSession2 = Session.SessionFactory.OpenSession()) + { + using (var nhTx = newSession2.BeginTransaction()) + { + newSession2.Save(entity); + newSession2.Flush(); + nhTx.Commit(); + } + } + + distrTx.Complete(); + } + }); + } + + protected override void Initialize() + { + } + + protected override void AddToConfiguration(Cfg.Configuration configuration) + { + ConfigurationKey.StoreDataAtDelete.SetUserValue(configuration, true); + } + + protected override IEnumerable Mappings + { + get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; } + } + } +} \ No newline at end of file diff --git a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs new file mode 100644 index 00000000..a6a16074 --- /dev/null +++ b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Linq; +using System.Transactions; +using NHibernate.Envers.Configuration; +using NHibernate.Envers.Tests.Entities; +using NUnit.Framework; + +namespace NHibernate.Envers.Tests.Integration.Basic +{ + public class TransactionScopeTest : TestBase + { + public TransactionScopeTest(AuditStrategyForTest strategyType) : base(strategyType) + { + } + +#if NETCOREAPP2_0 + [Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")] +#endif + [Test] + public void ShouldNotThrowIfUseConnectionOnSystemTransactionPrepareIsFalse() + { + var entity = new StrTestEntity { Str = "data" }; + + Assert.DoesNotThrow(() => + { + using (var distrTx = new TransactionScope()) + { + using (var newSession = Session.SessionFactory.OpenSession()) + { + newSession.FlushMode = FlushMode.Manual; + newSession.Save(entity); + newSession.Flush(); + } + distrTx.Complete(); + } + }); + } + + protected override void Initialize() + { + } + + protected override void AddToConfiguration(Cfg.Configuration configuration) + { + ConfigurationKey.StoreDataAtDelete.SetUserValue(configuration, true); + + // It is recommended since NH 5 + configuration.SetProperty(NHibernate.Cfg.Environment.UseConnectionOnSystemTransactionPrepare, "false"); + } + + protected override IEnumerable Mappings + { + get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; } + } + } +} \ No newline at end of file From 1caccf94379d24b00a0b2fb8dcaf1e45106e000b Mon Sep 17 00:00:00 2001 From: "mihail.iwanow" Date: Thu, 7 Mar 2019 11:22:37 +0100 Subject: [PATCH 2/2] Some more transaction scope tests --- ...sactionScopeWithUseConnectionOnPrepare.cs} | 15 ++++++----- ...tionScopeWithoutUseConnectionOnPrepare.cs} | 27 ++++++------------- 2 files changed, 17 insertions(+), 25 deletions(-) rename Src/NHibernate.Envers.Tests/Integration/Basic/{TransactionScopeDistributed.cs => TransactionScopeWithUseConnectionOnPrepare.cs} (79%) rename Src/NHibernate.Envers.Tests/Integration/Basic/{TransactionScopeTest.cs => TransactionScopeWithoutUseConnectionOnPrepare.cs} (57%) diff --git a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithUseConnectionOnPrepare.cs similarity index 79% rename from Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs rename to Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithUseConnectionOnPrepare.cs index 88279683..d5addc86 100644 --- a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeDistributed.cs +++ b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithUseConnectionOnPrepare.cs @@ -7,16 +7,16 @@ namespace NHibernate.Envers.Tests.Integration.Basic { - public class TransactionScopeDistributed : TestBase + public class TransactionScopeWithUseConnectionOnPrepare : TestBase { - public TransactionScopeDistributed(AuditStrategyForTest strategyType) : base(strategyType) + public TransactionScopeWithUseConnectionOnPrepare(AuditStrategyForTest strategyType) : base(strategyType) { } #if NETCOREAPP2_0 - [Ignore("Distributed transaction is not supported in Core.")] + [Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")] #endif - [Test] + [Test] public void ShouldNotThrowWhenDistributedTransaction() { var entity = new StrTestEntity { Str = "data" }; @@ -45,7 +45,7 @@ public void ShouldNotThrowWhenDistributedTransaction() } #if NETCOREAPP2_0 - [Ignore("Distributed transaction is not supported in Core.")] + [Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")] #endif [Test] public void ShouldNotThrowWhenDistributedTransactionWithAutoFlushMode() @@ -75,7 +75,7 @@ public void ShouldNotThrowWhenDistributedTransactionWithAutoFlushMode() } #if NETCOREAPP2_0 - [Ignore("Distributed transaction is not supported in Core.")] + [Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")] #endif [Test] public void ShouldNotThrowWhenDistributedTransactionWithNhTransaction() @@ -97,6 +97,9 @@ public void ShouldNotThrowWhenDistributedTransactionWithNhTransaction() { newSession2.Save(entity); newSession2.Flush(); + + // There is no problem in this case, + // because BeforeTransactionCompletion is called right after this line (before 1st commit phase of distributed transaction) nhTx.Commit(); } } diff --git a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithoutUseConnectionOnPrepare.cs similarity index 57% rename from Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs rename to Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithoutUseConnectionOnPrepare.cs index a6a16074..9b96d182 100644 --- a/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeTest.cs +++ b/Src/NHibernate.Envers.Tests/Integration/Basic/TransactionScopeWithoutUseConnectionOnPrepare.cs @@ -1,15 +1,12 @@ -using System.Collections.Generic; -using System.Linq; -using System.Transactions; -using NHibernate.Envers.Configuration; -using NHibernate.Envers.Tests.Entities; +using NHibernate.Envers.Tests.Entities; using NUnit.Framework; +using System.Transactions; namespace NHibernate.Envers.Tests.Integration.Basic { - public class TransactionScopeTest : TestBase + public class TransactionScopeWithoutUseConnectionOnPrepare : TransactionScopeWithUseConnectionOnPrepare { - public TransactionScopeTest(AuditStrategyForTest strategyType) : base(strategyType) + public TransactionScopeWithoutUseConnectionOnPrepare(AuditStrategyForTest strategyType) : base(strategyType) { } @@ -17,7 +14,7 @@ public TransactionScopeTest(AuditStrategyForTest strategyType) : base(strategyTy [Ignore("System.Transactions is not supported in Core. See https://github.com/dotnet/corefx/issues/19894")] #endif [Test] - public void ShouldNotThrowIfUseConnectionOnSystemTransactionPrepareIsFalse() + public void ShouldNotThrow() { var entity = new StrTestEntity { Str = "data" }; @@ -31,26 +28,18 @@ public void ShouldNotThrowIfUseConnectionOnSystemTransactionPrepareIsFalse() newSession.Save(entity); newSession.Flush(); } + distrTx.Complete(); } }); } - protected override void Initialize() - { - } - protected override void AddToConfiguration(Cfg.Configuration configuration) - { - ConfigurationKey.StoreDataAtDelete.SetUserValue(configuration, true); - + { // It is recommended since NH 5 configuration.SetProperty(NHibernate.Cfg.Environment.UseConnectionOnSystemTransactionPrepare, "false"); - } - protected override IEnumerable Mappings - { - get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; } + base.AddToConfiguration(configuration); } } } \ No newline at end of file