From 8c61db91648ae4852ac7db7522eb0168e01e01a0 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 2 Apr 2015 15:22:31 -0700 Subject: cleanup of C# API examples --- src/csharp/Grpc.Examples.MathClient/MathClient.cs | 10 +++- .../Grpc.Examples.Tests/Grpc.Examples.Tests.csproj | 12 ++++ .../Grpc.Examples.Tests/MathClientServerTests.cs | 21 +++---- src/csharp/Grpc.Examples.Tests/packages.config | 13 +++-- src/csharp/Grpc.Examples/MathExamples.cs | 67 ++++++++++------------ 5 files changed, 66 insertions(+), 57 deletions(-) (limited to 'src/csharp') diff --git a/src/csharp/Grpc.Examples.MathClient/MathClient.cs b/src/csharp/Grpc.Examples.MathClient/MathClient.cs index f5956bd33e..ca7683d399 100644 --- a/src/csharp/Grpc.Examples.MathClient/MathClient.cs +++ b/src/csharp/Grpc.Examples.MathClient/MathClient.cs @@ -46,11 +46,15 @@ namespace math MathGrpc.IMathServiceClient stub = new MathGrpc.MathServiceClientStub(channel); MathExamples.DivExample(stub); - MathExamples.FibExample(stub); + MathExamples.DivAsyncExample(stub).Wait(); - MathExamples.SumExample(stub); + MathExamples.FibExample(stub).Wait(); - MathExamples.DivManyExample(stub); + MathExamples.SumExample(stub).Wait(); + + MathExamples.DivManyExample(stub).Wait(); + + MathExamples.DependendRequestsExample(stub).Wait(); } GrpcEnvironment.Shutdown(); diff --git a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj index cf5a640079..f9c1caf700 100644 --- a/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj +++ b/src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj @@ -37,6 +37,18 @@ ..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll + + ..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll + + + ..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll + + + ..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll + + + ..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll + diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 85f213cb39..fa5d6688a6 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -33,6 +33,7 @@ using System; using System.Collections.Generic; +using System.Reactive.Linq; using System.Threading; using System.Threading.Tasks; using Grpc.Core; @@ -120,14 +121,12 @@ namespace math.Tests [Test] public void Sum() { - var res = client.Sum(); - foreach (var num in new long[] { 10, 20, 30 }) - { - res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build()); - } - res.Inputs.OnCompleted(); + var clientStreamingResult = client.Sum(); + var numList = new List { 10, 20, 30 }.ConvertAll( + n => Num.CreateBuilder().SetNum_(n).Build()); + numList.Subscribe(clientStreamingResult.Inputs); - Assert.AreEqual(60, res.Task.Result.Num_); + Assert.AreEqual(60, clientStreamingResult.Task.Result.Num_); } [Test] @@ -142,13 +141,7 @@ namespace math.Tests var recorder = new RecordingObserver(); var requestObserver = client.DivMany(recorder); - - foreach (var arg in divArgsList) - { - requestObserver.OnNext(arg); - } - requestObserver.OnCompleted(); - + divArgsList.Subscribe(requestObserver); var result = recorder.ToList().Result; CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); diff --git a/src/csharp/Grpc.Examples.Tests/packages.config b/src/csharp/Grpc.Examples.Tests/packages.config index 51c17bcd5e..06c5e6a4eb 100644 --- a/src/csharp/Grpc.Examples.Tests/packages.config +++ b/src/csharp/Grpc.Examples.Tests/packages.config @@ -1,5 +1,10 @@ - - - - + + + + + + + + + \ No newline at end of file diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index b8bb7eacbd..032372b2a1 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -45,51 +45,45 @@ namespace math Console.WriteLine("Div Result: " + result); } - public static void DivAsyncExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncExample(MathGrpc.IMathServiceClient stub) { - Task call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); - DivReply result = call.Result; - Console.WriteLine(result); + Task resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); + DivReply result = await resultTask; + Console.WriteLine("DivAsync Result: " + result); } - public static void DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) + public static async Task DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) { - Task call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); - DivReply result = call.Result; + Task resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); + DivReply result = await resultTask; Console.WriteLine(result); } - public static void FibExample(MathGrpc.IMathServiceClient stub) + public static async Task FibExample(MathGrpc.IMathServiceClient stub) { var recorder = new RecordingObserver(); stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder); - - List numbers = recorder.ToList().Result; - Console.WriteLine("Fib Result: " + string.Join("|", recorder.ToList().Result)); + List result = await recorder.ToList(); + Console.WriteLine("Fib Result: " + string.Join("|", result)); } - public static void SumExample(MathGrpc.IMathServiceClient stub) + public static async Task SumExample(MathGrpc.IMathServiceClient stub) { - List numbers = new List + var numbers = new List { new Num.Builder { Num_ = 1 }.Build(), new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 3 }.Build() }; - var res = stub.Sum(); - foreach (var num in numbers) - { - res.Inputs.OnNext(num); - } - res.Inputs.OnCompleted(); - - Console.WriteLine("Sum Result: " + res.Task.Result); + var clientStreamingResult = stub.Sum(); + numbers.Subscribe(clientStreamingResult.Inputs); + Console.WriteLine("Sum Result: " + await clientStreamingResult.Task); } - public static void DivManyExample(MathGrpc.IMathServiceClient stub) + public static async Task DivManyExample(MathGrpc.IMathServiceClient stub) { - List divArgsList = new List + var divArgsList = new List { new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), @@ -97,26 +91,27 @@ namespace math }; var recorder = new RecordingObserver(); - var inputs = stub.DivMany(recorder); - foreach (var input in divArgsList) - { - inputs.OnNext(input); - } - inputs.OnCompleted(); - - Console.WriteLine("DivMany Result: " + string.Join("|", recorder.ToList().Result)); + divArgsList.Subscribe(inputs); + var result = await recorder.ToList(); + Console.WriteLine("DivMany Result: " + string.Join("|", result)); } - public static void DependendRequestsExample(MathGrpc.IMathServiceClient stub) + public static async Task DependendRequestsExample(MathGrpc.IMathServiceClient stub) { - var numberList = new List + var numbers = new List { - new Num.Builder { Num_ = 1 }.Build(), - new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 3 }.Build() + new Num.Builder { Num_ = 1 }.Build(), + new Num.Builder { Num_ = 2 }.Build(), + new Num.Builder { Num_ = 3 }.Build() }; - numberList.ToObservable(); + var clientStreamingResult = stub.Sum(); + numbers.Subscribe(clientStreamingResult.Inputs); + Num sum = await clientStreamingResult.Task; + + DivReply result = await stub.DivAsync(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numbers.Count }.Build()); + Console.WriteLine("Avg Result: " + result); } } } -- cgit v1.2.3