blob: 50dacc2eaaf258ad28a01518e1afb59653f4e49c [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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 Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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 Tattermusch15111f52015-02-05 18:15:14 -080034using System;
Jan Tattermusch30868622015-02-19 09:22:33 -080035using System.Collections.Generic;
Jan Tattermusch31ba0632015-08-04 22:02:55 -070036using System.Linq;
Jan Tattermusch15111f52015-02-05 18:15:14 -080037using System.Threading;
38using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080039using Grpc.Core;
40using Grpc.Core.Utils;
41using NUnit.Framework;
Jan Tattermusch15111f52015-02-05 18:15:14 -080042
Jan Tattermusch28526312015-08-03 09:21:38 -070043namespace Math.Tests
Jan Tattermusch15111f52015-02-05 18:15:14 -080044{
45 /// <summary>
46 /// Math client talks to local math server.
47 /// </summary>
48 public class MathClientServerTest
49 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070050 const string Host = "localhost";
Jan Tattermusch15111f52015-02-05 18:15:14 -080051 Server server;
52 Channel channel;
Jan Tattermuschb5332812015-07-14 19:29:35 -070053 Math.MathClient client;
Jan Tattermusch15111f52015-02-05 18:15:14 -080054
55 [TestFixtureSetUp]
56 public void Init()
57 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070058 server = new Server
Jan Tattermusch021df8a2015-08-04 20:31:11 -070059 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070060 Services = { Math.BindService(new MathServiceImpl()) },
61 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
Jan Tattermusch021df8a2015-08-04 20:31:11 -070062 };
Jan Tattermusch15111f52015-02-05 18:15:14 -080063 server.Start();
Jan Tattermusch5bd70052015-10-06 16:47:49 -070064 channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
Jan Tattermuschf41ebc32016-06-22 12:47:14 -070065 client = new Math.MathClient(channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -080066 }
67
Jan Tattermusch607307d2015-02-18 11:05:45 -080068 [TestFixtureTearDown]
69 public void Cleanup()
70 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070071 channel.ShutdownAsync().Wait();
Jan Tattermusch607307d2015-02-18 11:05:45 -080072 server.ShutdownAsync().Wait();
Jan Tattermusch607307d2015-02-18 11:05:45 -080073 }
74
Jan Tattermusch15111f52015-02-05 18:15:14 -080075 [Test]
76 public void Div1()
77 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070078 DivReply response = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch15111f52015-02-05 18:15:14 -080079 Assert.AreEqual(3, response.Quotient);
80 Assert.AreEqual(1, response.Remainder);
81 }
82
83 [Test]
84 public void Div2()
85 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070086 DivReply response = client.Div(new DivArgs { Dividend = 0, Divisor = 1 });
Jan Tattermusch15111f52015-02-05 18:15:14 -080087 Assert.AreEqual(0, response.Quotient);
88 Assert.AreEqual(0, response.Remainder);
89 }
90
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070091 [Test]
92 public void DivByZero()
93 {
Jan Tattermusch46e85b02015-08-13 10:31:05 -070094 var ex = Assert.Throws<RpcException>(() => client.Div(new DivArgs { Dividend = 0, Divisor = 0 }));
Jan Tattermuschd6a83972016-05-10 16:38:23 -070095 Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode);
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070096 }
Jan Tattermusch15111f52015-02-05 18:15:14 -080097
98 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -070099 public async Task DivAsync()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800100 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700101 DivReply response = await client.DivAsync(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700102 Assert.AreEqual(3, response.Quotient);
103 Assert.AreEqual(1, response.Remainder);
Jan Tattermusch15111f52015-02-05 18:15:14 -0800104 }
105
106 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700107 public async Task Fib()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800108 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700109 using (var call = client.Fib(new FibArgs { Limit = 6 }))
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700110 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700111 var responses = await call.ResponseStream.ToListAsync();
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700112 CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 },
Jan Tattermuschec4359d2016-04-11 20:55:44 -0700113 responses.Select((n) => n.Num_));
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700114 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800115 }
116
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700117 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700118 public async Task FibWithCancel()
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700119 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700120 var cts = new CancellationTokenSource();
121
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700122 using (var call = client.Fib(new FibArgs { Limit = 0 }, cancellationToken: cts.Token))
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700123 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700124 List<long> responses = new List<long>();
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700125
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700126 try
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700127 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700128 while (await call.ResponseStream.MoveNext())
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700129 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700130 if (responses.Count == 0)
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700131 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700132 cts.CancelAfter(500); // make sure we cancel soon
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700133 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700134 responses.Add(call.ResponseStream.Current.Num_);
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700135 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700136 Assert.Fail();
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700137 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700138 catch (RpcException e)
139 {
140 Assert.IsTrue(responses.Count > 0);
141 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
142 }
143 }
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700144 }
145
Jan Tattermusch41062592015-07-23 17:30:24 -0700146 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700147 public async Task FibWithDeadline()
Jan Tattermusch41062592015-07-23 17:30:24 -0700148 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700149 using (var call = client.Fib(new FibArgs { Limit = 0 },
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700150 deadline: DateTime.UtcNow.AddMilliseconds(500)))
Jan Tattermusch41062592015-07-23 17:30:24 -0700151 {
Jan Tattermusch6c0dee82016-04-08 12:14:09 -0700152 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.ToListAsync());
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700153
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 Tattermusch9b048e52015-07-24 21:20:24 -0700156 }
Jan Tattermusch41062592015-07-23 17:30:24 -0700157 }
158
Jan Tattermusch15111f52015-02-05 18:15:14 -0800159 // TODO: test Fib with limit=0 and cancellation
160 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700161 public async Task Sum()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800162 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700163 using (var call = client.Sum())
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700164 {
Jan Tattermuschec4359d2016-04-11 20:55:44 -0700165 var numbers = new List<long> { 10, 20, 30 }.Select(n => new Num { Num_ = n });
Jan Tattermusch15111f52015-02-05 18:15:14 -0800166
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700167 await call.RequestStream.WriteAllAsync(numbers);
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700168 var result = await call.ResponseAsync;
169 Assert.AreEqual(60, result.Num_);
170 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800171 }
172
173 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700174 public async Task DivMany()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800175 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700176 var divArgsList = new List<DivArgs>
Jan Tattermusch075dde42015-03-11 18:21:00 -0700177 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700178 new DivArgs { Dividend = 10, Divisor = 3 },
179 new DivArgs { Dividend = 100, Divisor = 21 },
180 new DivArgs { Dividend = 7, Divisor = 2 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700181 };
Jan Tattermusch15111f52015-02-05 18:15:14 -0800182
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700183 using (var call = client.DivMany())
184 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700185 await call.RequestStream.WriteAllAsync(divArgsList);
186 var result = await call.ResponseStream.ToListAsync();
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700187
Jan Tattermuschec4359d2016-04-11 20:55:44 -0700188 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 Tattermusch9b048e52015-07-24 21:20:24 -0700190 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800191 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800192 }
193}