Skip to content
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

Support Dynamic BatchFetchStyle #2959

Merged
merged 13 commits into from
Aug 16, 2022
12 changes: 11 additions & 1 deletion src/NHibernate.Test/Async/CacheTest/BatchableCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Loader;
using NHibernate.Multi;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
Expand All @@ -24,9 +25,17 @@ namespace NHibernate.Test.CacheTest
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchableCacheFixtureAsync : TestCase
{
private readonly BatchFetchStyle _fetchStyle;

public BatchableCacheFixtureAsync(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override string[] Mappings => new[]
{
"CacheTest.ReadOnly.hbm.xml",
Expand All @@ -43,6 +52,7 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Environment.UseQueryCache, "true");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.BatchFetchStyle, _fetchStyle.ToString());
}

protected override void OnSetUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NHibernate.Loader;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;

namespace NHibernate.Test.CacheTest
{
using System.Threading.Tasks;
[TestFixture]
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchableCacheSubclassFixtureAsync : TestCase
{
private readonly BatchFetchStyle _fetchStyle;

public BatchableCacheSubclassFixtureAsync(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override string[] Mappings
{
get
Expand Down Expand Up @@ -56,6 +65,7 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Cfg.Environment.UseSecondLevelCache, "true");
configuration.SetProperty(Cfg.Environment.UseQueryCache, "true");
configuration.SetProperty(Cfg.Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Cfg.Environment.BatchFetchStyle, _fetchStyle.ToString());
}

protected override void OnSetUp()
Expand Down
18 changes: 17 additions & 1 deletion src/NHibernate.Test/Async/NHSpecificTest/NH3142/ChildrenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Cfg;
using NHibernate.Driver;
using NHibernate.Loader;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3142
{
using System.Threading.Tasks;
[TestFixture]
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class ChildrenTestAsync : BugTestCase
{
private readonly BatchFetchStyle _fetchStyle;

public ChildrenTestAsync(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.SetProperty(Cfg.Environment.BatchFetchStyle, _fetchStyle.ToString());
}

protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
{
return !(factory.ConnectionProvider.Driver is OracleManagedDataClientDriver);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Loader;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.NHSpecificTest.NH3530
{
using System.Threading.Tasks;
using System.Threading;
//NH-3530 (GH-1316)
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchFetchStyleFixtureAsync : TestCaseMappingByCode
{
private readonly BatchFetchStyle _fetchStyle;
private readonly List<object> _ids = new List<object>();

public BatchFetchStyleFixtureAsync(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.SetProperty(Environment.BatchFetchStyle, _fetchStyle.ToString());
}

[Test]
public async Task CanLoadEntityAsync()
{
await (PrepareEntitiesAsync(2));

using (var session = OpenSession())
{
var proxy = await (session.LoadAsync<Entity>(_ids[0]));
var result = await (session.GetAsync<Entity>(_ids[1]));

Assert.That(result.Name, Is.Not.Null);

var childrenCount = result.Children.Count;
//Assert.That(NHibernateUtil.IsInitialized(proxy), Is.True);
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
Assert.That(childrenCount, Is.EqualTo(4));
}
}

[KnownBug("GH-2960")]
[Test]
public async Task CanLoadComponentEntityAsync()
{
await (PrepareComponentEntitiesAsync(2));

using (var session = OpenSession())
{
var proxy = await (session.LoadAsync<EntityComponent>(_ids[0]));
var result = await (session.GetAsync<EntityComponent>(_ids[1]));

Assert.That(result.Name, Is.Not.Null);

var childrenCount = result.Children.Count;
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
Assert.That(childrenCount, Is.EqualTo(4));
}
}

[Test]
public async Task CanLoadComponentIdEntityAsync()
{
await (PrepareComponentIdEntitiesAsync(2));

using (var session = OpenSession())
{
var proxy = await (session.LoadAsync<EntityComponentId>(_ids[0]));
var result = await (session.GetAsync<EntityComponentId>(_ids[1]));

Assert.That(result.Name, Is.Not.Null);

var childrenCount = result.Children.Count;
Assert.That(NHibernateUtil.IsInitialized(proxy.Children), Is.True);
Assert.That(childrenCount, Is.EqualTo(4));
}
}

[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(5)]
public async Task CanLoadBatchAsync(int loadCount)
{
await (PrepareEntitiesAsync(5));

using (var session = OpenSession())
{
foreach (var id in _ids.Take(loadCount))
{
await (session.LoadAsync<Entity>(id));
}

var result = await (session.GetAsync<Entity>(_ids[0]));
var result2 = await (session.GetAsync<Entity>(_ids[1]));
var last = await (session.GetAsync<Entity>(_ids.Last()));

var count = result.Children.Count;
Assert.That(result.Name, Is.Not.Null);
Assert.That(last.Name, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(result2.Children), Is.True);
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();
transaction.Commit();
}
}

protected override HbmMapping GetMappings()
{
return EntityMappings.CreateMapping();
}

private async Task PrepareEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
{
_ids.Clear();
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
for (int i = 0; i < count; i++)
{
var entity = new Entity { Name = "some name" + 1 };
AddChildren(entity.Children, 4);
_ids.Add((Guid) await (session.SaveAsync(entity, cancellationToken)));
}

await (transaction.CommitAsync(cancellationToken));
}
}

private async Task PrepareComponentEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
{
_ids.Clear();
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
for (int i = 0; i < count; i++)
{
var entity = new EntityComponent { Id1 = i, Id2 = i + 1, Name = "some name" + 1 };
AddChildren(entity.Children, 4);

_ids.Add(await (session.SaveAsync(entity, cancellationToken)));
}

await (transaction.CommitAsync(cancellationToken));
}
}

private async Task PrepareComponentIdEntitiesAsync(int count, CancellationToken cancellationToken = default(CancellationToken))
{
_ids.Clear();
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
for (int i = 0; i < count; i++)
{
var entity = new EntityComponentId { Id = new ComponentId { Id1 = i, Id2 = i + 1 }, Name = "some name" + 1 };
AddChildren(entity.Children, 4);
_ids.Add(await (session.SaveAsync(entity, cancellationToken)));
}

await (transaction.CommitAsync(cancellationToken));
}
}

private static void AddChildren<T>(IList<T> list, int count) where T : new()
{
for (int i = 0; i < count; i++)
{
list.Add(new T());
}
}
}
}
12 changes: 11 additions & 1 deletion src/NHibernate.Test/CacheTest/BatchableCacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Loader;
using NHibernate.Multi;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CacheTest
{
[TestFixture]
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchableCacheFixture : TestCase
{
private readonly BatchFetchStyle _fetchStyle;

public BatchableCacheFixture(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override string[] Mappings => new[]
{
"CacheTest.ReadOnly.hbm.xml",
Expand All @@ -31,6 +40,7 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Environment.UseQueryCache, "true");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Environment.BatchFetchStyle, _fetchStyle.ToString());
}

protected override void OnSetUp()
Expand Down
12 changes: 11 additions & 1 deletion src/NHibernate.Test/CacheTest/BatchableCacheSubclassFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.DomainModel;
using NHibernate.Loader;
using NHibernate.Test.CacheTest.Caches;
using NUnit.Framework;

namespace NHibernate.Test.CacheTest
{
[TestFixture]
[TestFixture(BatchFetchStyle.Dynamic)]
[TestFixture(BatchFetchStyle.Legacy)]
public class BatchableCacheSubclassFixture : TestCase
{
private readonly BatchFetchStyle _fetchStyle;

public BatchableCacheSubclassFixture(BatchFetchStyle fetchStyle)
{
_fetchStyle = fetchStyle;
}

protected override string[] Mappings
{
get
Expand Down Expand Up @@ -45,6 +54,7 @@ protected override void Configure(Configuration configuration)
configuration.SetProperty(Cfg.Environment.UseSecondLevelCache, "true");
configuration.SetProperty(Cfg.Environment.UseQueryCache, "true");
configuration.SetProperty(Cfg.Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
configuration.SetProperty(Cfg.Environment.BatchFetchStyle, _fetchStyle.ToString());
}

protected override void OnSetUp()
Expand Down
Loading