Skip to content

Add transaction scope and distributed transaction tests #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In NHibernate.Envers.Tests.Integration there is converted Hibernate Envers tests, so these tests suits better in NetSpecific.Integration instead.

{
public class TransactionScopeDistributed : TestBase
{
public TransactionScopeDistributed(AuditStrategyForTest strategyType) : base(strategyType)
{
}

#if NETCOREAPP2_0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the ignore on the complete class instead?

[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<StrTestEntity>().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<StrTestEntity>().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<StrTestEntity>().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<string> Mappings
{
get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; }
}
}
}
Original file line number Diff line number Diff line change
@@ -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<string> Mappings
{
get { return new[] {"Entities.Mapping.hbm.xml", "Integration.Collection.NoRevision.Mapping.hbm.xml"}; }
}
}
}