Skip to content

Commit 70143fa

Browse files
David EllingsworthDavid Ellingsworth
authored andcommitted
Add Async tests for GitHub issue #2552
1 parent a107fee commit 70143fa

File tree

1 file changed

+194
-0
lines changed
  • src/NHibernate.Test/Async/NHSpecificTest/GH2552

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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.Reflection;
12+
using NHibernate.Cache;
13+
using NHibernate.Cfg;
14+
using NHibernate.Test.CacheTest.Caches;
15+
using NUnit.Framework;
16+
17+
namespace NHibernate.Test.NHSpecificTest.GH2552
18+
{
19+
using System.Threading.Tasks;
20+
using System.Threading;
21+
[TestFixture]
22+
public class FixtureAsync : BugTestCase
23+
{
24+
protected override string CacheConcurrencyStrategy => null;
25+
26+
protected override void Configure(Configuration configuration)
27+
{
28+
configuration.SetProperty(Environment.UseSecondLevelCache, "true");
29+
configuration.SetProperty(Environment.UseQueryCache, "true");
30+
configuration.SetProperty(Environment.GenerateStatistics, "true");
31+
configuration.SetProperty(Environment.CacheProvider, typeof(BatchableCacheProvider).AssemblyQualifiedName);
32+
}
33+
34+
protected override void OnSetUp()
35+
{
36+
// Initialize database with data
37+
using (var s = Sfi.OpenSession())
38+
using (var tx =s.BeginTransaction())
39+
{
40+
for (var i = 0; i < 6; i++)
41+
{
42+
var person = new PersonByFK()
43+
{
44+
Name = $"PersonByFK{i}",
45+
Details = (i % 2 == 0)
46+
? new DetailsByFK() { Data = $"DetailsByFK{i}" }
47+
: null,
48+
};
49+
50+
s.Save(person);
51+
}
52+
53+
for (var i = 0; i < 6; i++)
54+
{
55+
var person = new PersonByRef()
56+
{
57+
Name = $"PersonByRef{i}",
58+
Details = (i % 2 == 1)
59+
? new DetailsByRef() { Data = $"DetailsByRef{i}" }
60+
: null,
61+
};
62+
63+
s.Save(person);
64+
}
65+
66+
tx.Commit();
67+
}
68+
}
69+
70+
protected override void OnTearDown()
71+
{
72+
using (var s = OpenSession())
73+
using (var tx = s.BeginTransaction())
74+
{
75+
s.CreateQuery("delete from DetailsByFK").ExecuteUpdate();
76+
s.CreateQuery("delete from PersonByFK").ExecuteUpdate();
77+
s.CreateQuery("delete from DetailsByRef").ExecuteUpdate();
78+
s.CreateQuery("delete from PersonByRef").ExecuteUpdate();
79+
80+
tx.Commit();
81+
}
82+
83+
Sfi.Evict(typeof(PersonByFK));
84+
Sfi.Evict(typeof(DetailsByFK));
85+
Sfi.Evict(typeof(PersonByRef));
86+
Sfi.Evict(typeof(DetailsByRef));
87+
}
88+
89+
private BatchableCache GetDefaultQueryCache()
90+
{
91+
var queryCache = Sfi.GetQueryCache(null);
92+
var field = typeof(StandardQueryCache).GetField(
93+
"_cache",
94+
BindingFlags.NonPublic | BindingFlags.Instance);
95+
Assert.That(field, Is.Not.Null, "Unable to find _cache field");
96+
var cache = (BatchableCache) field.GetValue(queryCache);
97+
Assert.That(cache, Is.Not.Null, "_cache is null");
98+
99+
return cache;
100+
}
101+
102+
private async Task OneToOneTestAsync<TPerson, TDetails>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson:Person where TDetails : Details
103+
{
104+
var personPersister = Sfi.GetEntityPersister(typeof(TPerson).FullName);
105+
var detailsPersister = Sfi.GetEntityPersister(typeof(TDetails).FullName);
106+
107+
var personCache = personPersister.Cache.Cache as BatchableCache;
108+
var detailsCache = detailsPersister.Cache.Cache as BatchableCache;
109+
var queryCache = GetDefaultQueryCache();
110+
111+
Assert.That(personCache, Is.Not.Null);
112+
Assert.That(detailsCache, Is.Not.Null);
113+
114+
await (queryCache.ClearAsync(cancellationToken));
115+
await (personCache.ClearAsync(cancellationToken));
116+
await (detailsCache.ClearAsync(cancellationToken));
117+
118+
queryCache.ClearStatistics();
119+
personCache.ClearStatistics();
120+
detailsCache.ClearStatistics();
121+
122+
// Fill the empty caches with data.
123+
using (var s = OpenSession())
124+
using (var tx = s.BeginTransaction())
125+
{
126+
await (s.QueryOver<TPerson>()
127+
.Cacheable()
128+
.ListAsync(cancellationToken));
129+
130+
await (tx.CommitAsync(cancellationToken));
131+
}
132+
133+
// Verify query cache statistics
134+
Assert.That(queryCache.PutCalls, Has.Count.EqualTo(1));
135+
Assert.That(queryCache.GetCalls, Has.Count.EqualTo(1));
136+
137+
// Verify person cache statistics
138+
Assert.That(personCache.GetCalls, Has.Count.EqualTo(0));
139+
Assert.That(personCache.PutCalls, Has.Count.EqualTo(0));
140+
Assert.That(personCache.GetMultipleCalls, Has.Count.EqualTo(0));
141+
Assert.That(personCache.PutMultipleCalls, Has.Count.EqualTo(1));
142+
143+
// Verify details cache statistics
144+
Assert.That(detailsCache.GetCalls, Has.Count.EqualTo(0));
145+
Assert.That(detailsCache.PutCalls, Has.Count.EqualTo(3));
146+
Assert.That(detailsCache.GetMultipleCalls, Has.Count.EqualTo(0));
147+
Assert.That(detailsCache.PutMultipleCalls, Has.Count.EqualTo(0));
148+
149+
Sfi.Statistics.Clear();
150+
151+
using (var s = OpenSession())
152+
using (var tx = s.BeginTransaction())
153+
{
154+
await (s.QueryOver<TPerson>()
155+
.Cacheable()
156+
.ListAsync(cancellationToken));
157+
158+
await (tx.CommitAsync(cancellationToken));
159+
}
160+
161+
// Verify query cache statistics
162+
Assert.That(queryCache.GetCalls, Has.Count.EqualTo(2));
163+
Assert.That(queryCache.PutCalls, Has.Count.EqualTo(1));
164+
165+
// Verify person cache statistics
166+
Assert.That(personCache.GetCalls, Has.Count.EqualTo(0));
167+
Assert.That(personCache.PutCalls, Has.Count.EqualTo(0));
168+
Assert.That(personCache.GetMultipleCalls, Has.Count.EqualTo(1));
169+
Assert.That(personCache.PutMultipleCalls, Has.Count.EqualTo(1));
170+
171+
// Verify details cache statistics
172+
Assert.That(detailsCache.GetCalls, Has.Count.EqualTo(0));
173+
Assert.That(detailsCache.PutCalls, Has.Count.EqualTo(3));
174+
Assert.That(detailsCache.GetMultipleCalls, Has.Count.EqualTo(1));
175+
Assert.That(detailsCache.PutMultipleCalls, Has.Count.EqualTo(0));
176+
177+
// Verify that no second level cache misses occured.
178+
Assert.That(Sfi.Statistics.QueryCacheMissCount, Is.EqualTo(0));
179+
Assert.That(Sfi.Statistics.SecondLevelCacheMissCount, Is.EqualTo(0));
180+
}
181+
182+
[Test]
183+
public async Task OneToOneCacheByForeignKeyAsync()
184+
{
185+
await (OneToOneTestAsync<PersonByFK, DetailsByFK>());
186+
}
187+
188+
[Test]
189+
public async Task OneToOneCacheByRefAsync()
190+
{
191+
await (OneToOneTestAsync<PersonByRef, DetailsByRef>());
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)