diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1920/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1920/Fixture.cs new file mode 100644 index 00000000000..75693b26430 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1920/Fixture.cs @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// 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; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1920 +{ + using System.Threading.Tasks; + using System.Threading; + [TestFixture] + public class FixtureAsync : BugTestCase + { + private Guid entityId; + private Guid someOtherEntityId; + + protected override void OnSetUp() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" }); + someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize()); + + transaction.Commit(); + } + } + + [TestCase(true)] + [TestCase(false)] + public async Task CanLoadEntityAsync(bool loadProxyOfOtherEntity, CancellationToken cancellationToken = default(CancellationToken)) + { + using (var session = OpenSession()) + using (session.BeginTransaction()) + { + if (loadProxyOfOtherEntity) + await (session.LoadAsync(someOtherEntityId, cancellationToken)); + + var result = await (session.GetAsync(entityId, cancellationToken)); + + Assert.That(result.Name, Is.Not.Null); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from EntityWithBatchSize").ExecuteUpdate(); + transaction.Commit(); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1920/EntityWithBatchSize.cs b/src/NHibernate.Test/NHSpecificTest/GH1920/EntityWithBatchSize.cs new file mode 100644 index 00000000000..8cb470eaf51 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1920/EntityWithBatchSize.cs @@ -0,0 +1,10 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.GH1920 +{ + public class EntityWithBatchSize + { + public virtual Guid Id { get; set; } + public virtual string Name { get; set; } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1920/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH1920/Fixture.cs new file mode 100644 index 00000000000..e8f21e9e436 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1920/Fixture.cs @@ -0,0 +1,50 @@ +using System; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1920 +{ + [TestFixture] + public class Fixture : BugTestCase + { + private Guid entityId; + private Guid someOtherEntityId; + + protected override void OnSetUp() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + entityId = (Guid) session.Save(new EntityWithBatchSize { Name = "some name" }); + someOtherEntityId = (Guid) session.Save(new EntityWithBatchSize()); + + transaction.Commit(); + } + } + + [TestCase(true)] + [TestCase(false)] + public void CanLoadEntity(bool loadProxyOfOtherEntity) + { + using (var session = OpenSession()) + using (session.BeginTransaction()) + { + if (loadProxyOfOtherEntity) + session.Load(someOtherEntityId); + + var result = session.Get(entityId); + + Assert.That(result.Name, Is.Not.Null); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from EntityWithBatchSize").ExecuteUpdate(); + transaction.Commit(); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1920/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/GH1920/Mappings.hbm.xml new file mode 100644 index 00000000000..602b6c8cad5 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1920/Mappings.hbm.xml @@ -0,0 +1,9 @@ + + + + + + + + diff --git a/src/NHibernate/Async/Engine/BatchFetchQueue.cs b/src/NHibernate/Async/Engine/BatchFetchQueue.cs index d8a1807a043..074c823c9b0 100644 --- a/src/NHibernate/Async/Engine/BatchFetchQueue.cs +++ b/src/NHibernate/Async/Engine/BatchFetchQueue.cs @@ -102,7 +102,7 @@ async Task CheckCacheAndProcessResultAsync() ? collectionKeys.Count - Math.Min(batchSize, collectionKeys.Count) : 0; var toIndex = collectionKeys.Count - 1; - var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex.Value, fromIndex, toIndex); + var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex, fromIndex, toIndex); if (batchableCache == null) { for (var j = 0; j < collectionKeys.Count; j++) @@ -295,7 +295,7 @@ async Task CheckCacheAndProcessResultAsync() ? entityKeys.Count - Math.Min(batchSize, entityKeys.Count) : 0; var toIndex = entityKeys.Count - 1; - var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex); + var indexes = GetSortedKeyIndexes(entityKeys, idIndex, fromIndex, toIndex); if (batchableCache == null) { for (var j = 0; j < entityKeys.Count; j++) diff --git a/src/NHibernate/Engine/BatchFetchQueue.cs b/src/NHibernate/Engine/BatchFetchQueue.cs index 05426554fb5..0202507ad7c 100644 --- a/src/NHibernate/Engine/BatchFetchQueue.cs +++ b/src/NHibernate/Engine/BatchFetchQueue.cs @@ -271,7 +271,7 @@ bool CheckCacheAndProcessResult() ? collectionKeys.Count - Math.Min(batchSize, collectionKeys.Count) : 0; var toIndex = collectionKeys.Count - 1; - var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex.Value, fromIndex, toIndex); + var indexes = GetSortedKeyIndexes(collectionKeys, keyIndex, fromIndex, toIndex); if (batchableCache == null) { for (var j = 0; j < collectionKeys.Count; j++) @@ -455,7 +455,7 @@ bool CheckCacheAndProcessResult() ? entityKeys.Count - Math.Min(batchSize, entityKeys.Count) : 0; var toIndex = entityKeys.Count - 1; - var indexes = GetSortedKeyIndexes(entityKeys, idIndex.Value, fromIndex, toIndex); + var indexes = GetSortedKeyIndexes(entityKeys, idIndex, fromIndex, toIndex); if (batchableCache == null) { for (var j = 0; j < entityKeys.Count; j++) @@ -612,22 +612,24 @@ private bool[] AreCached(List - /// Sorts the given keys by thier indexes, where the keys that are after the demanded key will be located + /// Sorts the given keys by their indexes, where the keys that are after the demanded key will be located /// at the start and the remaining indexes at the end of the returned array. /// /// The type of the key - /// The list of pairs of keys and thier indexes. + /// The list of pairs of keys and their indexes. /// The index of the demanded key /// The index where the sorting will begin. /// The index where the sorting will end. /// An array of sorted key indexes. - private static int[] GetSortedKeyIndexes(List> keys, int keyIndex, int fromIndex, int toIndex) + private static int[] GetSortedKeyIndexes(List> keys, int? keyIndex, int fromIndex, int toIndex) { var result = new int[Math.Abs(toIndex - fromIndex) + 1]; var lowerIndexes = new List(); var i = 0; for (var j = fromIndex; j <= toIndex; j++) { + // If the index was not found (null), this test will be falsy and it will take the most recently + // registered entities or collections. if (keys[j].Value < keyIndex) { lowerIndexes.Add(j);