@@ -36,6 +36,31 @@ public async Task UnSubscribedAsyncActionNotInvoked()
3636 Assert . Equal ( 1 , handler . ReceivedMessageCount ) ;
3737 }
3838
39+ [ Fact ]
40+ public async Task UnSubscribedAsyncActionNotInvokedButOthersAre ( )
41+ {
42+ var mediatRCourier = new MediatRCourier ( new ( ) ) ;
43+
44+ var handler = new Handler ( new ( ) , new ( ) ) ;
45+ var handler2 = new Handler ( new ( ) , new ( ) ) ;
46+
47+ mediatRCourier . SubscribeWeak < TestNotification > ( handler . NotificationAction ) ;
48+ mediatRCourier . SubscribeWeak < TestNotification > ( handler . NotificationAction2 ) ;
49+ mediatRCourier . SubscribeWeak < TestNotification > ( handler2 . NotificationAction ) ;
50+ mediatRCourier . SubscribeWeak < TestNotification > ( handler2 . NotificationAction2 ) ;
51+
52+ await mediatRCourier . Handle ( new TestNotification ( ) , CancellationToken . None ) ;
53+
54+ mediatRCourier . UnSubscribe < TestNotification > ( handler . NotificationAction ) ;
55+
56+ await mediatRCourier . Handle ( new TestNotification ( ) , CancellationToken . None ) ;
57+
58+ Assert . Equal ( 1 , handler . ReceivedMessageCount ) ;
59+ Assert . Equal ( 2 , handler . ReceivedMessageCount2 ) ;
60+ Assert . Equal ( 2 , handler2 . ReceivedMessageCount ) ;
61+ Assert . Equal ( 2 , handler2 . ReceivedMessageCount2 ) ;
62+ }
63+
3964 [ Fact ]
4065 [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Critical Code Smell" , "S1215:\" GC.Collect\" should not be called" , Justification = "We want to test behavior around GC collection" ) ]
4166 [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "CodeQuality" , "IDE0079:Remove unnecessary suppression" , Justification = "False positive" ) ]
@@ -71,8 +96,13 @@ private sealed class Counter
7196 private sealed class Handler
7297 {
7398 private readonly Counter _counter ;
99+ private readonly Counter ? _counter2 ;
74100
75- public Handler ( Counter counter ) => _counter = counter ;
101+ public Handler ( Counter counter , Counter ? counter2 = null )
102+ {
103+ _counter = counter ;
104+ _counter2 = counter2 ;
105+ }
76106
77107#pragma warning disable S1172 // Unused method parameters should be removed
78108 public async Task NotificationAction ( TestNotification _ , CancellationToken cancellationToken )
@@ -83,6 +113,18 @@ public async Task NotificationAction(TestNotification _, CancellationToken cance
83113 await Task . Delay ( TimeSpan . FromMilliseconds ( 1 ) , cancellationToken ) ;
84114 }
85115
116+ #pragma warning disable S1172 // Unused method parameters should be removed
117+ public async Task NotificationAction2 ( TestNotification _ , CancellationToken cancellationToken )
118+ #pragma warning restore S1172 // Unused method parameters should be removed
119+ {
120+ if ( _counter2 is null ) return ;
121+
122+ _counter2 . ReceivedMessageCount ++ ;
123+
124+ await Task . Delay ( TimeSpan . FromMilliseconds ( 1 ) , cancellationToken ) . ConfigureAwait ( false ) ;
125+ }
126+
86127 public int ReceivedMessageCount => _counter . ReceivedMessageCount ;
128+ public int ? ReceivedMessageCount2 => _counter2 ? . ReceivedMessageCount ;
87129 }
88130}
0 commit comments