@@ -78,6 +78,50 @@ public async Task ClosedEventRaisedWhenTheClientIsStopped()
78
78
Assert . Null ( await closedEventTcs . Task ) ;
79
79
}
80
80
81
+ [ Fact ]
82
+ public async Task StopAsyncCanBeCalledFromOnHandler ( )
83
+ {
84
+ var connection = new TestConnection ( ) ;
85
+ var hubConnection = CreateHubConnection ( connection , loggerFactory : LoggerFactory ) ;
86
+
87
+ var tcs = new TaskCompletionSource < object > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
88
+ hubConnection . On ( "method" , async ( ) =>
89
+ {
90
+ await hubConnection . StopAsync ( ) . OrTimeout ( ) ;
91
+ tcs . SetResult ( null ) ;
92
+ } ) ;
93
+
94
+ await hubConnection . StartAsync ( ) . OrTimeout ( ) ;
95
+
96
+ await connection . ReceiveJsonMessage ( new { type = HubProtocolConstants . InvocationMessageType , target = "method" , arguments = new object [ ] { } } ) . OrTimeout ( ) ;
97
+
98
+ Assert . Null ( await tcs . Task . OrTimeout ( ) ) ;
99
+ }
100
+
101
+ [ Fact ]
102
+ public async Task StopAsyncDoesNotWaitForOnHandlers ( )
103
+ {
104
+ var connection = new TestConnection ( ) ;
105
+ var hubConnection = CreateHubConnection ( connection , loggerFactory : LoggerFactory ) ;
106
+
107
+ var tcs = new TaskCompletionSource < object > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
108
+ var methodCalledTcs = new TaskCompletionSource < object > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
109
+ hubConnection . On ( "method" , async ( ) =>
110
+ {
111
+ methodCalledTcs . SetResult ( null ) ;
112
+ await tcs . Task ;
113
+ } ) ;
114
+
115
+ await hubConnection . StartAsync ( ) . OrTimeout ( ) ;
116
+
117
+ await connection . ReceiveJsonMessage ( new { type = HubProtocolConstants . InvocationMessageType , target = "method" , arguments = new object [ ] { } } ) . OrTimeout ( ) ;
118
+
119
+ await methodCalledTcs . Task . OrTimeout ( ) ;
120
+ await hubConnection . StopAsync ( ) . OrTimeout ( ) ;
121
+
122
+ tcs . SetResult ( null ) ;
123
+ }
124
+
81
125
[ Fact ]
82
126
public async Task PendingInvocationsAreCanceledWhenConnectionClosesCleanly ( )
83
127
{
0 commit comments