Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 1 | using System; |
| 2 | using NUnit.Framework; |
| 3 | using Google.GRPC.Core; |
| 4 | using System.Threading; |
| 5 | using System.Threading.Tasks; |
| 6 | using Google.GRPC.Core.Utils; |
| 7 | using System.Collections.Generic; |
| 8 | |
| 9 | namespace math.Tests |
| 10 | { |
| 11 | /// <summary> |
| 12 | /// Math client talks to local math server. |
| 13 | /// </summary> |
| 14 | public class MathClientServerTest |
| 15 | { |
| 16 | string serverAddr = "localhost:" + PortPicker.PickUnusedPort(); |
| 17 | Server server; |
| 18 | Channel channel; |
| 19 | MathGrpc.IMathServiceClient client; |
| 20 | |
| 21 | [TestFixtureSetUp] |
| 22 | public void Init() |
| 23 | { |
| 24 | server = new Server(); |
| 25 | server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); |
| 26 | server.AddPort(serverAddr); |
| 27 | server.Start(); |
| 28 | channel = new Channel(serverAddr); |
| 29 | client = MathGrpc.NewStub(channel); |
| 30 | } |
| 31 | |
| 32 | [Test] |
| 33 | public void Div1() |
| 34 | { |
| 35 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
| 36 | Assert.AreEqual(3, response.Quotient); |
| 37 | Assert.AreEqual(1, response.Remainder); |
| 38 | } |
| 39 | |
| 40 | [Test] |
| 41 | public void Div2() |
| 42 | { |
| 43 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 1 }.Build()); |
| 44 | Assert.AreEqual(0, response.Quotient); |
| 45 | Assert.AreEqual(0, response.Remainder); |
| 46 | } |
| 47 | |
| 48 | // TODO: test division by zero |
| 49 | |
| 50 | [Test] |
| 51 | public void DivAsync() |
| 52 | { |
| 53 | DivReply response = client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()).Result; |
| 54 | Assert.AreEqual(3, response.Quotient); |
| 55 | Assert.AreEqual(1, response.Remainder); |
| 56 | } |
| 57 | |
| 58 | [Test] |
| 59 | public void Fib() |
| 60 | { |
| 61 | var recorder = new RecordingObserver<Num>(); |
| 62 | client.Fib(new FibArgs.Builder { Limit = 6 }.Build(), recorder); |
| 63 | |
| 64 | CollectionAssert.AreEqual(new List<long>{1, 1, 2, 3, 5, 8}, |
| 65 | recorder.ToList().Result.ConvertAll((n) => n.Num_)); |
| 66 | } |
| 67 | |
| 68 | // TODO: test Fib with limit=0 and cancellation |
| 69 | [Test] |
| 70 | public void Sum() |
| 71 | { |
| 72 | var res = client.Sum(); |
| 73 | foreach (var num in new long[] { 10, 20, 30 }) { |
| 74 | res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build()); |
| 75 | } |
| 76 | res.Inputs.OnCompleted(); |
| 77 | |
| 78 | Assert.AreEqual(60, res.Task.Result.Num_); |
| 79 | } |
| 80 | |
| 81 | [Test] |
| 82 | public void DivMany() |
| 83 | { |
| 84 | List<DivArgs> divArgsList = new List<DivArgs>{ |
| 85 | new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
| 86 | new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
| 87 | new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
| 88 | }; |
| 89 | |
| 90 | var recorder = new RecordingObserver<DivReply>(); |
| 91 | var requestObserver = client.DivMany(recorder); |
| 92 | |
| 93 | foreach (var arg in divArgsList) |
| 94 | { |
| 95 | requestObserver.OnNext(arg); |
| 96 | } |
| 97 | requestObserver.OnCompleted(); |
| 98 | |
| 99 | var result = recorder.ToList().Result; |
| 100 | |
| 101 | CollectionAssert.AreEqual(new long[] {3, 4, 3}, result.ConvertAll((divReply) => divReply.Quotient)); |
| 102 | CollectionAssert.AreEqual(new long[] {1, 16, 1}, result.ConvertAll((divReply) => divReply.Remainder)); |
| 103 | } |
| 104 | |
| 105 | [TestFixtureTearDown] |
| 106 | public void Cleanup() |
| 107 | { |
| 108 | channel.Dispose(); |
| 109 | |
| 110 | server.ShutdownAsync().Wait(); |
| 111 | GrpcEnvironment.Shutdown(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |