Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | #endregion |
| 33 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 36 | using System.Threading; |
| 37 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 38 | using Grpc.Core; |
| 39 | using Grpc.Core.Utils; |
| 40 | using NUnit.Framework; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 41 | |
| 42 | namespace math.Tests |
| 43 | { |
| 44 | /// <summary> |
| 45 | /// Math client talks to local math server. |
| 46 | /// </summary> |
| 47 | public class MathClientServerTest |
| 48 | { |
Jan Tattermusch | 07fadea | 2015-02-13 10:03:37 -0800 | [diff] [blame] | 49 | string host = "localhost"; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 50 | Server server; |
| 51 | Channel channel; |
| 52 | MathGrpc.IMathServiceClient client; |
| 53 | |
| 54 | [TestFixtureSetUp] |
| 55 | public void Init() |
| 56 | { |
Jan Tattermusch | 23821ce | 2015-02-13 10:46:02 -0800 | [diff] [blame] | 57 | GrpcEnvironment.Initialize(); |
| 58 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 59 | server = new Server(); |
| 60 | server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); |
Jan Tattermusch | 07fadea | 2015-02-13 10:03:37 -0800 | [diff] [blame] | 61 | int port = server.AddPort(host + ":0"); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 62 | server.Start(); |
Jan Tattermusch | 07fadea | 2015-02-13 10:03:37 -0800 | [diff] [blame] | 63 | channel = new Channel(host + ":" + port); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 64 | client = MathGrpc.NewStub(channel); |
| 65 | } |
| 66 | |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 67 | [TestFixtureTearDown] |
| 68 | public void Cleanup() |
| 69 | { |
| 70 | channel.Dispose(); |
| 71 | |
| 72 | server.ShutdownAsync().Wait(); |
| 73 | GrpcEnvironment.Shutdown(); |
| 74 | } |
| 75 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 76 | [Test] |
| 77 | public void Div1() |
| 78 | { |
| 79 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
| 80 | Assert.AreEqual(3, response.Quotient); |
| 81 | Assert.AreEqual(1, response.Remainder); |
| 82 | } |
| 83 | |
| 84 | [Test] |
| 85 | public void Div2() |
| 86 | { |
| 87 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 1 }.Build()); |
| 88 | Assert.AreEqual(0, response.Quotient); |
| 89 | Assert.AreEqual(0, response.Remainder); |
| 90 | } |
| 91 | |
| 92 | // TODO: test division by zero |
| 93 | |
| 94 | [Test] |
| 95 | public void DivAsync() |
| 96 | { |
| 97 | DivReply response = client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()).Result; |
| 98 | Assert.AreEqual(3, response.Quotient); |
| 99 | Assert.AreEqual(1, response.Remainder); |
| 100 | } |
| 101 | |
| 102 | [Test] |
| 103 | public void Fib() |
| 104 | { |
| 105 | var recorder = new RecordingObserver<Num>(); |
| 106 | client.Fib(new FibArgs.Builder { Limit = 6 }.Build(), recorder); |
| 107 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 108 | CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 109 | recorder.ToList().Result.ConvertAll((n) => n.Num_)); |
| 110 | } |
| 111 | |
| 112 | // TODO: test Fib with limit=0 and cancellation |
| 113 | [Test] |
| 114 | public void Sum() |
| 115 | { |
| 116 | var res = client.Sum(); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 117 | foreach (var num in new long[] { 10, 20, 30 }) |
| 118 | { |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 119 | res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build()); |
| 120 | } |
| 121 | res.Inputs.OnCompleted(); |
| 122 | |
| 123 | Assert.AreEqual(60, res.Task.Result.Num_); |
| 124 | } |
| 125 | |
| 126 | [Test] |
| 127 | public void DivMany() |
| 128 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 129 | List<DivArgs> divArgsList = new List<DivArgs> |
| 130 | { |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 131 | new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
| 132 | new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
| 133 | new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
| 134 | }; |
| 135 | |
| 136 | var recorder = new RecordingObserver<DivReply>(); |
| 137 | var requestObserver = client.DivMany(recorder); |
| 138 | |
| 139 | foreach (var arg in divArgsList) |
| 140 | { |
| 141 | requestObserver.OnNext(arg); |
| 142 | } |
| 143 | requestObserver.OnCompleted(); |
| 144 | |
| 145 | var result = recorder.ToList().Result; |
| 146 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 147 | CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); |
| 148 | CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder)); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 149 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 150 | } |
| 151 | } |