Skip to content

Commit d31e655

Browse files
committed
Make sure constants are not cached
1 parent 7acee28 commit d31e655

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/NHibernate.Test/Async/Linq/ConstantTest.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,41 @@ public async Task DmlPlansAreCachedAsync()
250250

251251
using (session.BeginTransaction())
252252
{
253+
await (db.Customers.Where(c => c.CustomerId == "UNKNOWN").UpdateAsync(x => new Customer {CompanyName = "Constant1"}));
253254
await (db.Customers.Where(c => c.CustomerId == "ALFKI").UpdateAsync(x => new Customer {CompanyName = x.CompanyName}));
254255
Assert.That(
255256
cache,
256-
Has.Count.EqualTo(1),
257-
"First query plan should be cached.");
257+
Has.Count.EqualTo(2),
258+
"Query plans should be cached.");
258259

259260
using (var spy = new LogSpy(queryPlanCacheType))
260261
{
261-
// Should hit plan cache.
262-
await (db.Customers.Where(c => c.CustomerId == "ANATR").UpdateAsync(x => new Customer {CompanyName = x.CompanyName}));
263-
Assert.That(cache, Has.Count.EqualTo(1), "Second query should not cause a plan to be cached.");
262+
//Queries below should hit plan cache.
263+
using (var sqlSpy = new SqlLogSpy())
264+
{
265+
await (db.Customers.Where(c => c.CustomerId == "ANATR").UpdateAsync(x => new Customer {CompanyName = x.CompanyName}));
266+
await (db.Customers.Where(c => c.CustomerId == "UNKNOWN").UpdateAsync(x => new Customer {CompanyName = "Constant2"}));
267+
268+
var sqlEvents = sqlSpy.Appender.GetEvents();
269+
Assert.That(
270+
sqlEvents[0].RenderedMessage,
271+
Does.Contain("ANATR").And.Not.Contain("UNKNOWN").And.Not.Contain("Constant1"),
272+
"Unexpected constant parameter value");
273+
Assert.That(
274+
sqlEvents[1].RenderedMessage,
275+
Does.Contain("UNKNOWN").And.Contain("Constant2").And.Not.Contain("Constant1"),
276+
"Unexpected constant parameter value");
277+
}
278+
279+
Assert.That(cache, Has.Count.EqualTo(2), "Additional queries should not cause a plan to be cached.");
264280
Assert.That(
265281
spy.GetWholeLog(),
266282
Does
267283
.Contain("located HQL query plan in cache")
268284
.And.Not.Contain("unable to locate HQL query plan in cache"));
269285

270286
await (db.Customers.Where(c => c.CustomerId == "ANATR").UpdateAsync(x => new Customer {ContactName = x.ContactName}));
271-
Assert.That(cache, Has.Count.EqualTo(2), "Third query should be cached");
287+
Assert.That(cache, Has.Count.EqualTo(3), "Query should be cached");
272288
}
273289
}
274290
}

src/NHibernate.Test/Linq/ConstantTest.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,25 +271,41 @@ public void DmlPlansAreCached()
271271

272272
using (session.BeginTransaction())
273273
{
274+
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {CompanyName = "Constant1"});
274275
db.Customers.Where(c => c.CustomerId == "ALFKI").Update(x => new Customer {CompanyName = x.CompanyName});
275276
Assert.That(
276277
cache,
277-
Has.Count.EqualTo(1),
278-
"First query plan should be cached.");
278+
Has.Count.EqualTo(2),
279+
"Query plans should be cached.");
279280

280281
using (var spy = new LogSpy(queryPlanCacheType))
281282
{
282-
// Should hit plan cache.
283-
db.Customers.Where(c => c.CustomerId == "ANATR").Update(x => new Customer {CompanyName = x.CompanyName});
284-
Assert.That(cache, Has.Count.EqualTo(1), "Second query should not cause a plan to be cached.");
283+
//Queries below should hit plan cache.
284+
using (var sqlSpy = new SqlLogSpy())
285+
{
286+
db.Customers.Where(c => c.CustomerId == "ANATR").Update(x => new Customer {CompanyName = x.CompanyName});
287+
db.Customers.Where(c => c.CustomerId == "UNKNOWN").Update(x => new Customer {CompanyName = "Constant2"});
288+
289+
var sqlEvents = sqlSpy.Appender.GetEvents();
290+
Assert.That(
291+
sqlEvents[0].RenderedMessage,
292+
Does.Contain("ANATR").And.Not.Contain("UNKNOWN").And.Not.Contain("Constant1"),
293+
"Unexpected constant parameter value");
294+
Assert.That(
295+
sqlEvents[1].RenderedMessage,
296+
Does.Contain("UNKNOWN").And.Contain("Constant2").And.Not.Contain("Constant1"),
297+
"Unexpected constant parameter value");
298+
}
299+
300+
Assert.That(cache, Has.Count.EqualTo(2), "Additional queries should not cause a plan to be cached.");
285301
Assert.That(
286302
spy.GetWholeLog(),
287303
Does
288304
.Contain("located HQL query plan in cache")
289305
.And.Not.Contain("unable to locate HQL query plan in cache"));
290306

291307
db.Customers.Where(c => c.CustomerId == "ANATR").Update(x => new Customer {ContactName = x.ContactName});
292-
Assert.That(cache, Has.Count.EqualTo(2), "Third query should be cached");
308+
Assert.That(cache, Has.Count.EqualTo(3), "Query should be cached");
293309
}
294310
}
295311
}

0 commit comments

Comments
 (0)