|
1 | | -#region Copyright notice and license |
| 1 | +#region Copyright notice and license |
2 | 2 |
|
3 | 3 | // Copyright 2019 The gRPC Authors |
4 | 4 | // |
@@ -107,7 +107,6 @@ public async Task AsyncClientStreamingCall_Success_RequestContentSent() |
107 | 107 | var responseTask = call.ResponseAsync; |
108 | 108 | Assert.IsFalse(responseTask.IsCompleted, "Response not returned until client stream is complete."); |
109 | 109 |
|
110 | | - |
111 | 110 | await call.RequestStream.WriteAsync(new HelloRequest { Name = "1" }).DefaultTimeout(); |
112 | 111 | await call.RequestStream.WriteAsync(new HelloRequest { Name = "2" }).DefaultTimeout(); |
113 | 112 |
|
@@ -268,6 +267,106 @@ public async Task ClientStreamWriter_WriteAfterResponseHasFinished_ErrorThrown() |
268 | 267 | Assert.AreEqual("Hello world", result.Message); |
269 | 268 | } |
270 | 269 |
|
| 270 | + [Test] |
| 271 | + public async Task AsyncClientStreamingCall_ErrorWhileWriting_StatusExceptionThrown() |
| 272 | + { |
| 273 | + // Arrange |
| 274 | + PushStreamContent<HelloRequest, HelloReply>? content = null; |
| 275 | + |
| 276 | + var responseTcs = new TaskCompletionSource<HttpResponseMessage>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 277 | + var httpClient = ClientTestHelpers.CreateTestClient(request => |
| 278 | + { |
| 279 | + content = (PushStreamContent<HelloRequest, HelloReply>)request.Content!; |
| 280 | + return responseTcs.Task; |
| 281 | + }); |
| 282 | + |
| 283 | + var invoker = HttpClientCallInvokerFactory.Create(httpClient); |
| 284 | + |
| 285 | + // Act |
| 286 | + |
| 287 | + // Client starts call |
| 288 | + var call = invoker.AsyncClientStreamingCall<HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions()); |
| 289 | + // Client starts request stream write |
| 290 | + var writeTask = call.RequestStream.WriteAsync(new HelloRequest()); |
| 291 | + |
| 292 | + // Simulate HttpClient starting to accept the write. Stream.WriteAsync is blocked. |
| 293 | + var writeSyncPoint = new SyncPoint(runContinuationsAsynchronously: true); |
| 294 | + var testStream = new TestStream(writeSyncPoint); |
| 295 | + var serializeToStreamTask = content!.SerializeToStreamAsync(testStream); |
| 296 | + |
| 297 | + // Server completes response. |
| 298 | + await writeSyncPoint.WaitForSyncPoint().DefaultTimeout(); |
| 299 | + responseTcs.SetResult(ResponseUtils.CreateResponse(HttpStatusCode.OK, new ByteArrayContent(Array.Empty<byte>()), grpcStatusCode: StatusCode.InvalidArgument)); |
| 300 | + |
| 301 | + await ExceptionAssert.ThrowsAsync<RpcException>(() => call.ResponseAsync).DefaultTimeout(); |
| 302 | + Assert.AreEqual(StatusCode.InvalidArgument, call.GetStatus().StatusCode); |
| 303 | + |
| 304 | + // Unblock Stream.WriteAsync |
| 305 | + writeSyncPoint.Continue(); |
| 306 | + |
| 307 | + // Get error thrown from write task. It should have the status returned by the server. |
| 308 | + var ex = await ExceptionAssert.ThrowsAsync<RpcException>(() => writeTask).DefaultTimeout(); |
| 309 | + |
| 310 | + // Assert |
| 311 | + Assert.AreEqual(StatusCode.InvalidArgument, ex.StatusCode); |
| 312 | + Assert.AreEqual(StatusCode.InvalidArgument, call.GetStatus().StatusCode); |
| 313 | + Assert.AreEqual(string.Empty, call.GetStatus().Detail); |
| 314 | + } |
| 315 | + |
| 316 | + private sealed class TestStream : Stream |
| 317 | + { |
| 318 | + private readonly SyncPoint _writeSyncPoint; |
| 319 | + |
| 320 | + public TestStream(SyncPoint writeSyncPoint) |
| 321 | + { |
| 322 | + _writeSyncPoint = writeSyncPoint; |
| 323 | + } |
| 324 | + |
| 325 | + public override bool CanRead { get; } |
| 326 | + public override bool CanSeek { get; } |
| 327 | + public override bool CanWrite { get; } |
| 328 | + public override long Length { get; } |
| 329 | + public override long Position { get; set; } |
| 330 | + |
| 331 | + public override void Flush() |
| 332 | + { |
| 333 | + } |
| 334 | + |
| 335 | + public override int Read(byte[] buffer, int offset, int count) |
| 336 | + { |
| 337 | + throw new NotImplementedException(); |
| 338 | + } |
| 339 | + |
| 340 | + public override long Seek(long offset, SeekOrigin origin) |
| 341 | + { |
| 342 | + throw new NotImplementedException(); |
| 343 | + } |
| 344 | + |
| 345 | + public override void SetLength(long value) |
| 346 | + { |
| 347 | + throw new NotImplementedException(); |
| 348 | + } |
| 349 | + |
| 350 | + public override void Write(byte[] buffer, int offset, int count) |
| 351 | + { |
| 352 | + throw new NotImplementedException(); |
| 353 | + } |
| 354 | + |
| 355 | +#if !NET472_OR_GREATER |
| 356 | + public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| 357 | + { |
| 358 | + await _writeSyncPoint.WaitToContinue(); |
| 359 | + throw new OperationCanceledException(); |
| 360 | + } |
| 361 | +#else |
| 362 | + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 363 | + { |
| 364 | + await _writeSyncPoint.WaitToContinue(); |
| 365 | + throw new OperationCanceledException(); |
| 366 | + } |
| 367 | +#endif |
| 368 | + } |
| 369 | + |
271 | 370 | [Test] |
272 | 371 | public async Task ClientStreamWriter_CancelledBeforeCallStarts_ThrowsError() |
273 | 372 | { |
|
0 commit comments