blob: ded80ffba5fe3b6410abccb685dbd5412934a0ed [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermusch15111f52015-02-05 18:15:14 -080019using System;
Jan Tattermusch30868622015-02-19 09:22:33 -080020using System.Collections.Generic;
Jan Tattermusch31ba0632015-08-04 22:02:55 -070021using System.Linq;
Jan Tattermusch15111f52015-02-05 18:15:14 -080022using System.Threading;
23using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080024using Grpc.Core;
25using Grpc.Core.Utils;
26using NUnit.Framework;
Jan Tattermusch15111f52015-02-05 18:15:14 -080027
Jan Tattermusch28526312015-08-03 09:21:38 -070028namespace Math.Tests
Jan Tattermusch15111f52015-02-05 18:15:14 -080029{
30 /// <summary>
31 /// Math client talks to local math server.
32 /// </summary>
33 public class MathClientServerTest
34 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070035 const string Host = "localhost";
Jan Tattermusch15111f52015-02-05 18:15:14 -080036 Server server;
37 Channel channel;
Jan Tattermuschb5332812015-07-14 19:29:35 -070038 Math.MathClient client;
Jan Tattermusch15111f52015-02-05 18:15:14 -080039
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020040 [OneTimeSetUp]
Jan Tattermusch15111f52015-02-05 18:15:14 -080041 public void Init()
42 {
Jan Tattermusch09d2f552017-04-20 11:39:37 +020043 // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
44 server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
Jan Tattermusch021df8a2015-08-04 20:31:11 -070045 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070046 Services = { Math.BindService(new MathServiceImpl()) },
47 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
Jan Tattermusch021df8a2015-08-04 20:31:11 -070048 };
Jan Tattermusch15111f52015-02-05 18:15:14 -080049 server.Start();
Jan Tattermusch5bd70052015-10-06 16:47:49 -070050 channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
Jan Tattermuschf41ebc32016-06-22 12:47:14 -070051 client = new Math.MathClient(channel);
Jan Tattermusch15111f52015-02-05 18:15:14 -080052 }
53
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020054 [OneTimeTearDown]
Jan Tattermusch607307d2015-02-18 11:05:45 -080055 public void Cleanup()
56 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070057 channel.ShutdownAsync().Wait();
Jan Tattermusch607307d2015-02-18 11:05:45 -080058 server.ShutdownAsync().Wait();
Jan Tattermusch607307d2015-02-18 11:05:45 -080059 }
60
Jan Tattermusch15111f52015-02-05 18:15:14 -080061 [Test]
62 public void Div1()
63 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070064 DivReply response = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch15111f52015-02-05 18:15:14 -080065 Assert.AreEqual(3, response.Quotient);
66 Assert.AreEqual(1, response.Remainder);
67 }
68
69 [Test]
70 public void Div2()
71 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070072 DivReply response = client.Div(new DivArgs { Dividend = 0, Divisor = 1 });
Jan Tattermusch15111f52015-02-05 18:15:14 -080073 Assert.AreEqual(0, response.Quotient);
74 Assert.AreEqual(0, response.Remainder);
75 }
76
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070077 [Test]
78 public void DivByZero()
79 {
Jan Tattermusch46e85b02015-08-13 10:31:05 -070080 var ex = Assert.Throws<RpcException>(() => client.Div(new DivArgs { Dividend = 0, Divisor = 0 }));
Jan Tattermuschd6a83972016-05-10 16:38:23 -070081 Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode);
Jan Tattermusch2d2652d2015-05-18 16:23:04 -070082 }
Jan Tattermusch15111f52015-02-05 18:15:14 -080083
84 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -070085 public async Task DivAsync()
Jan Tattermusch15111f52015-02-05 18:15:14 -080086 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070087 DivReply response = await client.DivAsync(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch9b048e52015-07-24 21:20:24 -070088 Assert.AreEqual(3, response.Quotient);
89 Assert.AreEqual(1, response.Remainder);
Jan Tattermusch15111f52015-02-05 18:15:14 -080090 }
91
92 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -070093 public async Task Fib()
Jan Tattermusch15111f52015-02-05 18:15:14 -080094 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070095 using (var call = client.Fib(new FibArgs { Limit = 6 }))
Jan Tattermuscha5272b62015-04-30 11:56:46 -070096 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070097 var responses = await call.ResponseStream.ToListAsync();
Jan Tattermusch9b048e52015-07-24 21:20:24 -070098 CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 },
Jan Tattermuschec4359d2016-04-11 20:55:44 -070099 responses.Select((n) => n.Num_));
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700100 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800101 }
102
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700103 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700104 public async Task FibWithCancel()
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700105 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700106 var cts = new CancellationTokenSource();
107
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700108 using (var call = client.Fib(new FibArgs { Limit = 0 }, cancellationToken: cts.Token))
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700109 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700110 List<long> responses = new List<long>();
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700111
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700112 try
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700113 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700114 while (await call.ResponseStream.MoveNext())
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700115 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700116 if (responses.Count == 0)
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700117 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700118 cts.CancelAfter(500); // make sure we cancel soon
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700119 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700120 responses.Add(call.ResponseStream.Current.Num_);
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700121 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700122 Assert.Fail();
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700123 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700124 catch (RpcException e)
125 {
126 Assert.IsTrue(responses.Count > 0);
127 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
128 }
129 }
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700130 }
131
Jan Tattermusch41062592015-07-23 17:30:24 -0700132 [Test]
Jan Tattermuschb8c77c52017-08-09 09:05:54 +0200133 public void FibWithDeadline()
Jan Tattermusch41062592015-07-23 17:30:24 -0700134 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700135 using (var call = client.Fib(new FibArgs { Limit = 0 },
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700136 deadline: DateTime.UtcNow.AddMilliseconds(500)))
Jan Tattermusch41062592015-07-23 17:30:24 -0700137 {
Jan Tattermusch6c0dee82016-04-08 12:14:09 -0700138 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.ToListAsync());
Jan Tattermuschc8d7b842015-08-07 20:52:21 -0700139
140 // We can't guarantee the status code always DeadlineExceeded. See issue #2685.
141 Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal });
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700142 }
Jan Tattermusch41062592015-07-23 17:30:24 -0700143 }
144
Jan Tattermusch15111f52015-02-05 18:15:14 -0800145 // TODO: test Fib with limit=0 and cancellation
146 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700147 public async Task Sum()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800148 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700149 using (var call = client.Sum())
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700150 {
Jan Tattermuschec4359d2016-04-11 20:55:44 -0700151 var numbers = new List<long> { 10, 20, 30 }.Select(n => new Num { Num_ = n });
Jan Tattermusch15111f52015-02-05 18:15:14 -0800152
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700153 await call.RequestStream.WriteAllAsync(numbers);
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700154 var result = await call.ResponseAsync;
155 Assert.AreEqual(60, result.Num_);
156 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800157 }
158
159 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700160 public async Task DivMany()
Jan Tattermusch15111f52015-02-05 18:15:14 -0800161 {
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700162 var divArgsList = new List<DivArgs>
Jan Tattermusch075dde42015-03-11 18:21:00 -0700163 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -0700164 new DivArgs { Dividend = 10, Divisor = 3 },
165 new DivArgs { Dividend = 100, Divisor = 21 },
166 new DivArgs { Dividend = 7, Divisor = 2 }
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700167 };
Jan Tattermusch15111f52015-02-05 18:15:14 -0800168
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700169 using (var call = client.DivMany())
170 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700171 await call.RequestStream.WriteAllAsync(divArgsList);
172 var result = await call.ResponseStream.ToListAsync();
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700173
Jan Tattermuschec4359d2016-04-11 20:55:44 -0700174 CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.Select((divReply) => divReply.Quotient));
175 CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.Select((divReply) => divReply.Remainder));
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700176 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800177 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800178 }
179}