@@ -219,21 +219,23 @@ public async Task MultipleGetReadOnlyTestAsync()
219
219
var persister = Sfi . GetEntityPersister ( typeof ( ReadOnly ) . FullName ) ;
220
220
Assert . That ( persister . Cache . Cache , Is . Not . Null ) ;
221
221
Assert . That ( persister . Cache . Cache , Is . TypeOf < BatchableCache > ( ) ) ;
222
- var ids = new List < int > ( ) ;
222
+ int [ ] getIds ;
223
+ int [ ] loadIds ;
223
224
224
225
using ( var s = Sfi . OpenSession ( ) )
225
226
using ( var tx = s . BeginTransaction ( ) )
226
227
{
227
228
var items = await ( s . Query < ReadOnly > ( ) . ToListAsync ( ) ) ;
228
- ids . AddRange ( items . OrderBy ( o => o . Id ) . Select ( o => o . Id ) ) ;
229
+ loadIds = getIds = items . OrderBy ( o => o . Id ) . Select ( o => o . Id ) . ToArray ( ) ;
229
230
await ( tx . CommitAsync ( ) ) ;
230
231
}
231
232
// Batch size 3
232
- var parentTestCases = new List < Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > >
233
+ var parentTestCases = new List < Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > >
233
234
{
234
235
// When the cache is empty, GetMultiple will be called two times. One time in type
235
236
// DefaultLoadEventListener and the other time in BatchingEntityLoader.
236
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
237
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
238
+ loadIds ,
237
239
0 ,
238
240
new [ ]
239
241
{
@@ -245,7 +247,8 @@ public async Task MultipleGetReadOnlyTestAsync()
245
247
) ,
246
248
// When there are not enough uninitialized entities after the demanded one to fill the batch,
247
249
// the nearest before the demanded entity are added.
248
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
250
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
251
+ loadIds ,
249
252
4 ,
250
253
new [ ]
251
254
{
@@ -255,7 +258,8 @@ public async Task MultipleGetReadOnlyTestAsync()
255
258
new [ ] { 3 , 4 , 5 } ,
256
259
null
257
260
) ,
258
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
261
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
262
+ loadIds ,
259
263
5 ,
260
264
new [ ]
261
265
{
@@ -265,7 +269,8 @@ public async Task MultipleGetReadOnlyTestAsync()
265
269
new [ ] { 3 , 4 , 5 } ,
266
270
null
267
271
) ,
268
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
272
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
273
+ loadIds ,
269
274
0 ,
270
275
new [ ]
271
276
{
@@ -274,7 +279,8 @@ public async Task MultipleGetReadOnlyTestAsync()
274
279
null ,
275
280
( i ) => i % 2 == 0 // Cache all even indexes before loading
276
281
) ,
277
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
282
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
283
+ loadIds ,
278
284
1 ,
279
285
new [ ]
280
286
{
@@ -284,7 +290,8 @@ public async Task MultipleGetReadOnlyTestAsync()
284
290
new [ ] { 1 , 3 , 5 } ,
285
291
( i ) => i % 2 == 0
286
292
) ,
287
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
293
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
294
+ loadIds ,
288
295
5 ,
289
296
new [ ]
290
297
{
@@ -294,7 +301,8 @@ public async Task MultipleGetReadOnlyTestAsync()
294
301
new [ ] { 1 , 3 , 5 } ,
295
302
( i ) => i % 2 == 0
296
303
) ,
297
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
304
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
305
+ loadIds ,
298
306
0 ,
299
307
new [ ]
300
308
{
@@ -304,7 +312,8 @@ public async Task MultipleGetReadOnlyTestAsync()
304
312
new [ ] { 0 , 2 , 4 } ,
305
313
( i ) => i % 2 != 0
306
314
) ,
307
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
315
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
316
+ loadIds ,
308
317
4 ,
309
318
new [ ]
310
319
{
@@ -313,12 +322,56 @@ public async Task MultipleGetReadOnlyTestAsync()
313
322
} ,
314
323
new [ ] { 0 , 2 , 4 } ,
315
324
( i ) => i % 2 != 0
325
+ ) ,
326
+ // Tests by loading different ids
327
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
328
+ loadIds . Where ( ( v , i ) => i != 0 ) . ToArray ( ) ,
329
+ 0 ,
330
+ new [ ]
331
+ {
332
+ new [ ] { 0 , 5 , 4 } , // triggered by LoadFromSecondLevelCache method of DefaultLoadEventListener type
333
+ new [ ] { 3 , 4 , 5 } , // triggered by Load method of BatchingEntityLoader type
334
+ } ,
335
+ new [ ] { 0 , 4 , 5 } ,
336
+ null
337
+ ) ,
338
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
339
+ loadIds . Where ( ( v , i ) => i != 4 ) . ToArray ( ) ,
340
+ 4 ,
341
+ new [ ]
342
+ {
343
+ new [ ] { 4 , 5 , 3 } ,
344
+ new [ ] { 5 , 3 , 2 } ,
345
+ } ,
346
+ new [ ] { 3 , 4 , 5 } ,
347
+ null
348
+ ) ,
349
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
350
+ loadIds . Where ( ( v , i ) => i != 0 ) . ToArray ( ) ,
351
+ 0 ,
352
+ new [ ]
353
+ {
354
+ new [ ] { 0 , 5 , 4 } // 0 get assembled and no further processing is done
355
+ } ,
356
+ null ,
357
+ ( i ) => i % 2 == 0 // Cache all even indexes before loading
358
+ ) ,
359
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
360
+ loadIds . Where ( ( v , i ) => i != 1 ) . ToArray ( ) ,
361
+ 1 ,
362
+ new [ ]
363
+ {
364
+ new [ ] { 1 , 5 , 4 } , // 4 gets assembled inside LoadFromSecondLevelCache
365
+ new [ ] { 5 , 3 , 2 }
366
+ } ,
367
+ new [ ] { 1 , 3 , 5 } ,
368
+ ( i ) => i % 2 == 0
316
369
)
317
370
} ;
318
371
319
372
foreach ( var tuple in parentTestCases )
320
373
{
321
- await ( AssertMultipleCacheCallsAsync < ReadOnly > ( ids , tuple . Item1 , tuple . Item2 , tuple . Item3 , tuple . Item4 ) ) ;
374
+ await ( AssertMultipleCacheCallsAsync < ReadOnly > ( tuple . Item1 , getIds , tuple . Item2 , tuple . Item3 , tuple . Item4 , tuple . Item5 ) ) ;
322
375
}
323
376
}
324
377
@@ -328,21 +381,23 @@ public async Task MultipleGetReadOnlyItemTestAsync()
328
381
var persister = Sfi . GetEntityPersister ( typeof ( ReadOnlyItem ) . FullName ) ;
329
382
Assert . That ( persister . Cache . Cache , Is . Not . Null ) ;
330
383
Assert . That ( persister . Cache . Cache , Is . TypeOf < BatchableCache > ( ) ) ;
331
- var ids = new List < int > ( ) ;
384
+ int [ ] getIds ;
385
+ int [ ] loadIds ;
332
386
333
387
using ( var s = Sfi . OpenSession ( ) )
334
388
using ( var tx = s . BeginTransaction ( ) )
335
389
{
336
390
var items = await ( s . Query < ReadOnlyItem > ( ) . Take ( 6 ) . ToListAsync ( ) ) ;
337
- ids . AddRange ( items . OrderBy ( o => o . Id ) . Select ( o => o . Id ) ) ;
391
+ loadIds = getIds = items . OrderBy ( o => o . Id ) . Select ( o => o . Id ) . ToArray ( ) ;
338
392
await ( tx . CommitAsync ( ) ) ;
339
393
}
340
394
// Batch size 4
341
- var parentTestCases = new List < Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > >
395
+ var parentTestCases = new List < Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > >
342
396
{
343
397
// When the cache is empty, GetMultiple will be called two times. One time in type
344
398
// DefaultLoadEventListener and the other time in BatchingEntityLoader.
345
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
399
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
400
+ loadIds ,
346
401
0 ,
347
402
new [ ]
348
403
{
@@ -354,7 +409,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
354
409
) ,
355
410
// When there are not enough uninitialized entities after the demanded one to fill the batch,
356
411
// the nearest before the demanded entity are added.
357
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
412
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
413
+ loadIds ,
358
414
4 ,
359
415
new [ ]
360
416
{
@@ -364,7 +420,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
364
420
new [ ] { 2 , 3 , 4 , 5 } ,
365
421
null
366
422
) ,
367
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
423
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
424
+ loadIds ,
368
425
5 ,
369
426
new [ ]
370
427
{
@@ -374,7 +431,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
374
431
new [ ] { 2 , 3 , 4 , 5 } ,
375
432
null
376
433
) ,
377
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
434
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
435
+ loadIds ,
378
436
0 ,
379
437
new [ ]
380
438
{
@@ -383,7 +441,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
383
441
null ,
384
442
( i ) => i % 2 == 0 // Cache all even indexes before loading
385
443
) ,
386
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
444
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
445
+ loadIds ,
387
446
1 ,
388
447
new [ ]
389
448
{
@@ -393,7 +452,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
393
452
new [ ] { 1 , 3 , 5 } ,
394
453
( i ) => i % 2 == 0
395
454
) ,
396
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
455
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
456
+ loadIds ,
397
457
5 ,
398
458
new [ ]
399
459
{
@@ -403,7 +463,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
403
463
new [ ] { 1 , 3 , 5 } ,
404
464
( i ) => i % 2 == 0
405
465
) ,
406
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
466
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
467
+ loadIds ,
407
468
0 ,
408
469
new [ ]
409
470
{
@@ -413,7 +474,8 @@ public async Task MultipleGetReadOnlyItemTestAsync()
413
474
new [ ] { 0 , 2 , 4 } ,
414
475
( i ) => i % 2 != 0
415
476
) ,
416
- new Tuple < int , int [ ] [ ] , int [ ] , Func < int , bool > > (
477
+ new Tuple < int [ ] , int , int [ ] [ ] , int [ ] , Func < int , bool > > (
478
+ loadIds ,
417
479
4 ,
418
480
new [ ]
419
481
{
@@ -427,7 +489,7 @@ public async Task MultipleGetReadOnlyItemTestAsync()
427
489
428
490
foreach ( var tuple in parentTestCases )
429
491
{
430
- await ( AssertMultipleCacheCallsAsync < ReadOnlyItem > ( ids , tuple . Item1 , tuple . Item2 , tuple . Item3 , tuple . Item4 ) ) ;
492
+ await ( AssertMultipleCacheCallsAsync < ReadOnlyItem > ( tuple . Item1 , getIds , tuple . Item2 , tuple . Item3 , tuple . Item4 , tuple . Item5 ) ) ;
431
493
}
432
494
}
433
495
@@ -764,7 +826,8 @@ public async Task QueryCacheTestAsync()
764
826
}
765
827
}
766
828
767
- private async Task AssertMultipleCacheCallsAsync < TEntity > ( List < int > ids , int idIndex , int [ ] [ ] fetchedIdIndexes , int [ ] putIdIndexes , Func < int , bool > cacheBeforeLoadFn = null , CancellationToken cancellationToken = default ( CancellationToken ) )
829
+ private async Task AssertMultipleCacheCallsAsync < TEntity > ( IEnumerable < int > loadIds , IReadOnlyList < int > getIds , int idIndex ,
830
+ int [ ] [ ] fetchedIdIndexes , int [ ] putIdIndexes , Func < int , bool > cacheBeforeLoadFn = null , CancellationToken cancellationToken = default ( CancellationToken ) )
768
831
where TEntity : CacheEntity
769
832
{
770
833
var persister = Sfi . GetEntityPersister ( typeof ( TEntity ) . FullName ) ;
@@ -776,7 +839,7 @@ public async Task QueryCacheTestAsync()
776
839
using ( var s = Sfi . OpenSession ( ) )
777
840
using ( var tx = s . BeginTransaction ( ) )
778
841
{
779
- foreach ( var id in ids . Where ( ( o , i ) => cacheBeforeLoadFn ( i ) ) )
842
+ foreach ( var id in getIds . Where ( ( o , i ) => cacheBeforeLoadFn ( i ) ) )
780
843
{
781
844
await ( s . GetAsync < TEntity > ( id , cancellationToken ) ) ;
782
845
}
@@ -788,12 +851,11 @@ public async Task QueryCacheTestAsync()
788
851
using ( var tx = s . BeginTransaction ( ) )
789
852
{
790
853
cache . ClearStatistics ( ) ;
791
-
792
- foreach ( var id in ids )
854
+ foreach ( var id in loadIds )
793
855
{
794
856
await ( s . LoadAsync < TEntity > ( id , cancellationToken ) ) ;
795
857
}
796
- var item = await ( s . GetAsync < TEntity > ( ids [ idIndex ] , cancellationToken ) ) ;
858
+ var item = await ( s . GetAsync < TEntity > ( getIds [ idIndex ] , cancellationToken ) ) ;
797
859
Assert . That ( item , Is . Not . Null ) ;
798
860
Assert . That ( cache . GetCalls , Has . Count . EqualTo ( 0 ) ) ;
799
861
Assert . That ( cache . PutCalls , Has . Count . EqualTo ( 0 ) ) ;
@@ -807,14 +869,14 @@ public async Task QueryCacheTestAsync()
807
869
Assert . That ( cache . PutMultipleCalls , Has . Count . EqualTo ( 1 ) ) ;
808
870
Assert . That (
809
871
cache . PutMultipleCalls [ 0 ] . OfType < CacheKey > ( ) . Select ( o => ( int ) o . Key ) ,
810
- Is . EquivalentTo ( putIdIndexes . Select ( o => ids [ o ] ) ) ) ;
872
+ Is . EquivalentTo ( putIdIndexes . Select ( o => getIds [ o ] ) ) ) ;
811
873
}
812
874
813
875
for ( int i = 0 ; i < fetchedIdIndexes . GetLength ( 0 ) ; i ++ )
814
876
{
815
877
Assert . That (
816
878
cache . GetMultipleCalls [ i ] . OfType < CacheKey > ( ) . Select ( o => ( int ) o . Key ) ,
817
- Is . EquivalentTo ( fetchedIdIndexes [ i ] . Select ( o => ids [ o ] ) ) ) ;
879
+ Is . EquivalentTo ( fetchedIdIndexes [ i ] . Select ( o => getIds [ o ] ) ) ) ;
818
880
}
819
881
820
882
await ( tx . CommitAsync ( cancellationToken ) ) ;
0 commit comments