|
11 | 11 | using System.Collections.Generic;
|
12 | 12 | using System.Linq;
|
13 | 13 | using System.Reflection;
|
| 14 | +using NHibernate.Criterion; |
14 | 15 | using NHibernate.DomainModel.Northwind.Entities;
|
15 | 16 | using NHibernate.Engine.Query;
|
16 | 17 | using NHibernate.Linq;
|
@@ -308,5 +309,62 @@ public async Task PlansWithNonParameterizedConstantsAreNotCachedAsync()
|
308 | 309 | Has.Count.EqualTo(0),
|
309 | 310 | "Query plan should not be cached.");
|
310 | 311 | }
|
| 312 | + |
| 313 | + [Test] |
| 314 | + public async Task PlansWithNonParameterizedConstantsAreNotCachedForExpandedQueryAsync() |
| 315 | + { |
| 316 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 317 | + |
| 318 | + var cache = (SoftLimitMRUCache) |
| 319 | + queryPlanCacheType |
| 320 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 321 | + .GetValue(Sfi.QueryPlanCache); |
| 322 | + cache.Clear(); |
| 323 | + |
| 324 | + var ids = new[] {"ANATR", "UNKNOWN"}.ToList(); |
| 325 | + await (db.Customers.Where(x => ids.Contains(x.CustomerId)).Select( |
| 326 | + c => new {c.CustomerId, c.ContactName, Constant = 1}).FirstAsync()); |
| 327 | + |
| 328 | + Assert.That( |
| 329 | + cache, |
| 330 | + Has.Count.EqualTo(0), |
| 331 | + "Query plan should not be cached."); |
| 332 | + } |
| 333 | + |
| 334 | + //GH-2298 - Different Update queries - same query cache plan |
| 335 | + [Test] |
| 336 | + public async Task DmlPlansForExpandedQueryAsync() |
| 337 | + { |
| 338 | + var queryPlanCacheType = typeof(QueryPlanCache); |
| 339 | + |
| 340 | + var cache = (SoftLimitMRUCache) |
| 341 | + queryPlanCacheType |
| 342 | + .GetField("planCache", BindingFlags.Instance | BindingFlags.NonPublic) |
| 343 | + .GetValue(Sfi.QueryPlanCache); |
| 344 | + cache.Clear(); |
| 345 | + |
| 346 | + using (session.BeginTransaction()) |
| 347 | + { |
| 348 | + var list = new[] {"UNKNOWN", "UNKNOWN2"}.ToList(); |
| 349 | + await (db.Customers.Where(x => list.Contains(x.CustomerId)).UpdateAsync( |
| 350 | + x => new Customer |
| 351 | + { |
| 352 | + CompanyName = "Constant1" |
| 353 | + })); |
| 354 | + |
| 355 | + await (db.Customers.Where(x => list.Contains(x.CustomerId)) |
| 356 | + .UpdateAsync( |
| 357 | + x => new Customer |
| 358 | + { |
| 359 | + ContactName = "Constant1" |
| 360 | + })); |
| 361 | + |
| 362 | + Assert.That( |
| 363 | + cache.Count, |
| 364 | + //2 original queries + 2 expanded queries are expected in cache |
| 365 | + Is.EqualTo(0).Or.EqualTo(4), |
| 366 | + "Query plans should either be cached separately or not cached at all."); |
| 367 | + } |
| 368 | + } |
311 | 369 | }
|
312 | 370 | }
|
0 commit comments