Skip to content

Commit 86a4b92

Browse files
David EllingsworthDavid Ellingsworth
authored andcommitted
Add async tests for GitHub issue #2552
1 parent 6cf174d commit 86a4b92

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)