@@ -51,7 +51,8 @@ public async Task TestCommands(Type hostConfiguratorType)
51
51
52
52
// register all commands in one assembly
53
53
//commandOptions.AddCommandAssembly(typeof(SUB).GetTypeInfo().Assembly);
54
- } ) . BuildAsServer ( ) )
54
+ } )
55
+ . BuildAsServer ( ) )
55
56
{
56
57
57
58
Assert . Equal ( "TestServer" , server . Name ) ;
@@ -423,5 +424,110 @@ public async Task TestCommandFilter(Type hostConfiguratorType)
423
424
await server . StopAsync ( ) ;
424
425
}
425
426
}
427
+
428
+ [ Theory ]
429
+ [ InlineData ( typeof ( RegularHostConfigurator ) ) ]
430
+ [ InlineData ( typeof ( SecureHostConfigurator ) ) ]
431
+ public async Task TestCommandWithDependencyInjection ( Type hostConfiguratorType )
432
+ {
433
+ var hostConfigurator = CreateObject < IHostConfigurator > ( hostConfiguratorType ) ;
434
+ using ( var server = CreateSocketServerBuilder < StringPackageInfo , CommandLinePipelineFilter > ( hostConfigurator )
435
+ . UseCommand ( commandOptions =>
436
+ {
437
+ commandOptions . AddCommand < DOUBLE > ( ) ;
438
+ } )
439
+ . ConfigureServices ( ( context , services ) =>
440
+ {
441
+ services . AddSingleton < DataStore > ( ) ;
442
+ } )
443
+ . BuildAsServer ( ) )
444
+ {
445
+
446
+ Assert . Equal ( "TestServer" , server . Name ) ;
447
+
448
+ Assert . True ( await server . StartAsync ( ) ) ;
449
+ OutputHelper . WriteLine ( "Server started." ) ;
450
+
451
+ var dataStore = server . ServiceProvider . GetService < DataStore > ( ) ;
452
+
453
+ // Default value of data store
454
+ Assert . Equal ( 1 , dataStore . Value ) ;
455
+
456
+ var client = hostConfigurator . CreateClient ( ) ;
457
+
458
+ using ( var stream = await hostConfigurator . GetClientStream ( client ) )
459
+ using ( var streamWriter = new StreamWriter ( stream , Utf8Encoding , 1024 * 1024 * 4 ) )
460
+ {
461
+ dataStore . ResetTask ( ) ;
462
+ await streamWriter . WriteAsync ( "DOUBLE\r \n " ) ;
463
+ await streamWriter . FlushAsync ( ) ;
464
+
465
+ var newValue = await dataStore . WaitNewValue ( ) ;
466
+
467
+ Assert . Equal ( 2 , newValue ) ;
468
+
469
+ dataStore . ResetTask ( ) ;
470
+ await streamWriter . WriteAsync ( "DOUBLE\r \n " ) ;
471
+ await streamWriter . FlushAsync ( ) ;
472
+
473
+ newValue = await dataStore . WaitNewValue ( ) ;
474
+
475
+ Assert . Equal ( 4 , newValue ) ;
476
+
477
+ dataStore . ResetTask ( ) ;
478
+ await streamWriter . WriteAsync ( "DOUBLE\r \n " ) ;
479
+ await streamWriter . FlushAsync ( ) ;
480
+
481
+ newValue = await dataStore . WaitNewValue ( ) ;
482
+
483
+ Assert . Equal ( 8 , newValue ) ;
484
+ }
485
+
486
+ await server . StopAsync ( ) ;
487
+ }
488
+ }
489
+
490
+ public class DataStore
491
+ {
492
+ private int _value = 1 ;
493
+
494
+ public int Value
495
+ {
496
+ get { return _value ; }
497
+ set
498
+ {
499
+ _value = value ;
500
+ _newValueTaskSource ? . SetResult ( value ) ;
501
+ }
502
+ }
503
+
504
+ private TaskCompletionSource < int > _newValueTaskSource ;
505
+
506
+ public Task < int > WaitNewValue ( )
507
+ {
508
+ return _newValueTaskSource . Task ;
509
+ }
510
+
511
+ public void ResetTask ( )
512
+ {
513
+ _newValueTaskSource = new TaskCompletionSource < int > ( ) ;
514
+ }
515
+ }
516
+
517
+ public class DOUBLE : IAsyncCommand < StringPackageInfo >
518
+ {
519
+ private readonly DataStore _dataStore ;
520
+
521
+ public DOUBLE ( DataStore dataStore )
522
+ {
523
+ _dataStore = dataStore ;
524
+ }
525
+
526
+ public ValueTask ExecuteAsync ( IAppSession session , StringPackageInfo package , CancellationToken cancellationToken )
527
+ {
528
+ _dataStore . Value = _dataStore . Value * 2 ;
529
+ return ValueTask . CompletedTask ;
530
+ }
531
+ }
426
532
}
427
533
}
0 commit comments