cleanup of C# API examples

pull/1204/head
Jan Tattermusch 10 years ago
parent c7439042b1
commit 8c61db9164
  1. 10
      src/csharp/Grpc.Examples.MathClient/MathClient.cs
  2. 12
      src/csharp/Grpc.Examples.Tests/Grpc.Examples.Tests.csproj
  3. 21
      src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
  4. 13
      src/csharp/Grpc.Examples.Tests/packages.config
  5. 67
      src/csharp/Grpc.Examples/MathExamples.cs

@ -46,11 +46,15 @@ namespace math
MathGrpc.IMathServiceClient stub = new MathGrpc.MathServiceClientStub(channel); MathGrpc.IMathServiceClient stub = new MathGrpc.MathServiceClientStub(channel);
MathExamples.DivExample(stub); 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(); GrpcEnvironment.Shutdown();

@ -37,6 +37,18 @@
<Reference Include="Google.ProtocolBuffers"> <Reference Include="Google.ProtocolBuffers">
<HintPath>..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll</HintPath> <HintPath>..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Reactive.Interfaces">
<HintPath>..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Core">
<HintPath>..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.Linq">
<HintPath>..\packages\Rx-Linq.2.2.5\lib\net45\System.Reactive.Linq.dll</HintPath>
</Reference>
<Reference Include="System.Reactive.PlatformServices">
<HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

@ -33,6 +33,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Grpc.Core; using Grpc.Core;
@ -120,14 +121,12 @@ namespace math.Tests
[Test] [Test]
public void Sum() public void Sum()
{ {
var res = client.Sum(); var clientStreamingResult = client.Sum();
foreach (var num in new long[] { 10, 20, 30 }) var numList = new List<long> { 10, 20, 30 }.ConvertAll(
{ n => Num.CreateBuilder().SetNum_(n).Build());
res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build()); numList.Subscribe(clientStreamingResult.Inputs);
}
res.Inputs.OnCompleted();
Assert.AreEqual(60, res.Task.Result.Num_); Assert.AreEqual(60, clientStreamingResult.Task.Result.Num_);
} }
[Test] [Test]
@ -142,13 +141,7 @@ namespace math.Tests
var recorder = new RecordingObserver<DivReply>(); var recorder = new RecordingObserver<DivReply>();
var requestObserver = client.DivMany(recorder); var requestObserver = client.DivMany(recorder);
divArgsList.Subscribe(requestObserver);
foreach (var arg in divArgsList)
{
requestObserver.OnNext(arg);
}
requestObserver.OnCompleted();
var result = recorder.ToList().Result; var result = recorder.ToList().Result;
CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient));

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" /> <package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" /> <package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="Rx-Core" version="2.2.5" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
</packages> </packages>

@ -45,51 +45,45 @@ namespace math
Console.WriteLine("Div Result: " + result); Console.WriteLine("Div Result: " + result);
} }
public static void DivAsyncExample(MathGrpc.IMathServiceClient stub) public static async Task DivAsyncExample(MathGrpc.IMathServiceClient stub)
{ {
Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); Task<DivReply> resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build());
DivReply result = call.Result; DivReply result = await resultTask;
Console.WriteLine(result); Console.WriteLine("DivAsync Result: " + result);
} }
public static void DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub) public static async Task DivAsyncWithCancellationExample(MathGrpc.IMathServiceClient stub)
{ {
Task<DivReply> call = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); Task<DivReply> resultTask = stub.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build());
DivReply result = call.Result; DivReply result = await resultTask;
Console.WriteLine(result); Console.WriteLine(result);
} }
public static void FibExample(MathGrpc.IMathServiceClient stub) public static async Task FibExample(MathGrpc.IMathServiceClient stub)
{ {
var recorder = new RecordingObserver<Num>(); var recorder = new RecordingObserver<Num>();
stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder); stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder);
List<Num> result = await recorder.ToList();
List<Num> numbers = recorder.ToList().Result; Console.WriteLine("Fib Result: " + string.Join("|", result));
Console.WriteLine("Fib Result: " + string.Join("|", recorder.ToList().Result));
} }
public static void SumExample(MathGrpc.IMathServiceClient stub) public static async Task SumExample(MathGrpc.IMathServiceClient stub)
{ {
List<Num> numbers = new List<Num> var numbers = new List<Num>
{ {
new Num.Builder { Num_ = 1 }.Build(), new Num.Builder { Num_ = 1 }.Build(),
new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 2 }.Build(),
new Num.Builder { Num_ = 3 }.Build() new Num.Builder { Num_ = 3 }.Build()
}; };
var res = stub.Sum(); var clientStreamingResult = stub.Sum();
foreach (var num in numbers) numbers.Subscribe(clientStreamingResult.Inputs);
{ Console.WriteLine("Sum Result: " + await clientStreamingResult.Task);
res.Inputs.OnNext(num);
}
res.Inputs.OnCompleted();
Console.WriteLine("Sum Result: " + res.Task.Result);
} }
public static void DivManyExample(MathGrpc.IMathServiceClient stub) public static async Task DivManyExample(MathGrpc.IMathServiceClient stub)
{ {
List<DivArgs> divArgsList = new List<DivArgs> var divArgsList = new List<DivArgs>
{ {
new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
@ -97,26 +91,27 @@ namespace math
}; };
var recorder = new RecordingObserver<DivReply>(); var recorder = new RecordingObserver<DivReply>();
var inputs = stub.DivMany(recorder); var inputs = stub.DivMany(recorder);
foreach (var input in divArgsList) divArgsList.Subscribe(inputs);
{ var result = await recorder.ToList();
inputs.OnNext(input); Console.WriteLine("DivMany Result: " + string.Join("|", result));
}
inputs.OnCompleted();
Console.WriteLine("DivMany Result: " + string.Join("|", recorder.ToList().Result));
} }
public static void DependendRequestsExample(MathGrpc.IMathServiceClient stub) public static async Task DependendRequestsExample(MathGrpc.IMathServiceClient stub)
{ {
var numberList = new List<Num> var numbers = new List<Num>
{ {
new Num.Builder { Num_ = 1 }.Build(), new Num.Builder { Num_ = 1 }.Build(),
new Num.Builder { Num_ = 2 }.Build(), new Num.Builder { Num_ = 3 }.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);
} }
} }
} }

Loading…
Cancel
Save