1
1
using FluentAssertions ;
2
+ using Microsoft . Extensions . Configuration ;
2
3
using Microsoft . Extensions . DependencyInjection ;
3
4
using Microsoft . Extensions . Hosting ;
4
5
@@ -59,4 +60,39 @@ public async Task SendMultipleCommandsFewTimes()
59
60
counter . Get ( CommandTwo . Handler . HandleCounterKey ) . Should ( ) . Be ( 3 ) ;
60
61
counter . Get ( CommandTwo . Handler . ConstructorCounterKey ) . Should ( ) . Be ( 3 ) ;
61
62
}
63
+
64
+ [ TestMethod ]
65
+ public async Task SendMultipleCommandsFromConfig ( )
66
+ {
67
+ var services = new ServiceCollection ( ) ;
68
+
69
+ services . AddMediatrTestingInfrastructure ( ) ;
70
+
71
+ var configuration = new ConfigurationManager ( ) ;
72
+ configuration . AddInMemoryCollection ( new [ ]
73
+ {
74
+ new KeyValuePair < string , string ? > ( "Timers:Backi.Tests.CommandOne" , "00:00:03" ) ,
75
+ new KeyValuePair < string , string ? > ( "Timers:Backi.Tests.CommandTwo" , "00:00:04" ) ,
76
+ } ) ;
77
+
78
+ services . AddTimediatr ( timediatr => timediatr . Configure ( o =>
79
+ {
80
+ o . AddAllFrom ( configuration . GetSection ( "Timers" ) , typeof ( CounterCollection ) . Assembly ) ;
81
+ } ) ) ;
82
+
83
+ var provider = services . BuildServiceProvider ( ) ;
84
+
85
+ var backgroundService = ( TimediatrBackgroundService ) provider . GetRequiredService < IHostedService > ( ) ;
86
+
87
+ await backgroundService . StartAsync ( CancellationToken . None ) ;
88
+ await Task . Delay ( TimeSpan . FromSeconds ( 10 ) ) ;
89
+ await backgroundService . StopAsync ( CancellationToken . None ) ;
90
+
91
+ var counter = provider . GetRequiredService < CounterCollection > ( ) ;
92
+
93
+ counter . Get ( CommandOne . Handler . HandleCounterKey ) . Should ( ) . Be ( 4 ) ;
94
+ counter . Get ( CommandOne . Handler . ConstructorCounterKey ) . Should ( ) . Be ( 4 ) ;
95
+ counter . Get ( CommandTwo . Handler . HandleCounterKey ) . Should ( ) . Be ( 3 ) ;
96
+ counter . Get ( CommandTwo . Handler . ConstructorCounterKey ) . Should ( ) . Be ( 3 ) ;
97
+ }
62
98
}
0 commit comments