From 2d2652d61b87933c6f790ac7e89066e62821b5b9 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 18 May 2015 16:23:04 -0700 Subject: [PATCH] implemented disposal logic for calls --- src/csharp/Grpc.Core/AsyncClientStreamingCall.cs | 7 ++++--- src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs | 7 ++++--- src/csharp/Grpc.Core/AsyncServerStreamingCall.cs | 7 ++++--- src/csharp/Grpc.Core/Calls.cs | 6 +++--- .../Grpc.Examples.Tests/MathClientServerTests.cs | 16 +++++++++++++--- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs index 83231ce4df8..d66b0d49749 100644 --- a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs @@ -44,11 +44,13 @@ namespace Grpc.Core { readonly IClientStreamWriter requestStream; readonly Task result; + readonly Action disposeAction; - public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task result) + public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task result, Action disposeAction) { this.requestStream = requestStream; this.result = result; + this.disposeAction = disposeAction; } /// @@ -90,8 +92,7 @@ namespace Grpc.Core /// public void Dispose() { - // TODO(jtattermusch): implement - throw new NotImplementedException(); + disposeAction.Invoke(); } } } diff --git a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs index 39a54cc9a3a..4c0d5936ac4 100644 --- a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs @@ -44,11 +44,13 @@ namespace Grpc.Core { readonly IClientStreamWriter requestStream; readonly IAsyncStreamReader responseStream; + readonly Action disposeAction; - public AsyncDuplexStreamingCall(IClientStreamWriter requestStream, IAsyncStreamReader responseStream) + public AsyncDuplexStreamingCall(IClientStreamWriter requestStream, IAsyncStreamReader responseStream, Action disposeAction) { this.requestStream = requestStream; this.responseStream = responseStream; + this.disposeAction = disposeAction; } /// @@ -81,8 +83,7 @@ namespace Grpc.Core /// public void Dispose() { - // TODO(jtattermusch): implement - throw new NotImplementedException(); + disposeAction.Invoke(); } } } diff --git a/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs index 1036f4a3ec3..7a479b9a23d 100644 --- a/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs @@ -43,10 +43,12 @@ namespace Grpc.Core public sealed class AsyncServerStreamingCall : IDisposable { readonly IAsyncStreamReader responseStream; + readonly Action disposeAction; - public AsyncServerStreamingCall(IAsyncStreamReader responseStream) + public AsyncServerStreamingCall(IAsyncStreamReader responseStream, Action disposeAction) { this.responseStream = responseStream; + this.disposeAction = disposeAction; } /// @@ -68,8 +70,7 @@ namespace Grpc.Core /// public void Dispose() { - // TODO(jtattermusch): implement - throw new NotImplementedException(); + disposeAction.Invoke(); } } } diff --git a/src/csharp/Grpc.Core/Calls.cs b/src/csharp/Grpc.Core/Calls.cs index ba42a2d4f87..9f8baac6841 100644 --- a/src/csharp/Grpc.Core/Calls.cs +++ b/src/csharp/Grpc.Core/Calls.cs @@ -73,7 +73,7 @@ namespace Grpc.Core asyncCall.StartServerStreamingCall(req, call.Headers); RegisterCancellationCallback(asyncCall, token); var responseStream = new ClientResponseStream(asyncCall); - return new AsyncServerStreamingCall(responseStream); + return new AsyncServerStreamingCall(responseStream, asyncCall.Cancel); } public static AsyncClientStreamingCall AsyncClientStreamingCall(Call call, CancellationToken token) @@ -85,7 +85,7 @@ namespace Grpc.Core var resultTask = asyncCall.ClientStreamingCallAsync(call.Headers); RegisterCancellationCallback(asyncCall, token); var requestStream = new ClientRequestStream(asyncCall); - return new AsyncClientStreamingCall(requestStream, resultTask); + return new AsyncClientStreamingCall(requestStream, resultTask, asyncCall.Cancel); } public static AsyncDuplexStreamingCall AsyncDuplexStreamingCall(Call call, CancellationToken token) @@ -98,7 +98,7 @@ namespace Grpc.Core RegisterCancellationCallback(asyncCall, token); var requestStream = new ClientRequestStream(asyncCall); var responseStream = new ClientResponseStream(asyncCall); - return new AsyncDuplexStreamingCall(requestStream, responseStream); + return new AsyncDuplexStreamingCall(requestStream, responseStream, asyncCall.Cancel); } private static void RegisterCancellationCallback(AsyncCall asyncCall, CancellationToken token) diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 267852487c8..5aa6f4162d4 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -96,7 +96,19 @@ namespace math.Tests Assert.AreEqual(0, response.Remainder); } - // TODO(jtattermusch): test division by zero + [Test] + public void DivByZero() + { + try + { + DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 0 }.Build()); + Assert.Fail(); + } + catch (RpcException e) + { + Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); + } + } [Test] public void DivAsync() @@ -119,7 +131,6 @@ namespace math.Tests var responses = await call.ResponseStream.ToList(); CollectionAssert.AreEqual(new List { 1, 1, 2, 3, 5, 8 }, responses.ConvertAll((n) => n.Num_)); - } }).Wait(); } @@ -154,7 +165,6 @@ namespace math.Tests new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() }; - using (var call = client.DivMany()) { await call.RequestStream.WriteAll(divArgsList);