Skip to content

Don't ignore minimalPut parameter in ReadWriteCache #3178

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

Merged
merged 14 commits into from
Oct 17, 2022
Merged
5 changes: 3 additions & 2 deletions src/NHibernate.Test/Async/CacheTest/CacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace NHibernate.Test.CacheTest
public class CacheFixtureAsync: TestCase
{
[Test]
public async Task TestSimpleCacheAsync()
public async Task TestSimpleReadWriteCacheAsync()
{
await (DoTestCacheAsync(new HashtableCacheProvider()));
}
Expand Down Expand Up @@ -57,7 +57,8 @@ protected CacheKey CreateCacheKey(string text)

Assert.IsNull(await (ccs.GetAsync(fooKey, longBefore, cancellationToken)));
Assert.AreEqual("foo", await (ccs.GetAsync(fooKey, after, cancellationToken)));
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, true, cancellationToken)));
Assert.IsTrue(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));

// update it;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2552/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override void Configure(NHCfg.Configuration configuration)
{
configuration.SetProperty(NHCfg.Environment.UseSecondLevelCache, "true");
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
configuration.SetProperty(NHCfg.Environment.UseMinimalPuts, "true");
}

protected override void OnTearDown()
Expand Down
180 changes: 180 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3176/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//------------------------------------------------------------------------------
// <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.Linq;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Linq;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3176
{
using System.Threading.Tasks;
[TestFixture(CacheFactory.ReadOnly, false)]
[TestFixture(CacheFactory.NonstrictReadWrite, false)]
[TestFixture(CacheFactory.ReadWrite, false)]
[TestFixture(CacheFactory.ReadWrite, true)]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
private readonly bool _versioned;
private int _id;

protected override string CacheConcurrencyStrategy { get; }

public ByCodeFixtureAsync(string cacheStrategy, bool versioned)
{
_versioned = versioned;
CacheConcurrencyStrategy = cacheStrategy;
}

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(
rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
rc.Property(x => x.Name);
rc.Component(
x => x.Component,
m =>
{
m.Property(x => x.Field);
m.Lazy(true);
});
if (_versioned)
rc.Version(x => x.Version, m => { });
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
configuration.Properties[Environment.UseSecondLevelCache] = "true";
configuration.Properties[Environment.GenerateStatistics] = "true";
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } };
session.Save(e1);
_id = e1.Id;

var e2 = new Entity { Name = "Sally" };
session.Save(e2);

transaction.Commit();
}
}

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

[Test]
public async Task TestPreLoadedDataAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// Load the entities into the second-level cache.
await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).ToListAsync());

var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());

Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);

var field = result.Component?.Field;

Assert.That(field, Is.EqualTo("Jim"));
}

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());

Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);

var field = result.Component?.Field;

Assert.That(field, Is.EqualTo("Jim"));
}
}

[Test]
public async Task InitializedLazyPropertyShouldBeCachedAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e = await (session.GetAsync<Entity>(_id));
Assert.That(e.Component?.Field, Is.EqualTo("Jim"));

Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True);
await (transaction.CommitAsync());
}

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e = await (session.GetAsync<Entity>(_id));

Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True, "Lazy property is not cached");
var field = e.Component?.Field;

Assert.That(field, Is.EqualTo("Jim"));
await (transaction.CommitAsync());
}
}

[Test]
public async Task TestNonPreLoadedDataAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());

Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);

var field = result.Component?.Field;

Assert.That(field, Is.EqualTo("Jim"));
}

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());

Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);

var field = result.Component?.Field;

Assert.That(field, Is.EqualTo("Jim"));
}
}
}
}
5 changes: 3 additions & 2 deletions src/NHibernate.Test/CacheTest/CacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NHibernate.Test.CacheTest
public class CacheFixture: TestCase
{
[Test]
public void TestSimpleCache()
public void TestSimpleReadWriteCache()
{
DoTestCache(new HashtableCacheProvider());
}
Expand Down Expand Up @@ -46,7 +46,8 @@ public void DoTestCache(ICacheProvider cacheProvider)

Assert.IsNull(ccs.Get(fooKey, longBefore));
Assert.AreEqual("foo", ccs.Get(fooKey, after));
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false));
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, true));
Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false));

// update it;

Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2552/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected override void Configure(NHCfg.Configuration configuration)
{
configuration.SetProperty(NHCfg.Environment.UseSecondLevelCache, "true");
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
configuration.SetProperty(NHCfg.Environment.UseMinimalPuts, "true");
}

protected override void OnTearDown()
Expand Down
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3176/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH3176
{
public class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Component Component { get; set; }
public virtual int Version { get; set; }
}

public class Component
{
public virtual string Field { get; set; }
}
}
Loading