13
13
using NHibernate . Cfg ;
14
14
using NHibernate . Cfg . MappingSchema ;
15
15
using NHibernate . Collection ;
16
+ using NHibernate . Engine . Query ;
16
17
using NHibernate . Mapping . ByCode ;
17
18
using NUnit . Framework ;
18
19
using NHibernate . Linq ;
19
20
20
21
namespace NHibernate . Test . NHSpecificTest . NH2319
21
22
{
22
23
using System . Threading . Tasks ;
24
+ using System . Threading ;
23
25
[ TestFixture ]
24
26
public abstract class FixtureBaseAsync : TestCaseMappingByCode
25
27
{
26
- private Guid _parentId ;
28
+ private Guid _parent1Id ;
27
29
private Guid _child1Id ;
30
+ private Guid _parent2Id ;
31
+ private Guid _child3Id ;
28
32
29
33
[ Test ]
30
- public async Task ShouldBeAbleToFindChildrenByNameAsync ( )
34
+ public Task ShouldBeAbleToFindChildrenByNameAsync ( )
35
+ {
36
+ return FindChildrenByNameAsync ( _parent1Id , _child1Id ) ;
37
+ }
38
+
39
+ private async Task FindChildrenByNameAsync ( Guid parentId , Guid childId , CancellationToken cancellationToken = default ( CancellationToken ) )
31
40
{
32
41
using ( var session = OpenSession ( ) )
33
42
using ( session . BeginTransaction ( ) )
34
43
{
35
- var parent = await ( session . GetAsync < Parent > ( _parentId ) ) ;
44
+ var parent = await ( session . GetAsync < Parent > ( parentId , cancellationToken ) ) ;
36
45
37
46
Assert . That ( parent , Is . Not . Null ) ;
38
47
39
48
var filtered = await ( parent . Children
40
49
. AsQueryable ( )
41
50
. Where ( x => x . Name == "Jack" )
42
- . ToListAsync ( ) ) ;
51
+ . ToListAsync ( cancellationToken ) ) ;
43
52
44
53
Assert . That ( filtered , Has . Count . EqualTo ( 1 ) ) ;
45
- Assert . That ( filtered [ 0 ] . Id , Is . EqualTo ( _child1Id ) ) ;
54
+ Assert . That ( filtered [ 0 ] . Id , Is . EqualTo ( childId ) ) ;
46
55
}
47
56
}
48
57
@@ -52,7 +61,7 @@ public async Task ShouldBeAbleToPerformComplexFilteringAsync()
52
61
using ( var session = OpenSession ( ) )
53
62
using ( session . BeginTransaction ( ) )
54
63
{
55
- var parent = await ( session . GetAsync < Parent > ( _parentId ) ) ;
64
+ var parent = await ( session . GetAsync < Parent > ( _parent1Id ) ) ;
56
65
57
66
Assert . NotNull ( parent ) ;
58
67
@@ -67,13 +76,42 @@ public async Task ShouldBeAbleToPerformComplexFilteringAsync()
67
76
}
68
77
}
69
78
79
+ [ Test ]
80
+ public async Task ShouldBeAbleToReuseQueryPlanAsync ( )
81
+ {
82
+ await ( ShouldBeAbleToFindChildrenByNameAsync ( ) ) ;
83
+ using ( var spy = new LogSpy ( typeof ( QueryPlanCache ) ) )
84
+ {
85
+ Assert . That ( ShouldBeAbleToFindChildrenByNameAsync , Throws . Nothing ) ;
86
+ AssertPlanCacheHit ( spy ) ;
87
+ }
88
+ }
89
+
90
+ [ Test ]
91
+ public async Task ShouldNotMixResultsAsync ( )
92
+ {
93
+ await ( FindChildrenByNameAsync ( _parent1Id , _child1Id ) ) ;
94
+ using ( var spy = new LogSpy ( typeof ( QueryPlanCache ) ) )
95
+ {
96
+ await ( FindChildrenByNameAsync ( _parent2Id , _child3Id ) ) ;
97
+ AssertPlanCacheHit ( spy ) ;
98
+ }
99
+ }
100
+
101
+ private static void AssertPlanCacheHit ( LogSpy spy ) =>
102
+ // Each query currently ask the cache two times, so asserting reuse requires to check cache has not been missed
103
+ // rather than only asserting it has been hit.
104
+ Assert . That ( spy . GetWholeLog ( ) ,
105
+ Contains . Substring ( "located collection-filter query plan in cache (" )
106
+ . And . Not . Contains ( "unable to locate collection-filter query plan in cache" ) ) ;
107
+
70
108
[ Test ]
71
109
public async Task ShouldNotInitializeCollectionWhenPerformingQueryAsync ( )
72
110
{
73
111
using ( var session = OpenSession ( ) )
74
112
using ( session . BeginTransaction ( ) )
75
113
{
76
- var parent = await ( session . GetAsync < Parent > ( _parentId ) ) ;
114
+ var parent = await ( session . GetAsync < Parent > ( _parent1Id ) ) ;
77
115
Assert . That ( parent , Is . Not . Null ) ;
78
116
79
117
var persistentCollection = ( IPersistentCollection ) parent . Children ;
@@ -94,7 +132,7 @@ public async Task ShouldPerformSqlQueryEvenIfCollectionAlreadyInitializedAsync()
94
132
using ( var session = OpenSession ( ) )
95
133
using ( session . BeginTransaction ( ) )
96
134
{
97
- var parent = await ( session . GetAsync < Parent > ( _parentId ) ) ;
135
+ var parent = await ( session . GetAsync < Parent > ( _parent1Id ) ) ;
98
136
Assert . That ( parent , Is . Not . Null ) ;
99
137
100
138
var loaded = parent . Children . ToList ( ) ;
@@ -120,7 +158,7 @@ public async Task TestFilterAsync()
120
158
using ( var session = OpenSession ( ) )
121
159
using ( session . BeginTransaction ( ) )
122
160
{
123
- var parent = await ( session . GetAsync < Parent > ( _parentId ) ) ;
161
+ var parent = await ( session . GetAsync < Parent > ( _parent1Id ) ) ;
124
162
Assert . That ( parent , Is . Not . Null ) ;
125
163
126
164
var children = await ( ( await ( session . CreateFilterAsync ( parent . Children , "where this.Name = 'Jack'" ) ) )
@@ -141,11 +179,11 @@ protected override void OnSetUp()
141
179
using ( var session = OpenSession ( ) )
142
180
using ( var transaction = session . BeginTransaction ( ) )
143
181
{
144
- var parent1 = new Parent { Name = "Bob" } ;
145
- _parentId = ( Guid ) session . Save ( parent1 ) ;
182
+ var parent1 = new Parent { Name = "Bob" } ;
183
+ _parent1Id = ( Guid ) session . Save ( parent1 ) ;
146
184
147
- var parent2 = new Parent { Name = "Martin" } ;
148
- session . Save ( parent2 ) ;
185
+ var parent2 = new Parent { Name = "Martin" } ;
186
+ _parent2Id = ( Guid ) session . Save ( parent2 ) ;
149
187
150
188
var child1 = new Child
151
189
{
@@ -185,7 +223,7 @@ protected override void OnSetUp()
185
223
Parent = parent2
186
224
} ;
187
225
parent2 . Children . Add ( child3 ) ;
188
- session . Save ( child3 ) ;
226
+ _child3Id = ( Guid ) session . Save ( child3 ) ;
189
227
190
228
session . Flush ( ) ;
191
229
transaction . Commit ( ) ;
0 commit comments