Skip to content

Commit 65a12ea

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH2552 Add async version of tests to verify cache updated on delete.
1 parent ab23e13 commit 65a12ea

File tree

1 file changed

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

1 file changed

+85
-0
lines changed

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,79 @@ protected override void OnTearDown()
7777
Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
7878
}
7979

80+
private async Task OneToOneUpdateTestAsync<TPerson, TDetails>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson : Person, new() where TDetails : Details, new()
81+
{
82+
List<object> ids = await (this.CreatePersonAndDetailsAsync<TPerson, TDetails>(cancellationToken));
83+
84+
IStatistics statistics = Sfi.Statistics;
85+
86+
// Clear the second level cache and the statistics
87+
await (Sfi.EvictEntityAsync(typeof(TPerson).FullName, cancellationToken));
88+
await (Sfi.EvictEntityAsync(typeof(TDetails).FullName, cancellationToken));
89+
await (Sfi.EvictQueriesAsync(cancellationToken));
90+
91+
statistics.Clear();
92+
93+
// Fill the empty caches with data.
94+
await (this.FetchPeopleByIdAsync<TPerson>(ids, cancellationToken));
95+
96+
// Verify that no data was retrieved from the cache.
97+
Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
98+
statistics.Clear();
99+
100+
int personId = await (DeleteDetailsFromFirstPersonAsync<TPerson>(cancellationToken));
101+
102+
// Verify that the cache was updated
103+
Assert.AreEqual(1, statistics.SecondLevelCachePutCount, "Second level cache put count");
104+
statistics.Clear();
105+
106+
// Verify that the Person was updated in the cache
107+
using (ISession s = Sfi.OpenSession())
108+
using (ITransaction tx = s.BeginTransaction())
109+
{
110+
TPerson person = await (s.GetAsync<TPerson>(personId, cancellationToken));
111+
112+
Assert.IsNull(person.Details);
113+
}
114+
115+
Assert.AreEqual(0, statistics.SecondLevelCacheMissCount, "Second level cache miss count");
116+
statistics.Clear();
117+
118+
// Verify that the Details was removed from the cache and deleted.
119+
using (ISession s = Sfi.OpenSession())
120+
using (ITransaction tx = s.BeginTransaction())
121+
{
122+
TDetails details = await (s.GetAsync<TDetails>(personId, cancellationToken));
123+
124+
Assert.Null(details);
125+
}
126+
127+
Assert.AreEqual(0, statistics.SecondLevelCacheHitCount, "Second level cache hit count");
128+
}
129+
130+
private async Task<int> DeleteDetailsFromFirstPersonAsync<TPerson>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson:Person
131+
{
132+
using (ISession s = Sfi.OpenSession())
133+
using (ITransaction tx = s.BeginTransaction())
134+
{
135+
// Get the first person with details.
136+
Person person = await (s.QueryOver<TPerson>()
137+
.Where(p => p.Details != null)
138+
.Take(1)
139+
.SingleOrDefaultAsync(cancellationToken));
140+
141+
Assert.NotNull(person);
142+
Assert.NotNull(person.Details);
143+
144+
await (s.SaveOrUpdateAsync(person, cancellationToken));
145+
person.Details = null;
146+
147+
await (tx.CommitAsync(cancellationToken));
148+
149+
return person.Id;
150+
}
151+
}
152+
80153
private async Task<List<object>> CreatePersonAndDetailsAsync<TPerson, TDetails>(CancellationToken cancellationToken = default(CancellationToken)) where TPerson : Person, new() where TDetails : Details, new()
81154
{
82155
List<object> ids = new List<object>();
@@ -137,5 +210,17 @@ public async Task OneToOneCacheFetchByRefAsync()
137210
{
138211
await (OneToOneFetchTestAsync<PersonByRef, DetailsByRef>());
139212
}
213+
214+
[Test]
215+
public async Task OneToOneCacheUpdateByForeignKeyAsync()
216+
{
217+
await (OneToOneUpdateTestAsync<PersonByFK, DetailsByFK>());
218+
}
219+
220+
[Test]
221+
public async Task OneToOneCacheUpdateByRefAsync()
222+
{
223+
await (OneToOneUpdateTestAsync<PersonByRef, DetailsByRef>());
224+
}
140225
}
141226
}

0 commit comments

Comments
 (0)