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 | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 36 | using System.Linq; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 37 | using System.Threading; |
| 38 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core; |
| 40 | using Grpc.Core.Utils; |
| 41 | using NUnit.Framework; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 2852631 | 2015-08-03 09:21:38 -0700 | [diff] [blame] | 43 | namespace Math.Tests |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 44 | { |
| 45 | /// <summary> |
| 46 | /// Math client talks to local math server. |
| 47 | /// </summary> |
| 48 | public class MathClientServerTest |
| 49 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 50 | const string Host = "localhost"; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 51 | Server server; |
| 52 | Channel channel; |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 53 | Math.MathClient client; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 54 | |
| 55 | [TestFixtureSetUp] |
| 56 | public void Init() |
| 57 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 58 | server = new Server |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 59 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 60 | Services = { Math.BindService(new MathServiceImpl()) }, |
| 61 | Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 62 | }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 63 | server.Start(); |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 64 | channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure); |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 65 | client = new Math.MathClient(channel); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 68 | [TestFixtureTearDown] |
| 69 | public void Cleanup() |
| 70 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 71 | channel.ShutdownAsync().Wait(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 72 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 75 | [Test] |
| 76 | public void Div1() |
| 77 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 78 | DivReply response = client.Div(new DivArgs { Dividend = 10, Divisor = 3 }); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 79 | Assert.AreEqual(3, response.Quotient); |
| 80 | Assert.AreEqual(1, response.Remainder); |
| 81 | } |
| 82 | |
| 83 | [Test] |
| 84 | public void Div2() |
| 85 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 86 | DivReply response = client.Div(new DivArgs { Dividend = 0, Divisor = 1 }); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 87 | Assert.AreEqual(0, response.Quotient); |
| 88 | Assert.AreEqual(0, response.Remainder); |
| 89 | } |
| 90 | |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 91 | [Test] |
| 92 | public void DivByZero() |
| 93 | { |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 94 | var ex = Assert.Throws<RpcException>(() => client.Div(new DivArgs { Dividend = 0, Divisor = 0 })); |
Jan Tattermusch | d6a8397 | 2016-05-10 16:38:23 -0700 | [diff] [blame] | 95 | Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode); |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 96 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 97 | |
| 98 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 99 | public async Task DivAsync() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 100 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 101 | DivReply response = await client.DivAsync(new DivArgs { Dividend = 10, Divisor = 3 }); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 102 | Assert.AreEqual(3, response.Quotient); |
| 103 | Assert.AreEqual(1, response.Remainder); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 107 | public async Task Fib() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 108 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 109 | using (var call = client.Fib(new FibArgs { Limit = 6 })) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 110 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 111 | var responses = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 112 | CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
Jan Tattermusch | ec4359d | 2016-04-11 20:55:44 -0700 | [diff] [blame] | 113 | responses.Select((n) => n.Num_)); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 114 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 117 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 118 | public async Task FibWithCancel() |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 119 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 120 | var cts = new CancellationTokenSource(); |
| 121 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 122 | using (var call = client.Fib(new FibArgs { Limit = 0 }, cancellationToken: cts.Token)) |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 123 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 124 | List<long> responses = new List<long>(); |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 125 | |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 126 | try |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 127 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 128 | while (await call.ResponseStream.MoveNext()) |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 129 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 130 | if (responses.Count == 0) |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 131 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 132 | cts.CancelAfter(500); // make sure we cancel soon |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 133 | } |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 134 | responses.Add(call.ResponseStream.Current.Num_); |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 135 | } |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 136 | Assert.Fail(); |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 137 | } |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 138 | catch (RpcException e) |
| 139 | { |
| 140 | Assert.IsTrue(responses.Count > 0); |
| 141 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 142 | } |
| 143 | } |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jan Tattermusch | 4106259 | 2015-07-23 17:30:24 -0700 | [diff] [blame] | 146 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 147 | public async Task FibWithDeadline() |
Jan Tattermusch | 4106259 | 2015-07-23 17:30:24 -0700 | [diff] [blame] | 148 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 149 | using (var call = client.Fib(new FibArgs { Limit = 0 }, |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 150 | deadline: DateTime.UtcNow.AddMilliseconds(500))) |
Jan Tattermusch | 4106259 | 2015-07-23 17:30:24 -0700 | [diff] [blame] | 151 | { |
Jan Tattermusch | 6c0dee8 | 2016-04-08 12:14:09 -0700 | [diff] [blame] | 152 | var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.ToListAsync()); |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 153 | |
| 154 | // We can't guarantee the status code always DeadlineExceeded. See issue #2685. |
| 155 | Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal }); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 156 | } |
Jan Tattermusch | 4106259 | 2015-07-23 17:30:24 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 159 | // TODO: test Fib with limit=0 and cancellation |
| 160 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 161 | public async Task Sum() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 162 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 163 | using (var call = client.Sum()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 164 | { |
Jan Tattermusch | ec4359d | 2016-04-11 20:55:44 -0700 | [diff] [blame] | 165 | var numbers = new List<long> { 10, 20, 30 }.Select(n => new Num { Num_ = n }); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 166 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 167 | await call.RequestStream.WriteAllAsync(numbers); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 168 | var result = await call.ResponseAsync; |
| 169 | Assert.AreEqual(60, result.Num_); |
| 170 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | [Test] |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 174 | public async Task DivMany() |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 175 | { |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 176 | var divArgsList = new List<DivArgs> |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 177 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 178 | new DivArgs { Dividend = 10, Divisor = 3 }, |
| 179 | new DivArgs { Dividend = 100, Divisor = 21 }, |
| 180 | new DivArgs { Dividend = 7, Divisor = 2 } |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 181 | }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 182 | |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 183 | using (var call = client.DivMany()) |
| 184 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 185 | await call.RequestStream.WriteAllAsync(divArgsList); |
| 186 | var result = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 187 | |
Jan Tattermusch | ec4359d | 2016-04-11 20:55:44 -0700 | [diff] [blame] | 188 | CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.Select((divReply) => divReply.Quotient)); |
| 189 | CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.Select((divReply) => divReply.Remainder)); |
Jan Tattermusch | 9b048e5 | 2015-07-24 21:20:24 -0700 | [diff] [blame] | 190 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 191 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 192 | } |
| 193 | } |