@@ -157,6 +157,8 @@ public async Task InitializeAsync_InvokesHandlers()
157
157
var handler2 = new Mock < CircuitHandler > ( MockBehavior . Strict ) ;
158
158
var sequence = new MockSequence ( ) ;
159
159
160
+ SetupMockInboundActivityHandlers ( sequence , handler1 , handler2 ) ;
161
+
160
162
handler1
161
163
. InSequence ( sequence )
162
164
. Setup ( h => h . OnCircuitOpenedAsync ( It . IsAny < Circuit > ( ) , cancellationToken ) )
@@ -242,6 +244,8 @@ public async Task InitializeAsync_ReportsOwnAsyncExceptions()
242
244
var tcs = new TaskCompletionSource ( ) ;
243
245
var reportedErrors = new List < UnhandledExceptionEventArgs > ( ) ;
244
246
247
+ SetupMockInboundActivityHandler ( handler ) ;
248
+
245
249
handler
246
250
. Setup ( h => h . OnCircuitOpenedAsync ( It . IsAny < Circuit > ( ) , It . IsAny < CancellationToken > ( ) ) )
247
251
. Returns ( tcs . Task )
@@ -284,6 +288,8 @@ public async Task DisposeAsync_InvokesCircuitHandler()
284
288
var handler2 = new Mock < CircuitHandler > ( MockBehavior . Strict ) ;
285
289
var sequence = new MockSequence ( ) ;
286
290
291
+ SetupMockInboundActivityHandlers ( sequence , handler1 , handler2 ) ;
292
+
287
293
handler1
288
294
. InSequence ( sequence )
289
295
. Setup ( h => h . OnConnectionDownAsync ( It . IsAny < Circuit > ( ) , cancellationToken ) )
@@ -327,29 +333,31 @@ public async Task HandleInboundActivityAsync_InvokesCircuitActivityHandlers()
327
333
var handler3 = new Mock < CircuitHandler > ( MockBehavior . Strict ) ;
328
334
var sequence = new MockSequence ( ) ;
329
335
330
- // We deliberately avoid making handler2 an inbound activity handler
331
- var activityHandler1 = handler1 . As < IHandleCircuitActivity > ( ) ;
332
- var activityHandler3 = handler3 . As < IHandleCircuitActivity > ( ) ;
333
-
334
336
var asyncLocal1 = new AsyncLocal < bool > ( ) ;
335
337
var asyncLocal3 = new AsyncLocal < bool > ( ) ;
336
338
337
- activityHandler1
339
+ handler3
338
340
. InSequence ( sequence )
339
- . Setup ( h => h . HandleInboundActivityAsync ( It . IsAny < CircuitInboundActivityContext > ( ) , It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
340
- . Returns ( async ( CircuitInboundActivityContext context , Func < CircuitInboundActivityContext , Task > next ) =>
341
+ . Setup ( h => h . CreateInboundActivityHandler ( It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
342
+ . Returns ( ( Func < CircuitInboundActivityContext , Task > next ) => async ( CircuitInboundActivityContext context ) =>
341
343
{
342
- asyncLocal1 . Value = true ;
344
+ asyncLocal3 . Value = true ;
343
345
await next ( context ) ;
344
346
} )
345
347
. Verifiable ( ) ;
346
348
347
- activityHandler3
349
+ handler2
350
+ . InSequence ( sequence )
351
+ . Setup ( h => h . CreateInboundActivityHandler ( It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
352
+ . Returns ( ( Func < CircuitInboundActivityContext , Task > next ) => next )
353
+ . Verifiable ( ) ;
354
+
355
+ handler1
348
356
. InSequence ( sequence )
349
- . Setup ( h => h . HandleInboundActivityAsync ( It . IsAny < CircuitInboundActivityContext > ( ) , It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
350
- . Returns ( async ( CircuitInboundActivityContext context , Func < CircuitInboundActivityContext , Task > next ) =>
357
+ . Setup ( h => h . CreateInboundActivityHandler ( It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
358
+ . Returns ( ( Func < CircuitInboundActivityContext , Task > next ) => async ( CircuitInboundActivityContext context ) =>
351
359
{
352
- asyncLocal3 . Value = true ;
360
+ asyncLocal1 . Value = true ;
353
361
await next ( context ) ;
354
362
} )
355
363
. Verifiable ( ) ;
@@ -367,8 +375,9 @@ await circuitHost.HandleInboundActivityAsync(() =>
367
375
} ) ;
368
376
369
377
// Assert
370
- activityHandler1 . VerifyAll ( ) ;
371
- activityHandler3 . VerifyAll ( ) ;
378
+ handler1 . VerifyAll ( ) ;
379
+ handler2 . VerifyAll ( ) ;
380
+ handler3 . VerifyAll ( ) ;
372
381
373
382
Assert . False ( asyncLocal1 . Value ) ;
374
383
Assert . False ( asyncLocal3 . Value ) ;
@@ -404,6 +413,26 @@ private static TestRemoteRenderer GetRemoteRenderer()
404
413
Mock . Of < IClientProxy > ( ) ) ;
405
414
}
406
415
416
+ private static void SetupMockInboundActivityHandlers ( MockSequence sequence , params Mock < CircuitHandler > [ ] circuitHandlers )
417
+ {
418
+ for ( var i = circuitHandlers . Length - 1 ; i >= 0 ; i -- )
419
+ {
420
+ circuitHandlers [ i ]
421
+ . InSequence ( sequence )
422
+ . Setup ( h => h . CreateInboundActivityHandler ( It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
423
+ . Returns ( ( Func < CircuitInboundActivityContext , Task > next ) => next )
424
+ . Verifiable ( ) ;
425
+ }
426
+ }
427
+
428
+ private static void SetupMockInboundActivityHandler ( Mock < CircuitHandler > circuitHandler )
429
+ {
430
+ circuitHandler
431
+ . Setup ( h => h . CreateInboundActivityHandler ( It . IsAny < Func < CircuitInboundActivityContext , Task > > ( ) ) )
432
+ . Returns ( ( Func < CircuitInboundActivityContext , Task > next ) => next )
433
+ . Verifiable ( ) ;
434
+ }
435
+
407
436
private class TestRemoteRenderer : RemoteRenderer
408
437
{
409
438
public TestRemoteRenderer ( IServiceProvider serviceProvider , IClientProxy client )
0 commit comments