blob: ecd9d514fd9031993d3ab1b21a9ca6e6577e87e5 [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 Tattermusch15111f52015-02-05 18:15:14 -080036using System.Threading;
37using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080038using Grpc.Core;
39using Grpc.Core.Utils;
40using NUnit.Framework;
Jan Tattermusch15111f52015-02-05 18:15:14 -080041
42namespace math.Tests
43{
44 /// <summary>
45 /// Math client talks to local math server.
46 /// </summary>
47 public class MathClientServerTest
48 {
Jan Tattermusch07fadea2015-02-13 10:03:37 -080049 string host = "localhost";
Jan Tattermusch15111f52015-02-05 18:15:14 -080050 Server server;
51 Channel channel;
Jan Tattermuschb5332812015-07-14 19:29:35 -070052 Math.MathClient client;
Jan Tattermusch15111f52015-02-05 18:15:14 -080053
54 [TestFixtureSetUp]
55 public void Init()
56 {
57 server = new Server();
Jan Tattermusch085533e2015-05-07 14:34:45 -070058 server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
Jan Tattermuscha96ac052015-07-24 14:49:30 -070059 int port = server.AddPort(host, Server.PickUnusedPort, ServerCredentials.Insecure);
Jan Tattermusch15111f52015-02-05 18:15:14 -080060 server.Start();
Jan Tattermuscha96ac052015-07-24 14:49:30 -070061 channel = new Channel(host, port, Credentials.Insecure);
Jan Tattermuschb5332812015-07-14 19:29:35 -070062 client = Math.NewClient(channel);
Jan Tattermuschc0b37212015-03-13 08:35:41 -070063
Jan Tattermuscha5272b62015-04-30 11:56:46 -070064 // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests
Jan Tattermuschc0b37212015-03-13 08:35:41 -070065 // for header support.
Jan Tattermuschb5332812015-07-14 19:29:35 -070066 client.HeaderInterceptor = (metadata) =>
Jan Tattermuschc0b37212015-03-13 08:35:41 -070067 {
Jan Tattermuschb5332812015-07-14 19:29:35 -070068 metadata.Add(new Metadata.Entry("customHeader", "abcdef"));
69 };
Jan Tattermusch15111f52015-02-05 18:15:14 -080070 }
71
Jan Tattermusch607307d2015-02-18 11:05:45 -080072 [TestFixtureTearDown]
73 public void Cleanup()
74 {
75 channel.Dispose();
Jan Tattermusch607307d2015-02-18 11:05:45 -080076 server.ShutdownAsync().Wait();
77 GrpcEnvironment.Shutdown();
78 }
79
Jan Tattermusch15111f52015-02-05 18:15:14 -080080 [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 Tattermusch2d2652d2015-05-18 16:23:04 -070096 [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 Tattermusch15111f52015-02-05 18:15:14 -0800109
110 [Test]
111 public void DivAsync()
112 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700113 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 Tattermusch15111f52015-02-05 18:15:14 -0800119 }
120
121 [Test]
122 public void Fib()
123 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700124 Task.Run(async () =>
125 {
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700126 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 Tattermusch9f550a32015-05-18 16:00:36 -0700131 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700132 }).Wait();
Jan Tattermusch15111f52015-02-05 18:15:14 -0800133 }
134
Jan Tattermuschf7cfc8a2015-07-23 17:26:35 -0700135 [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 Tattermusch41062592015-07-23 17:30:24 -0700168 [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 Tattermusch15111f52015-02-05 18:15:14 -0800189 // TODO: test Fib with limit=0 and cancellation
190 [Test]
191 public void Sum()
192 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700193 Task.Run(async () =>
194 {
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700195 using (var call = client.Sum())
196 {
197 var numbers = new List<long> { 10, 20, 30 }.ConvertAll(
198 n => Num.CreateBuilder().SetNum_(n).Build());
Jan Tattermusch15111f52015-02-05 18:15:14 -0800199
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700200 await call.RequestStream.WriteAll(numbers);
Jan Tattermuscha236ff22015-07-21 12:33:31 -0700201 var result = await call.ResponseAsync;
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700202 Assert.AreEqual(60, result.Num_);
203 }
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700204 }).Wait();
Jan Tattermusch15111f52015-02-05 18:15:14 -0800205 }
206
207 [Test]
208 public void DivMany()
209 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700210 Task.Run(async () =>
Jan Tattermusch075dde42015-03-11 18:21:00 -0700211 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700212 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 Tattermusch15111f52015-02-05 18:15:14 -0800218
Jan Tattermusch9f550a32015-05-18 16:00:36 -0700219 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 Tattermuscha5272b62015-04-30 11:56:46 -0700227 }).Wait();
Jan Tattermusch15111f52015-02-05 18:15:14 -0800228 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800229 }
230}