@@ -119,6 +119,68 @@ async Task NotificationAction(TestNotification _, CancellationToken cancellation
119119 Assert . Equal ( 1 , receivedMessageCount ) ;
120120 }
121121
122+ [ Fact ]
123+ public async Task Handlers_RunInParallel_WhenUseTaskWhenAllIsTrue ( )
124+ {
125+ var options = new CourierOptions { UseTaskWhenAll = true } ;
126+ var mediatRCourier = new MediatRCourier ( options ) ;
127+
128+ var delays = new [ ] { 100 , 200 , 300 } ;
129+ var started = new bool [ delays . Length ] ;
130+ var completed = new bool [ delays . Length ] ;
131+
132+ for ( int i = 0 ; i < delays . Length ; i ++ )
133+ {
134+ int idx = i ;
135+ mediatRCourier . Subscribe < TestNotification > ( async ( _ , ct ) =>
136+ {
137+ started [ idx ] = true ;
138+ await Task . Delay ( delays [ idx ] , ct ) ;
139+ completed [ idx ] = true ;
140+ } ) ;
141+ }
142+
143+ var sw = System . Diagnostics . Stopwatch . StartNew ( ) ;
144+ await mediatRCourier . Handle ( new TestNotification ( ) , CancellationToken . None ) ;
145+ sw . Stop ( ) ;
146+
147+ Assert . All ( started , Assert . True ) ;
148+ Assert . All ( completed , Assert . True ) ;
149+ // Should complete in just over the max delay (parallel)
150+ Assert . InRange ( sw . ElapsedMilliseconds , delays . Max ( ) , delays . Max ( ) + 150 ) ;
151+ }
152+
153+ [ Fact ]
154+ public async Task Handlers_RunSequentially_WhenUseTaskWhenAllIsFalse ( )
155+ {
156+ var options = new CourierOptions { CaptureThreadContext = false , UseTaskWhenAll = false } ;
157+ var mediatRCourier = new MediatRCourier ( options ) ;
158+
159+ var delays = new [ ] { 100 , 200 , 300 } ;
160+ var started = new bool [ delays . Length ] ;
161+ var completed = new bool [ delays . Length ] ;
162+
163+ for ( int i = 0 ; i < delays . Length ; i ++ )
164+ {
165+ int idx = i ;
166+ mediatRCourier . Subscribe < TestNotification > ( async ( _ , ct ) =>
167+ {
168+ started [ idx ] = true ;
169+ await Task . Delay ( delays [ idx ] , ct ) ;
170+ completed [ idx ] = true ;
171+ } ) ;
172+ }
173+
174+ var sw = System . Diagnostics . Stopwatch . StartNew ( ) ;
175+ await mediatRCourier . Handle ( new TestNotification ( ) , CancellationToken . None ) ;
176+ sw . Stop ( ) ;
177+
178+ Assert . All ( started , s => Assert . True ( s ) ) ;
179+ Assert . All ( completed , c => Assert . True ( c ) ) ;
180+ // Should complete in just over the sum of delays (sequential)
181+ Assert . InRange ( sw . ElapsedMilliseconds , delays . Sum ( ) , delays . Sum ( ) + 150 ) ;
182+ }
183+
122184 private sealed class AsyncTestData : IEnumerable < object [ ] >
123185 {
124186 public IEnumerator < object [ ] > GetEnumerator ( )
0 commit comments