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 | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 35 | using System.Diagnostics; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 36 | using System.Threading; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 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.Internal; |
| 40 | using Grpc.Core.Utils; |
| 41 | using NUnit.Framework; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 42 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 43 | namespace Grpc.Core.Tests |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | { |
| 45 | public class ClientServerTest |
| 46 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 47 | const string Host = "localhost"; |
| 48 | const string ServiceName = "/tests.Test"; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 49 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 50 | static readonly Method<string, string> EchoMethod = new Method<string, string>( |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 51 | MethodType.Unary, |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 52 | "/tests.Test/Echo", |
| 53 | Marshallers.StringMarshaller, |
| 54 | Marshallers.StringMarshaller); |
| 55 | |
| 56 | static readonly Method<string, string> ConcatAndEchoMethod = new Method<string, string>( |
| 57 | MethodType.ClientStreaming, |
| 58 | "/tests.Test/ConcatAndEcho", |
| 59 | Marshallers.StringMarshaller, |
| 60 | Marshallers.StringMarshaller); |
| 61 | |
| 62 | static readonly Method<string, string> NonexistentMethod = new Method<string, string>( |
| 63 | MethodType.Unary, |
| 64 | "/tests.Test/NonexistentMethod", |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 65 | Marshallers.StringMarshaller, |
| 66 | Marshallers.StringMarshaller); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 67 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 68 | static readonly ServerServiceDefinition ServiceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 69 | .AddMethod(EchoMethod, EchoHandler) |
| 70 | .AddMethod(ConcatAndEchoMethod, ConcatAndEchoHandler) |
| 71 | .Build(); |
| 72 | |
| 73 | Server server; |
| 74 | Channel channel; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 75 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 76 | [SetUp] |
| 77 | public void Init() |
| 78 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 79 | server = new Server(); |
| 80 | server.AddServiceDefinition(ServiceDefinition); |
Jan Tattermusch | 03e82e2 | 2015-05-06 16:37:12 -0700 | [diff] [blame] | 81 | int port = server.AddListeningPort(Host, Server.PickUnusedPort); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 82 | server.Start(); |
Jan Tattermusch | da71a4d | 2015-06-08 15:36:53 -0700 | [diff] [blame] | 83 | channel = new Channel(Host, port); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | [TearDown] |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 87 | public void Cleanup() |
| 88 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 89 | channel.Dispose(); |
| 90 | server.ShutdownAsync().Wait(); |
| 91 | } |
| 92 | |
| 93 | [TestFixtureTearDown] |
| 94 | public void CleanupClass() |
| 95 | { |
Jan Tattermusch | ec77624 | 2015-02-18 14:06:56 -0800 | [diff] [blame] | 96 | GrpcEnvironment.Shutdown(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 99 | [Test] |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 100 | public void UnaryCall() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 101 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 102 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 103 | Assert.AreEqual("ABC", Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 107 | public void UnaryCall_ServerHandlerThrows() |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 108 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 109 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 110 | try |
| 111 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 112 | Calls.BlockingUnaryCall(call, "THROW", CancellationToken.None); |
| 113 | Assert.Fail(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 114 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 115 | catch (RpcException e) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 116 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 117 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 118 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 119 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 120 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 121 | [Test] |
| 122 | public void AsyncUnaryCall() |
| 123 | { |
| 124 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 125 | var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result; |
| 126 | Assert.AreEqual("ABC", result); |
| 127 | } |
| 128 | |
| 129 | [Test] |
| 130 | public void AsyncUnaryCall_ServerHandlerThrows() |
| 131 | { |
Jan Tattermusch | 32d95b9 | 2015-05-04 17:56:32 -0700 | [diff] [blame] | 132 | Task.Run(async () => |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 133 | { |
| 134 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 135 | try |
| 136 | { |
| 137 | await Calls.AsyncUnaryCall(call, "THROW", CancellationToken.None); |
| 138 | Assert.Fail(); |
| 139 | } |
| 140 | catch (RpcException e) |
| 141 | { |
Jan Tattermusch | 32d95b9 | 2015-05-04 17:56:32 -0700 | [diff] [blame] | 142 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 143 | } |
| 144 | }).Wait(); |
| 145 | } |
| 146 | |
| 147 | [Test] |
| 148 | public void ClientStreamingCall() |
| 149 | { |
| 150 | Task.Run(async () => |
| 151 | { |
| 152 | var call = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); |
| 153 | var callResult = Calls.AsyncClientStreamingCall(call, CancellationToken.None); |
| 154 | |
| 155 | await callResult.RequestStream.WriteAll(new string[] { "A", "B", "C" }); |
| 156 | Assert.AreEqual("ABC", await callResult.Result); |
| 157 | }).Wait(); |
| 158 | } |
| 159 | |
| 160 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 161 | public void ClientStreamingCall_CancelAfterBegin() |
| 162 | { |
| 163 | Task.Run(async () => |
| 164 | { |
| 165 | var call = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); |
| 166 | |
| 167 | var cts = new CancellationTokenSource(); |
| 168 | var callResult = Calls.AsyncClientStreamingCall(call, cts.Token); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 169 | |
| 170 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 171 | await Task.Delay(1000); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 172 | cts.Cancel(); |
| 173 | |
| 174 | try |
| 175 | { |
| 176 | await callResult.Result; |
| 177 | } |
Jan Tattermusch | 618647d | 2015-05-01 11:23:28 -0700 | [diff] [blame] | 178 | catch (RpcException e) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 179 | { |
| 180 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 181 | } |
| 182 | }).Wait(); |
| 183 | } |
| 184 | |
| 185 | [Test] |
| 186 | public void UnaryCall_DisposedChannel() |
| 187 | { |
| 188 | channel.Dispose(); |
| 189 | |
| 190 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 191 | Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None)); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 194 | [Test] |
| 195 | public void UnaryCallPerformance() |
| 196 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 197 | var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 198 | BenchmarkUtil.RunBenchmark(100, 100, |
| 199 | () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); }); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 200 | } |
Jan Tattermusch | 797b875 | 2015-06-17 10:53:59 -0700 | [diff] [blame] | 201 | |
| 202 | [Test] |
| 203 | public void UnknownMethodHandler() |
| 204 | { |
| 205 | var call = new Call<string, string>(ServiceName, NonexistentMethod, channel, Metadata.Empty); |
| 206 | try |
| 207 | { |
| 208 | Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); |
| 209 | Assert.Fail(); |
| 210 | } |
| 211 | catch (RpcException e) |
| 212 | { |
| 213 | Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); |
| 214 | } |
| 215 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 216 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 217 | private static async Task<string> EchoHandler(ServerCallContext context, string request) |
Jan Tattermusch | 8d7ce43 | 2015-02-18 11:20:43 -0800 | [diff] [blame] | 218 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 219 | if (request == "THROW") |
| 220 | { |
| 221 | throw new Exception("This was thrown on purpose by a test"); |
| 222 | } |
| 223 | return request; |
| 224 | } |
| 225 | |
Jan Tattermusch | 8ab1f7e | 2015-05-06 15:56:30 -0700 | [diff] [blame] | 226 | private static async Task<string> ConcatAndEchoHandler(ServerCallContext context, IAsyncStreamReader<string> requestStream) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 227 | { |
| 228 | string result = ""; |
| 229 | await requestStream.ForEach(async (request) => |
| 230 | { |
| 231 | if (request == "THROW") |
| 232 | { |
| 233 | throw new Exception("This was thrown on purpose by a test"); |
| 234 | } |
| 235 | result += request; |
| 236 | }); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 237 | // simulate processing takes some time. |
| 238 | await Task.Delay(250); |
| 239 | return result; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 240 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 241 | } |
| 242 | } |