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; |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 52 | Math.MathClient client; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 53 | |
| 54 | [TestFixtureSetUp] |
| 55 | public void Init() |
| 56 | { |
| 57 | server = new Server(); |
Jan Tattermusch | 085533e | 2015-05-07 14:34:45 -0700 | [diff] [blame] | 58 | server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 59 | int port = server.AddListeningPort(host, Server.PickUnusedPort); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 60 | server.Start(); |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 61 | channel = new Channel(host, port); |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 62 | client = Math.NewClient(channel); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 63 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 64 | // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 65 | // for header support. |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 66 | client.HeaderInterceptor = (metadata) => |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 67 | { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 68 | metadata.Add(new Metadata.Entry("customHeader", "abcdef")); |
| 69 | }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 72 | [TestFixtureTearDown] |
| 73 | public void Cleanup() |
| 74 | { |
| 75 | channel.Dispose(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 76 | server.ShutdownAsync().Wait(); |
| 77 | GrpcEnvironment.Shutdown(); |
| 78 | } |
| 79 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 80 | [Test] |
| 81 | public void Div1() |
| 82 | { |
| 83 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
| 84 | Assert.AreEqual(3, response.Quotient); |
| 85 | Assert.AreEqual(1, response.Remainder); |
| 86 | } |
| 87 | |
| 88 | [Test] |
| 89 | public void Div2() |
| 90 | { |
| 91 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 1 }.Build()); |
| 92 | Assert.AreEqual(0, response.Quotient); |
| 93 | Assert.AreEqual(0, response.Remainder); |
| 94 | } |
| 95 | |
Jan Tattermusch | 2d2652d | 2015-05-18 16:23:04 -0700 | [diff] [blame] | 96 | [Test] |
| 97 | public void DivByZero() |
| 98 | { |
| 99 | try |
| 100 | { |
| 101 | DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 0 }.Build()); |
| 102 | Assert.Fail(); |
| 103 | } |
| 104 | catch (RpcException e) |
| 105 | { |
| 106 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
| 107 | } |
| 108 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 109 | |
| 110 | [Test] |
| 111 | public void DivAsync() |
| 112 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 113 | Task.Run(async () => |
| 114 | { |
| 115 | DivReply response = await client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
| 116 | Assert.AreEqual(3, response.Quotient); |
| 117 | Assert.AreEqual(1, response.Remainder); |
| 118 | }).Wait(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | [Test] |
| 122 | public void Fib() |
| 123 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 124 | Task.Run(async () => |
| 125 | { |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 126 | using (var call = client.Fib(new FibArgs.Builder { Limit = 6 }.Build())) |
| 127 | { |
| 128 | var responses = await call.ResponseStream.ToList(); |
| 129 | CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
| 130 | responses.ConvertAll((n) => n.Num_)); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 131 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 132 | }).Wait(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jan Tattermusch | f7cfc8a | 2015-07-23 17:26:35 -0700 | [diff] [blame] | 135 | [Test] |
| 136 | public void FibWithCancel() |
| 137 | { |
| 138 | Task.Run(async () => |
| 139 | { |
| 140 | var cts = new CancellationTokenSource(); |
| 141 | |
| 142 | using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
| 143 | cancellationToken: cts.Token)) |
| 144 | { |
| 145 | List<long> responses = new List<long>(); |
| 146 | |
| 147 | try |
| 148 | { |
| 149 | while (await call.ResponseStream.MoveNext()) |
| 150 | { |
| 151 | if (responses.Count == 0) |
| 152 | { |
| 153 | cts.CancelAfter(500); // make sure we cancel soon |
| 154 | } |
| 155 | responses.Add(call.ResponseStream.Current.Num_); |
| 156 | } |
| 157 | Assert.Fail(); |
| 158 | } |
| 159 | catch (RpcException e) |
| 160 | { |
| 161 | Assert.IsTrue(responses.Count > 0); |
| 162 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 163 | } |
| 164 | } |
| 165 | }).Wait(); |
| 166 | } |
| 167 | |
Jan Tattermusch | 4106259 | 2015-07-23 17:30:24 -0700 | [diff] [blame] | 168 | [Test] |
| 169 | public void FibWithDeadline() |
| 170 | { |
| 171 | Task.Run(async () => |
| 172 | { |
| 173 | using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
| 174 | deadline: DateTime.UtcNow.AddMilliseconds(500))) |
| 175 | { |
| 176 | try |
| 177 | { |
| 178 | await call.ResponseStream.ToList(); |
| 179 | Assert.Fail(); |
| 180 | } |
| 181 | catch (RpcException e) |
| 182 | { |
| 183 | Assert.AreEqual(StatusCode.DeadlineExceeded, e.Status.StatusCode); |
| 184 | } |
| 185 | } |
| 186 | }).Wait(); |
| 187 | } |
| 188 | |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 189 | // TODO: test Fib with limit=0 and cancellation |
| 190 | [Test] |
| 191 | public void Sum() |
| 192 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 193 | Task.Run(async () => |
| 194 | { |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 195 | using (var call = client.Sum()) |
| 196 | { |
| 197 | var numbers = new List<long> { 10, 20, 30 }.ConvertAll( |
| 198 | n => Num.CreateBuilder().SetNum_(n).Build()); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 199 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 200 | await call.RequestStream.WriteAll(numbers); |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 201 | var result = await call.ResponseAsync; |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 202 | Assert.AreEqual(60, result.Num_); |
| 203 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 204 | }).Wait(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | [Test] |
| 208 | public void DivMany() |
| 209 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 210 | Task.Run(async () => |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 211 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 212 | var divArgsList = new List<DivArgs> |
| 213 | { |
| 214 | new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
| 215 | new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
| 216 | new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
| 217 | }; |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 218 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 219 | using (var call = client.DivMany()) |
| 220 | { |
| 221 | await call.RequestStream.WriteAll(divArgsList); |
| 222 | var result = await call.ResponseStream.ToList(); |
| 223 | |
| 224 | CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); |
| 225 | CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder)); |
| 226 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 227 | }).Wait(); |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 228 | } |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 229 | } |
| 230 | } |