@@ -17,6 +17,14 @@ public record TestEvent(string Name) : ITestEvent;
1717 public record OtherEvent ( string Name ) : ITestEvent ;
1818 public record UnrelatedEvent ( string Name ) ;
1919
20+ public record CascadeCommand ( string Name ) ;
21+
22+ public class CascadeHandler
23+ {
24+ public ( Result , TestEvent ) Handle ( CascadeCommand cmd )
25+ => ( Result . Success ( ) , new TestEvent ( cmd . Name ) ) ;
26+ }
27+
2028 /// <summary>Polls until <paramref name="condition"/> returns true or the timeout expires.</summary>
2129 private static async Task WaitUntilAsync ( Func < bool > condition , int timeoutMs = 2000 )
2230 {
@@ -355,4 +363,39 @@ await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
355363 { }
356364 } ) ;
357365 }
366+
367+ [ Fact ]
368+ public async Task SubscribeAsync_ReceivesCascadingEvents ( )
369+ {
370+ var services = new ServiceCollection ( ) ;
371+ services . AddLogging ( c => c . AddTestLogger ( o => o . UseOutputHelper ( ( ) => _output ) ) ) ;
372+ services . AddMediator ( b => b . AddAssembly < TestEvent > ( ) ) ;
373+
374+ await using var provider = services . BuildServiceProvider ( ) ;
375+ var registry = provider . GetRequiredService < HandlerRegistry > ( ) ;
376+ var mediator = provider . GetRequiredService < IMediator > ( ) ;
377+
378+ using var cts = new CancellationTokenSource ( ) ;
379+ var received = new List < TestEvent > ( ) ;
380+
381+ var subscriberTask = Task . Run ( async ( ) =>
382+ {
383+ await foreach ( var item in mediator . SubscribeAsync < TestEvent > ( cts . Token ) )
384+ {
385+ received . Add ( item ) ;
386+ }
387+ } ) ;
388+
389+ await WaitUntilAsync ( ( ) => registry . HasSubscribers ) ;
390+
391+ // Invoke a command whose handler returns a cascading TestEvent via tuple
392+ await mediator . InvokeAsync < Result > ( new CascadeCommand ( "from-cascade" ) ) ;
393+
394+ await WaitUntilAsync ( ( ) => received . Count >= 1 ) ;
395+ cts . Cancel ( ) ;
396+ await subscriberTask ;
397+
398+ Assert . Single ( received ) ;
399+ Assert . Equal ( "from-cascade" , received [ 0 ] . Name ) ;
400+ }
358401}
0 commit comments