3
3
using System . Linq ;
4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
+ using Microsoft . Extensions . DependencyInjection ;
6
7
using Snapshooter . Xunit ;
7
8
using Xunit ;
9
+ using Xunit . Abstractions ;
8
10
using static GreenDonut . TestHelpers ;
9
11
// ReSharper disable CollectionNeverUpdated.Local
10
12
// ReSharper disable InconsistentNaming
11
13
12
14
namespace GreenDonut ;
13
15
14
- public class DataLoaderTests
16
+ public class DataLoaderTests ( ITestOutputHelper output )
15
17
{
16
18
[ Fact ( DisplayName = "Clear: Should not throw any exception" ) ]
17
19
public void ClearNoException ( )
18
20
{
19
21
// arrange
20
22
var fetch = CreateFetch < string , string > ( ) ;
21
- var batchScheduler = new ManualBatchScheduler ( ) ;
22
- var loader = new DataLoader < string , string > ( fetch , batchScheduler ) ;
23
+ var services = new ServiceCollection ( )
24
+ . AddScoped < IBatchScheduler , ManualBatchScheduler > ( )
25
+ . AddDataLoader ( sp => new DataLoader < string , string > ( fetch , sp . GetRequiredService < IBatchScheduler > ( ) ) ) ;
26
+ var scope = services . BuildServiceProvider ( ) . CreateScope ( ) ;
27
+ var dataLoader = scope . ServiceProvider . GetRequiredService < DataLoader < string , string > > ( ) ;
23
28
24
29
// act
25
- void Verify ( ) => loader . Clear ( ) ;
30
+ void Verify ( ) => dataLoader . Clear ( ) ;
26
31
27
32
// assert
28
33
Assert . Null ( Record . Exception ( Verify ) ) ;
@@ -411,11 +416,11 @@ ValueTask Fetch(
411
416
[ InlineData ( 5 , 25 , 25 , 0 , true , false ) ]
412
417
[ InlineData ( 5 , 25 , 25 , 0 , false , true ) ]
413
418
[ InlineData ( 5 , 25 , 25 , 0 , false , false ) ]
414
- // [InlineData(100, 1000, 25, 25, true, true)]
415
- // [InlineData(100, 1000, 25, 0, true, true)]
416
- // [InlineData(100, 1000, 25, 0, true, false)]
417
- // [InlineData(100, 1000, 25, 25, false, true)]
418
- // [InlineData(100, 1000, 25, 0, false, false)]
419
+ [ InlineData ( 100 , 1000 , 25 , 25 , true , true ) ]
420
+ [ InlineData ( 100 , 1000 , 25 , 0 , true , true ) ]
421
+ [ InlineData ( 100 , 1000 , 25 , 0 , true , false ) ]
422
+ [ InlineData ( 100 , 1000 , 25 , 25 , false , true ) ]
423
+ [ InlineData ( 100 , 1000 , 25 , 0 , false , false ) ]
419
424
[ Theory ( DisplayName = "LoadAsync: Runs integration tests with different settings" ) ]
420
425
public async Task LoadTest (
421
426
int uniqueKeys ,
@@ -445,14 +450,16 @@ async ValueTask Wait()
445
450
=> await Task . Delay ( random . Next ( maxDelay ) , cancellationToken ) ;
446
451
}
447
452
453
+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 30 ) ) ;
454
+ var ct = cts . Token ;
448
455
using var cacheOwner = caching
449
456
? new TaskCacheOwner ( )
450
457
: null ;
451
-
458
+
452
459
var options = new DataLoaderOptions
453
460
{
454
461
Cache = cacheOwner ? . Cache ,
455
- CancellationToken = cacheOwner ? . CancellationToken ?? default ,
462
+ CancellationToken = ct ,
456
463
MaxBatchSize = batching ? 1 : maxBatchSize ,
457
464
} ;
458
465
@@ -468,26 +475,31 @@ async ValueTask Wait()
468
475
var requests = new Task < int > [ maxRequests ] ;
469
476
470
477
// act
478
+ output . WriteLine ( "LoadAsync" ) ;
471
479
for ( var i = 0 ; i < maxRequests ; i ++ )
472
480
{
473
481
requests [ i ] = Task . Factory . StartNew ( async ( ) =>
474
482
{
475
483
var index = random . Next ( uniqueKeys ) ;
476
484
var delay = random . Next ( maxDelay ) ;
477
485
478
- await Task . Delay ( delay ) ;
486
+ await Task . Delay ( delay , ct ) ;
479
487
480
- return await loader . LoadAsync ( keyArray [ index ] ) ;
488
+ return await loader . LoadAsync ( keyArray [ index ] , ct ) ;
481
489
} , TaskCreationOptions . RunContinuationsAsynchronously ) . Unwrap ( ) ;
482
490
}
483
491
492
+ output . WriteLine ( "Start Dispatch" ) ;
484
493
while ( requests . Any ( task => ! task . IsCompleted ) )
485
494
{
486
- await Task . Delay ( 25 ) ;
495
+ output . WriteLine ( "Wait" ) ;
496
+ await Task . Delay ( 25 , ct ) ;
497
+ output . WriteLine ( "Dispatch" ) ;
487
498
batchScheduler . Dispatch ( ) ;
488
499
}
489
500
490
501
// assert
502
+ output . WriteLine ( "Wait for results." ) ;
491
503
var responses = await Task . WhenAll ( requests ) ;
492
504
493
505
foreach ( var response in responses )
0 commit comments