Skip to content

Commit d33bce9

Browse files
authored
Don't ignore minimalPut parameter in ReadWriteCache (nhibernate#3178)
Fixes nhibernate#3176
1 parent d8a061b commit d33bce9

File tree

11 files changed

+434
-27
lines changed

11 files changed

+434
-27
lines changed

src/NHibernate.Test/Async/CacheTest/CacheFixture.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace NHibernate.Test.CacheTest
2222
public class CacheFixtureAsync: TestCase
2323
{
2424
[Test]
25-
public async Task TestSimpleCacheAsync()
25+
public async Task TestSimpleReadWriteCacheAsync()
2626
{
2727
await (DoTestCacheAsync(new HashtableCacheProvider()));
2828
}
@@ -57,7 +57,8 @@ protected CacheKey CreateCacheKey(string text)
5757

5858
Assert.IsNull(await (ccs.GetAsync(fooKey, longBefore, cancellationToken)));
5959
Assert.AreEqual("foo", await (ccs.GetAsync(fooKey, after, cancellationToken)));
60-
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));
60+
Assert.IsFalse(await (ccs.PutAsync(fooKey, "foo", before, null, null, true, cancellationToken)));
61+
Assert.IsTrue(await (ccs.PutAsync(fooKey, "foo", before, null, null, false, cancellationToken)));
6162

6263
// update it;
6364

src/NHibernate.Test/Async/NHSpecificTest/GH2552/Fixture.cs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected override void Configure(NHCfg.Configuration configuration)
2727
{
2828
configuration.SetProperty(NHCfg.Environment.UseSecondLevelCache, "true");
2929
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
30+
configuration.SetProperty(NHCfg.Environment.UseMinimalPuts, "true");
3031
}
3132

