OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 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. |
| 19 // |
| 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 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.Linq; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Utils; |
| 41 using NUnit.Framework; |
| 42 |
| 43 namespace Math.Tests |
| 44 { |
| 45 /// <summary> |
| 46 /// Math client talks to local math server. |
| 47 /// </summary> |
| 48 public class MathClientServerTest |
| 49 { |
| 50 const string Host = "localhost"; |
| 51 Server server; |
| 52 Channel channel; |
| 53 Math.MathClient client; |
| 54 |
| 55 [TestFixtureSetUp] |
| 56 public void Init() |
| 57 { |
| 58 server = new Server |
| 59 { |
| 60 Services = { Math.BindService(new MathServiceImpl()) }, |
| 61 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insec
ure } } |
| 62 }; |
| 63 server.Start(); |
| 64 channel = new Channel(Host, server.Ports.Single().BoundPort, Channel
Credentials.Insecure); |
| 65 client = Math.NewClient(channel); |
| 66 } |
| 67 |
| 68 [TestFixtureTearDown] |
| 69 public void Cleanup() |
| 70 { |
| 71 channel.ShutdownAsync().Wait(); |
| 72 server.ShutdownAsync().Wait(); |
| 73 } |
| 74 |
| 75 [Test] |
| 76 public void Div1() |
| 77 { |
| 78 DivReply response = client.Div(new DivArgs { Dividend = 10, Divisor
= 3 }); |
| 79 Assert.AreEqual(3, response.Quotient); |
| 80 Assert.AreEqual(1, response.Remainder); |
| 81 } |
| 82 |
| 83 [Test] |
| 84 public void Div2() |
| 85 { |
| 86 DivReply response = client.Div(new DivArgs { Dividend = 0, Divisor =
1 }); |
| 87 Assert.AreEqual(0, response.Quotient); |
| 88 Assert.AreEqual(0, response.Remainder); |
| 89 } |
| 90 |
| 91 [Test] |
| 92 public void DivByZero() |
| 93 { |
| 94 var ex = Assert.Throws<RpcException>(() => client.Div(new DivArgs {
Dividend = 0, Divisor = 0 })); |
| 95 Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode); |
| 96 } |
| 97 |
| 98 [Test] |
| 99 public async Task DivAsync() |
| 100 { |
| 101 DivReply response = await client.DivAsync(new DivArgs { Dividend = 1
0, Divisor = 3 }); |
| 102 Assert.AreEqual(3, response.Quotient); |
| 103 Assert.AreEqual(1, response.Remainder); |
| 104 } |
| 105 |
| 106 [Test] |
| 107 public async Task Fib() |
| 108 { |
| 109 using (var call = client.Fib(new FibArgs { Limit = 6 })) |
| 110 { |
| 111 var responses = await call.ResponseStream.ToListAsync(); |
| 112 CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
| 113 responses.ConvertAll((n) => n.Num_)); |
| 114 } |
| 115 } |
| 116 |
| 117 [Test] |
| 118 public async Task FibWithCancel() |
| 119 { |
| 120 var cts = new CancellationTokenSource(); |
| 121 |
| 122 using (var call = client.Fib(new FibArgs { Limit = 0 }, cancellation
Token: cts.Token)) |
| 123 { |
| 124 List<long> responses = new List<long>(); |
| 125 |
| 126 try |
| 127 { |
| 128 while (await call.ResponseStream.MoveNext()) |
| 129 { |
| 130 if (responses.Count == 0) |
| 131 { |
| 132 cts.CancelAfter(500); // make sure we cancel soon |
| 133 } |
| 134 responses.Add(call.ResponseStream.Current.Num_); |
| 135 } |
| 136 Assert.Fail(); |
| 137 } |
| 138 catch (RpcException e) |
| 139 { |
| 140 Assert.IsTrue(responses.Count > 0); |
| 141 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 [Test] |
| 147 public async Task FibWithDeadline() |
| 148 { |
| 149 using (var call = client.Fib(new FibArgs { Limit = 0 }, |
| 150 deadline: DateTime.UtcNow.AddMilliseconds(500))) |
| 151 { |
| 152 var ex = Assert.Throws<RpcException>(async () => await call.Resp
onseStream.ToListAsync()); |
| 153 |
| 154 // We can't guarantee the status code always DeadlineExceeded. S
ee issue #2685. |
| 155 Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.Deadlin
eExceeded, StatusCode.Internal }); |
| 156 } |
| 157 } |
| 158 |
| 159 // TODO: test Fib with limit=0 and cancellation |
| 160 [Test] |
| 161 public async Task Sum() |
| 162 { |
| 163 using (var call = client.Sum()) |
| 164 { |
| 165 var numbers = new List<long> { 10, 20, 30 }.ConvertAll(n => new
Num { Num_ = n }); |
| 166 |
| 167 await call.RequestStream.WriteAllAsync(numbers); |
| 168 var result = await call.ResponseAsync; |
| 169 Assert.AreEqual(60, result.Num_); |
| 170 } |
| 171 } |
| 172 |
| 173 [Test] |
| 174 public async Task DivMany() |
| 175 { |
| 176 var divArgsList = new List<DivArgs> |
| 177 { |
| 178 new DivArgs { Dividend = 10, Divisor = 3 }, |
| 179 new DivArgs { Dividend = 100, Divisor = 21 }, |
| 180 new DivArgs { Dividend = 7, Divisor = 2 } |
| 181 }; |
| 182 |
| 183 using (var call = client.DivMany()) |
| 184 { |
| 185 await call.RequestStream.WriteAllAsync(divArgsList); |
| 186 var result = await call.ResponseStream.ToListAsync(); |
| 187 |
| 188 CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.Convert
All((divReply) => divReply.Quotient)); |
| 189 CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.Conver
tAll((divReply) => divReply.Remainder)); |
| 190 } |
| 191 } |
| 192 } |
| 193 } |
OLD | NEW |