@@ -80,6 +80,8 @@ public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
80
80
81
81
public class NoErrorHubConnectionContext : HubConnectionContext
82
82
{
83
+ public TaskCompletionSource < object > ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
84
+
83
85
public NoErrorHubConnectionContext ( ConnectionContext connectionContext , TimeSpan keepAliveInterval , ILoggerFactory loggerFactory ) : base ( connectionContext , keepAliveInterval , loggerFactory )
84
86
{
85
87
}
@@ -88,6 +90,8 @@ public override ValueTask WriteAsync(HubMessage message, CancellationToken cance
88
90
{
89
91
if ( message is CompletionMessage completionMessage )
90
92
{
93
+ ReceivedCompleted . TrySetResult ( null ) ;
94
+
91
95
if ( ! string . IsNullOrEmpty ( completionMessage . Error ) )
92
96
{
93
97
throw new Exception ( "Error invoking hub method: " + completionMessage . Error ) ;
@@ -163,72 +167,116 @@ public ChannelReader<int> StreamChannelReaderCount(int count)
163
167
164
168
return channel . Reader ;
165
169
}
170
+
171
+ public async Task UploadStream ( ChannelReader < string > channelReader )
172
+ {
173
+ while ( await channelReader . WaitToReadAsync ( ) )
174
+ {
175
+ while ( channelReader . TryRead ( out var item ) )
176
+ {
177
+ }
178
+ }
179
+ }
166
180
}
167
181
168
182
[ Benchmark ]
169
183
public Task Invocation ( )
170
184
{
171
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "Invocation" , Array . Empty < object > ( ) ) ) ;
185
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "Invocation" , Array . Empty < object > ( ) ) ) ;
172
186
}
173
187
174
188
[ Benchmark ]
175
189
public Task InvocationAsync ( )
176
190
{
177
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationAsync" , Array . Empty < object > ( ) ) ) ;
191
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationAsync" , Array . Empty < object > ( ) ) ) ;
178
192
}
179
193
180
194
[ Benchmark ]
181
195
public Task InvocationReturnValue ( )
182
196
{
183
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnValue" , Array . Empty < object > ( ) ) ) ;
197
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnValue" , Array . Empty < object > ( ) ) ) ;
184
198
}
185
199
186
200
[ Benchmark ]
187
201
public Task InvocationReturnAsync ( )
188
202
{
189
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnAsync" , Array . Empty < object > ( ) ) ) ;
203
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnAsync" , Array . Empty < object > ( ) ) ) ;
190
204
}
191
205
192
206
[ Benchmark ]
193
207
public Task InvocationValueTaskAsync ( )
194
208
{
195
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
209
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
196
210
}
197
211
198
212
[ Benchmark ]
199
213
public Task StreamChannelReader ( )
200
214
{
201
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReader" , Array . Empty < object > ( ) ) ) ;
215
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReader" , Array . Empty < object > ( ) ) ) ;
202
216
}
203
217
204
218
[ Benchmark ]
205
219
public Task StreamChannelReaderAsync ( )
206
220
{
207
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderAsync" , Array . Empty < object > ( ) ) ) ;
221
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderAsync" , Array . Empty < object > ( ) ) ) ;
208
222
}
209
223
210
224
[ Benchmark ]
211
225
public Task StreamChannelReaderValueTaskAsync ( )
212
226
{
213
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
227
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
214
228
}
215
229
216
230
[ Benchmark ]
217
- public Task StreamChannelReaderCount_Zero ( )
231
+ public async Task StreamChannelReaderCount_Zero ( )
218
232
{
219
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 0 } ) ) ;
233
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 0 } ) ) ;
234
+
235
+ await ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted . Task ;
236
+ ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
220
237
}
221
238
222
239
[ Benchmark ]
223
- public Task StreamChannelReaderCount_One ( )
240
+ public async Task StreamChannelReaderCount_One ( )
224
241
{
225
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1 } ) ) ;
242
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1 } ) ) ;
243
+
244
+ await ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted . Task ;
245
+ ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
246
+ }
247
+
248
+ [ Benchmark ]
249
+ public async Task StreamChannelReaderCount_Thousand ( )
250
+ {
251
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1000 } ) ) ;
252
+
253
+ await ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted . Task ;
254
+ ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
255
+ }
256
+
257
+ [ Benchmark ]
258
+ public async Task UploadStream_One ( )
259
+ {
260
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , nameof ( TestHub . UploadStream ) , Array . Empty < object > ( ) , streamIds : new string [ ] { "1" } ) ) ;
261
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamItemMessage ( "1" , "test" ) ) ;
262
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , CompletionMessage . Empty ( "1" ) ) ;
263
+
264
+ await ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted . Task ;
265
+ ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
226
266
}
227
267
228
268
[ Benchmark ]
229
- public Task StreamChannelReaderCount_Thousand ( )
269
+ public async Task UploadStream_Thousand ( )
230
270
{
231
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1000 } ) ) ;
271
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , nameof ( TestHub . UploadStream ) , Array . Empty < object > ( ) , streamIds : new string [ ] { "1" } ) ) ;
272
+ for ( var i = 0 ; i < 1000 ; ++ i )
273
+ {
274
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamItemMessage ( "1" , "test" ) ) ;
275
+ }
276
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , CompletionMessage . Empty ( "1" ) ) ;
277
+
278
+ await ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted . Task ;
279
+ ( _connectionContext as NoErrorHubConnectionContext ) . ReceivedCompleted = new TaskCompletionSource < object > ( ) ;
232
280
}
233
281
}
234
282
}
0 commit comments