From cc4f78d151aa4dc4fcc2d83037e7e577cc0351c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Mon, 12 Mar 2018 02:10:28 +0100 Subject: [PATCH] Missing Async test for GH1594 --- .../Async/NHSpecificTest/GH1594/Fixture.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/NHibernate.Test/Async/NHSpecificTest/GH1594/Fixture.cs diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1594/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1594/Fixture.cs new file mode 100644 index 00000000000..ca584ea9c43 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1594/Fixture.cs @@ -0,0 +1,84 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System.Linq; +using System.Threading; +using System.Transactions; +using NHibernate.Engine; +using NUnit.Framework; +using NHibernate.Linq; + +namespace NHibernate.Test.NHSpecificTest.GH1594 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : BugTestCase + { + protected override bool AppliesTo(ISessionFactoryImplementor factory) => + factory.ConnectionProvider.Driver.SupportsSystemTransactions; + + protected override void OnSetUp() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + var e1 = new GH1594.Entity {Name = "Bob"}; + session.Save(e1); + + var e2 = new GH1594.Entity {Name = "Sally"}; + session.Save(e2); + + transaction.Commit(); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + // The HQL delete does all the job inside the database without loading the entities, but it does + // not handle delete order for avoiding violating constraints if any. Use + // session.Delete("from System.Object"); + // instead if in need of having NHbernate ordering the deletes, but this will cause + // loading the entities in the session. + session.CreateQuery("delete from System.Object").ExecuteUpdate(); + + transaction.Commit(); + } + } + + [Test] + public async Task ExecutionContextLocalValuesLeakAsync() + { + using (var session = OpenSession()) + { + await (RunInTransactionAsync(session)); + var localValuesCountAfterFirstCall = ExecutionContext.Capture().LocalValuesCount(); + await (RunInTransactionAsync(session)); + var localValuesCountAfterSecondCall = ExecutionContext.Capture().LocalValuesCount(); + Assert.AreEqual(localValuesCountAfterFirstCall, localValuesCountAfterSecondCall); + } + } + + private async Task RunInTransactionAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken)) + { + using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) + { + var result = from e in session.Query() + where e.Name == "Bob" + select e; + + await (result.ToListAsync(cancellationToken)); + ts.Complete(); + } + } + } +}