Skip to content

Commit d182220

Browse files
authored
Fix AutoFlushIfRequired result when flush was performed in Auto flush mode (nhibernate#3188)
1 parent ff6e513 commit d182220

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3583/AutoFlushFixture.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System.Collections.Generic;
12+
using System.Diagnostics;
1113
using System.Linq;
1214
using System.Transactions;
1315
using NHibernate.Cfg.MappingSchema;
14-
using NHibernate.Linq;
16+
using NHibernate.Impl;
1517
using NHibernate.Mapping.ByCode;
1618
using NUnit.Framework;
19+
using NHibernate.Linq;
1720

1821
namespace NHibernate.Test.NHSpecificTest.NH3583
1922
{
2023
using System.Threading.Tasks;
24+
using System.Threading;
2125
[TestFixture]
2226
public class AutoFlushFixtureAsync : TestCaseMappingByCode
2327
{
@@ -61,6 +65,24 @@ public async Task ShouldAutoFlushWhenInExplicitTransactionAsync()
6165
Assert.That(result.Count, Is.EqualTo(1));
6266
}
6367
}
68+
69+
[Test]
70+
public async Task AutoFlushIfRequiredShouldReturnTrueAsync()
71+
{
72+
using (var session = OpenSession())
73+
using (session.BeginTransaction())
74+
{
75+
var e1 = new Entity { Name = "Bob" };
76+
await (session.SaveAsync(e1));
77+
78+
var implementor = session as AbstractSessionImpl;
79+
var spaces = new HashSet<string> { "Entity" };
80+
Debug.Assert(implementor != null, nameof(implementor) + " != null");
81+
Assert.That(session.FlushMode, Is.EqualTo(FlushMode.Auto), "Issue is only presented in FlushMode.Auto");
82+
Assert.That(await (implementor.AutoFlushIfRequiredAsync(spaces, CancellationToken.None)), Is.True, "AutoFlushIfRequired should have reported, that the flush has been done.");
83+
}
84+
}
85+
6486

6587
[Test]
6688
public async Task ShouldAutoFlushWhenInDistributedTransactionAsync()

src/NHibernate.Test/NHSpecificTest/NH3583/AutoFlushFixture.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
3+
using System.Linq;
24
using System.Transactions;
35
using NHibernate.Cfg.MappingSchema;
4-
using NHibernate.Linq;
6+
using NHibernate.Impl;
57
using NHibernate.Mapping.ByCode;
68
using NUnit.Framework;
79

@@ -50,6 +52,24 @@ public void ShouldAutoFlushWhenInExplicitTransaction()
5052
Assert.That(result.Count, Is.EqualTo(1));
5153
}
5254
}
55+
56+
[Test]
57+
public void AutoFlushIfRequiredShouldReturnTrue()
58+
{
59+
using (var session = OpenSession())
60+
using (session.BeginTransaction())
61+
{
62+
var e1 = new Entity { Name = "Bob" };
63+
session.Save(e1);
64+
65+
var implementor = session as AbstractSessionImpl;
66+
var spaces = new HashSet<string> { "Entity" };
67+
Debug.Assert(implementor != null, nameof(implementor) + " != null");
68+
Assert.That(session.FlushMode, Is.EqualTo(FlushMode.Auto), "Issue is only presented in FlushMode.Auto");
69+
Assert.That(implementor.AutoFlushIfRequired(spaces), Is.True, "AutoFlushIfRequired should have reported, that the flush has been done.");
70+
}
71+
}
72+
5373

5474
[Test]
5575
public void ShouldAutoFlushWhenInDistributedTransaction()

src/NHibernate/Async/Event/Default/DefaultAutoFlushEventListener.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public virtual async Task OnAutoFlushAsync(AutoFlushEvent @event, CancellationTo
3939

4040
await (FlushEverythingToExecutionsAsync(@event, cancellationToken)).ConfigureAwait(false);
4141

42-
if (FlushIsReallyNeeded(@event, source))
42+
var flushIsReallyNeeded = FlushIsReallyNeeded(@event, source);
43+
if (flushIsReallyNeeded)
4344
{
4445
if (log.IsDebugEnabled())
4546
log.Debug("Need to execute flush");
@@ -61,7 +62,7 @@ public virtual async Task OnAutoFlushAsync(AutoFlushEvent @event, CancellationTo
6162
source.ActionQueue.ClearFromFlushNeededCheck(oldSize);
6263
}
6364

64-
@event.FlushRequired = FlushIsReallyNeeded(@event, source);
65+
@event.FlushRequired = flushIsReallyNeeded;
6566
}
6667
}
6768
}

src/NHibernate/Event/Default/DefaultAutoFlushEventListener.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public virtual void OnAutoFlush(AutoFlushEvent @event)
3131

3232
FlushEverythingToExecutions(@event);
3333

34-
if (FlushIsReallyNeeded(@event, source))
34+
var flushIsReallyNeeded = FlushIsReallyNeeded(@event, source);
35+
if (flushIsReallyNeeded)
3536
{
3637
if (log.IsDebugEnabled())
3738
log.Debug("Need to execute flush");
@@ -53,7 +54,7 @@ public virtual void OnAutoFlush(AutoFlushEvent @event)
5354
source.ActionQueue.ClearFromFlushNeededCheck(oldSize);
5455
}
5556

56-
@event.FlushRequired = FlushIsReallyNeeded(@event, source);
57+
@event.FlushRequired = flushIsReallyNeeded;
5758
}
5859
}
5960
}

0 commit comments

Comments
 (0)