3233
protected override void OnTearDown()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cache;
13+
using NHibernate.Cfg;
14+
using NHibernate.Cfg.MappingSchema;
15+
using NHibernate.Linq;
16+
using NHibernate.Mapping.ByCode;
17+
using NUnit.Framework;
18+
19+
namespace NHibernate.Test.NHSpecificTest.GH3176
20+
{
21+
using System.Threading.Tasks;
22+
[TestFixture(CacheFactory.ReadOnly, false)]
23+
[TestFixture(CacheFactory.NonstrictReadWrite, false)]
24+
[TestFixture(CacheFactory.ReadWrite, false)]
25+
[TestFixture(CacheFactory.ReadWrite, true)]
26+
public class ByCodeFixtureAsync : TestCaseMappingByCode
27+
{
28+
private readonly bool _versioned;
29+
private int _id;
30+
31+
protected override string CacheConcurrencyStrategy { get; }
32+
33+
public ByCodeFixtureAsync(string cacheStrategy, bool versioned)
34+
{
35+
_versioned = versioned;
36+
CacheConcurrencyStrategy = cacheStrategy;
37+
}
38+
39+
protected override HbmMapping GetMappings()
40+
{
41+
var mapper = new ModelMapper();
42+
mapper.Class<Entity>(
43+
rc =>
44+
{
45+
rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
46+
rc.Property(x => x.Name);
47+
rc.Component(
48+
x => x.Component,
49+
m =>
50+
{
51+
m.Property(x => x.Field);
52+
m.Lazy(true);
53+
});
54+
if (_versioned)
55+
rc.Version(x => x.Version, m => { });
56+
});
57+
58+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
59+
}
60+
61+
protected override void Configure(Configuration configuration)
62+
{
63+
base.Configure(configuration);
64+
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
65+
configuration.Properties[Environment.UseSecondLevelCache] = "true";
66+
configuration.Properties[Environment.GenerateStatistics] = "true";
67+
}
68+
69+
protected override void OnSetUp()
70+
{
71+
using (var session = OpenSession())
72+
using (var transaction = session.BeginTransaction())
73+
{
74+
var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } };
75+
session.Save(e1);
76+
_id = e1.Id;
77+
78+
var e2 = new Entity { Name = "Sally" };
79+
session.Save(e2);
80+
81+
transaction.Commit();
82+
}
83+
}
84+
85+
protected override void OnTearDown()
86+
{
87+
using (var session = OpenSession())
88+
using (var transaction = session.BeginTransaction())
89+
{
90+
session.CreateQuery("delete from Entity").ExecuteUpdate();
91+
transaction.Commit();
92+
}
93+
}
94+
95+
[Test]
96+
public async Task TestPreLoadedDataAsync()
97+
{
98+
using (var session = OpenSession())
99+
using (var transaction = session.BeginTransaction())
100+
{
101+
// Load the entities into the second-level cache.
102+
await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).ToListAsync());
103+
104+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
105+
106+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
107+
108+
var field = result.Component?.Field;
109+
110+
Assert.That(field, Is.EqualTo("Jim"));
111+
}
112+
113+
using (var session = OpenSession())
114+
using (var transaction = session.BeginTransaction())
115+
{
116+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
117+
118+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
119+
120+
var field = result.Component?.Field;
121+
122+
Assert.That(field, Is.EqualTo("Jim"));
123+
}
124+
}
125+
126+
[Test]
127+
public async Task InitializedLazyPropertyShouldBeCachedAsync()
128+
{
129+
using (var session = OpenSession())
130+
using (var transaction = session.BeginTransaction())
131+
{
132+
var e = await (session.GetAsync<Entity>(_id));
133+
Assert.That(e.Component?.Field, Is.EqualTo("Jim"));
134+
135+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True);
136+
await (transaction.CommitAsync());
137+
}
138+
139+
using (var session = OpenSession())
140+
using (var transaction = session.BeginTransaction())
141+
{
142+
var e = await (session.GetAsync<Entity>(_id));
143+
144+
Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True, "Lazy property is not cached");
145+
var field = e.Component?.Field;
146+
147+
Assert.That(field, Is.EqualTo("Jim"));
148+
await (transaction.CommitAsync());
149+
}
150+
}
151+
152+
[Test]
153+
public async Task TestNonPreLoadedDataAsync()
154+
{
155+
using (var session = OpenSession())
156+
using (var transaction = session.BeginTransaction())
157+
{
158+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
159+
160+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
161+
162+
var field = result.Component?.Field;
163+
164+
Assert.That(field, Is.EqualTo("Jim"));
165+
}
166+
167+
using (var session = OpenSession())
168+
using (var transaction = session.BeginTransaction())
169+
{
170+
var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync());
171+
172+
Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True);
173+
174+
var field = result.Component?.Field;
175+
176+
Assert.That(field, Is.EqualTo("Jim"));
177+
}
178+
}
179+
}
180+
}

src/NHibernate.Test/CacheTest/CacheFixture.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace NHibernate.Test.CacheTest
1111
public class CacheFixture: TestCase
1212
{
1313
[Test]
14-
public void TestSimpleCache()
14+
public void TestSimpleReadWriteCache()
1515
{
1616
DoTestCache(new HashtableCacheProvider());
1717
}
@@ -46,7 +46,8 @@ public void DoTestCache(ICacheProvider cacheProvider)
4646

4747
Assert.IsNull(ccs.Get(fooKey, longBefore));
4848
Assert.AreEqual("foo", ccs.Get(fooKey, after));
49-
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, false));
49+
Assert.IsFalse(ccs.Put(fooKey, "foo", before, null, null, true));
50+
Assert.IsTrue(ccs.Put(fooKey, "foo", before, null, null, false));
5051

5152
// update it;
5253

src/NHibernate.Test/NHSpecificTest/GH2552/Fixture.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected override void Configure(NHCfg.Configuration configuration)
1414
{
1515
configuration.SetProperty(NHCfg.Environment.UseSecondLevelCache, "true");
1616
configuration.SetProperty(NHCfg.Environment.GenerateStatistics, "true");
17+
configuration.SetProperty(NHCfg.Environment.UseMinimalPuts, "true");
1718
}
1819

1920
protected override void OnTearDown()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3176
2+
{
3+
public class Entity
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual Component Component { get; set; }
8+
public virtual int Version { get; set; }
9+
}
10+
11+
public class Component
12+
{
13+
public virtual string Field { get; set; }
14+
}
15+
}

0 commit comments

Comments
 (0)