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 | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 36 | using System.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | using System.Threading; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 39 | using Grpc.Core; |
| 40 | using Grpc.Core.Internal; |
| 41 | using Grpc.Core.Utils; |
| 42 | using NUnit.Framework; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 43 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 44 | namespace Grpc.Core.Tests |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | { |
| 46 | public class ClientServerTest |
| 47 | { |
Jan Tattermusch | 062c329 | 2015-07-23 20:28:42 -0700 | [diff] [blame] | 48 | const string Host = "127.0.0.1"; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 49 | const string ServiceName = "/tests.Test"; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 50 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 51 | static readonly Method<string, string> EchoMethod = new Method<string, string>( |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 52 | MethodType.Unary, |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 53 | "/tests.Test/Echo", |
| 54 | Marshallers.StringMarshaller, |
| 55 | Marshallers.StringMarshaller); |
| 56 | |
| 57 | static readonly Method<string, string> ConcatAndEchoMethod = new Method<string, string>( |
| 58 | MethodType.ClientStreaming, |
| 59 | "/tests.Test/ConcatAndEcho", |
| 60 | Marshallers.StringMarshaller, |
| 61 | Marshallers.StringMarshaller); |
| 62 | |
| 63 | static readonly Method<string, string> NonexistentMethod = new Method<string, string>( |
| 64 | MethodType.Unary, |
| 65 | "/tests.Test/NonexistentMethod", |
Jan Tattermusch | 15111f5 | 2015-02-05 18:15:14 -0800 | [diff] [blame] | 66 | Marshallers.StringMarshaller, |
| 67 | Marshallers.StringMarshaller); |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 68 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 69 | static readonly ServerServiceDefinition ServiceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 70 | .AddMethod(EchoMethod, EchoHandler) |
| 71 | .AddMethod(ConcatAndEchoMethod, ConcatAndEchoHandler) |
| 72 | .Build(); |
| 73 | |
| 74 | Server server; |
| 75 | Channel channel; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 76 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 77 | [SetUp] |
| 78 | public void Init() |
| 79 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 80 | server = new Server(); |
| 81 | server.AddServiceDefinition(ServiceDefinition); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 82 | int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 83 | server.Start(); |
Jan Tattermusch | a96ac05 | 2015-07-24 14:49:30 -0700 | [diff] [blame] | 84 | channel = new Channel(Host, port, Credentials.Insecure); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | [TearDown] |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 88 | public void Cleanup() |
| 89 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 90 | channel.Dispose(); |
| 91 | server.ShutdownAsync().Wait(); |
| 92 | } |
| 93 | |
| 94 | [TestFixtureTearDown] |
| 95 | public void CleanupClass() |
| 96 | { |
Jan Tattermusch | ec77624 | 2015-02-18 14:06:56 -0800 | [diff] [blame] | 97 | GrpcEnvironment.Shutdown(); |
Jan Tattermusch | 607307d | 2015-02-18 11:05:45 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 100 | [Test] |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 101 | public void UnaryCall() |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 102 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 103 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 104 | Assert.AreEqual("ABC", Calls.BlockingUnaryCall(internalCall, "ABC", CancellationToken.None)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 108 | public void UnaryCall_ServerHandlerThrows() |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 109 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 110 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 111 | try |
| 112 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 113 | Calls.BlockingUnaryCall(internalCall, "THROW", CancellationToken.None); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 114 | Assert.Fail(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 115 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 116 | catch (RpcException e) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 117 | { |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 118 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 119 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 120 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 121 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 122 | [Test] |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 123 | public void UnaryCall_ServerHandlerThrowsRpcException() |
| 124 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 125 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 126 | try |
| 127 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 128 | Calls.BlockingUnaryCall(internalCall, "THROW_UNAUTHENTICATED", CancellationToken.None); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 129 | Assert.Fail(); |
| 130 | } |
| 131 | catch (RpcException e) |
| 132 | { |
Jan Tattermusch | 7d219cf | 2015-07-21 12:23:31 -0700 | [diff] [blame] | 133 | Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | [Test] |
| 138 | public void UnaryCall_ServerHandlerSetsStatus() |
| 139 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 140 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 141 | try |
| 142 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 143 | Calls.BlockingUnaryCall(internalCall, "SET_UNAUTHENTICATED", CancellationToken.None); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 144 | Assert.Fail(); |
| 145 | } |
| 146 | catch (RpcException e) |
| 147 | { |
| 148 | Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 153 | public void AsyncUnaryCall() |
| 154 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 155 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 156 | var result = Calls.AsyncUnaryCall(internalCall, "ABC", CancellationToken.None).ResponseAsync.Result; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 157 | Assert.AreEqual("ABC", result); |
| 158 | } |
| 159 | |
| 160 | [Test] |
| 161 | public void AsyncUnaryCall_ServerHandlerThrows() |
| 162 | { |
Jan Tattermusch | 32d95b9 | 2015-05-04 17:56:32 -0700 | [diff] [blame] | 163 | Task.Run(async () => |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 164 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 165 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 166 | try |
| 167 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 168 | await Calls.AsyncUnaryCall(internalCall, "THROW", CancellationToken.None); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 169 | Assert.Fail(); |
| 170 | } |
| 171 | catch (RpcException e) |
| 172 | { |
Jan Tattermusch | 32d95b9 | 2015-05-04 17:56:32 -0700 | [diff] [blame] | 173 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 174 | } |
| 175 | }).Wait(); |
| 176 | } |
| 177 | |
| 178 | [Test] |
| 179 | public void ClientStreamingCall() |
| 180 | { |
| 181 | Task.Run(async () => |
| 182 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 183 | var internalCall = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); |
| 184 | var call = Calls.AsyncClientStreamingCall(internalCall, CancellationToken.None); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 185 | |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 186 | await call.RequestStream.WriteAll(new string[] { "A", "B", "C" }); |
| 187 | Assert.AreEqual("ABC", await call.ResponseAsync); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 188 | }).Wait(); |
| 189 | } |
| 190 | |
| 191 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 192 | public void ClientStreamingCall_CancelAfterBegin() |
| 193 | { |
| 194 | Task.Run(async () => |
| 195 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 196 | var internalCall = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 197 | |
| 198 | var cts = new CancellationTokenSource(); |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 199 | var call = Calls.AsyncClientStreamingCall(internalCall, cts.Token); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 200 | |
| 201 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 202 | await Task.Delay(1000); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 203 | cts.Cancel(); |
| 204 | |
| 205 | try |
| 206 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 207 | await call.ResponseAsync; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 208 | } |
Jan Tattermusch | 618647d | 2015-05-01 11:23:28 -0700 | [diff] [blame] | 209 | catch (RpcException e) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 210 | { |
| 211 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 212 | } |
| 213 | }).Wait(); |
| 214 | } |
| 215 | |
| 216 | [Test] |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 217 | public void AsyncUnaryCall_EchoMetadata() |
| 218 | { |
Jan Tattermusch | 7d219cf | 2015-07-21 12:23:31 -0700 | [diff] [blame] | 219 | var headers = new Metadata |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 220 | { |
| 221 | new Metadata.Entry("asciiHeader", "abcdefg"), |
Jan Tattermusch | ae01709 | 2015-07-22 11:59:13 -0700 | [diff] [blame] | 222 | new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff }), |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 223 | }; |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 224 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, headers); |
| 225 | var call = Calls.AsyncUnaryCall(internalCall, "ABC", CancellationToken.None); |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 226 | |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 227 | Assert.AreEqual("ABC", call.ResponseAsync.Result); |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 228 | |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 229 | Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode); |
Jan Tattermusch | 7d219cf | 2015-07-21 12:23:31 -0700 | [diff] [blame] | 230 | |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 231 | var trailers = call.GetTrailers(); |
Jan Tattermusch | 7d219cf | 2015-07-21 12:23:31 -0700 | [diff] [blame] | 232 | Assert.AreEqual(2, trailers.Count); |
| 233 | Assert.AreEqual(headers[0].Key, trailers[0].Key); |
| 234 | Assert.AreEqual(headers[0].Value, trailers[0].Value); |
| 235 | |
| 236 | Assert.AreEqual(headers[1].Key, trailers[1].Key); |
| 237 | CollectionAssert.AreEqual(headers[1].ValueBytes, trailers[1].ValueBytes); |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | [Test] |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 241 | public void UnaryCall_DisposedChannel() |
| 242 | { |
| 243 | channel.Dispose(); |
| 244 | |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 245 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 246 | Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(internalCall, "ABC", CancellationToken.None)); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 249 | [Test] |
| 250 | public void UnaryCallPerformance() |
| 251 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 252 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 253 | BenchmarkUtil.RunBenchmark(100, 100, |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 254 | () => { Calls.BlockingUnaryCall(internalCall, "ABC", default(CancellationToken)); }); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 255 | } |
Jan Tattermusch | 797b875 | 2015-06-17 10:53:59 -0700 | [diff] [blame] | 256 | |
| 257 | [Test] |
| 258 | public void UnknownMethodHandler() |
| 259 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 260 | var internalCall = new Call<string, string>(ServiceName, NonexistentMethod, channel, Metadata.Empty); |
Jan Tattermusch | 797b875 | 2015-06-17 10:53:59 -0700 | [diff] [blame] | 261 | try |
| 262 | { |
Jan Tattermusch | e7e1c82 | 2015-07-21 12:38:07 -0700 | [diff] [blame] | 263 | Calls.BlockingUnaryCall(internalCall, "ABC", default(CancellationToken)); |
Jan Tattermusch | 797b875 | 2015-06-17 10:53:59 -0700 | [diff] [blame] | 264 | Assert.Fail(); |
| 265 | } |
| 266 | catch (RpcException e) |
| 267 | { |
| 268 | Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); |
| 269 | } |
| 270 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 271 | |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 272 | [Test] |
| 273 | public void UserAgentStringPresent() |
| 274 | { |
| 275 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 276 | string userAgent = Calls.BlockingUnaryCall(internalCall, "RETURN-USER-AGENT", CancellationToken.None); |
| 277 | Assert.IsTrue(userAgent.StartsWith("grpc-csharp/")); |
| 278 | } |
| 279 | |
Jan Tattermusch | 062c329 | 2015-07-23 20:28:42 -0700 | [diff] [blame] | 280 | [Test] |
| 281 | public void PeerInfoPresent() |
| 282 | { |
| 283 | var internalCall = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); |
| 284 | string peer = Calls.BlockingUnaryCall(internalCall, "RETURN-PEER", CancellationToken.None); |
| 285 | Assert.IsTrue(peer.Contains(Host)); |
| 286 | } |
| 287 | |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 288 | private static async Task<string> EchoHandler(string request, ServerCallContext context) |
Jan Tattermusch | 8d7ce43 | 2015-02-18 11:20:43 -0800 | [diff] [blame] | 289 | { |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 290 | foreach (Metadata.Entry metadataEntry in context.RequestHeaders) |
| 291 | { |
Jan Tattermusch | 766d72b | 2015-07-21 20:09:25 -0700 | [diff] [blame] | 292 | if (metadataEntry.Key != "user-agent") |
| 293 | { |
| 294 | context.ResponseTrailers.Add(metadataEntry); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (request == "RETURN-USER-AGENT") |
| 299 | { |
| 300 | return context.RequestHeaders.Where(entry => entry.Key == "user-agent").Single().Value; |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Jan Tattermusch | 062c329 | 2015-07-23 20:28:42 -0700 | [diff] [blame] | 303 | if (request == "RETURN-PEER") |
| 304 | { |
| 305 | return context.Peer; |
| 306 | } |
| 307 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 308 | if (request == "THROW") |
| 309 | { |
| 310 | throw new Exception("This was thrown on purpose by a test"); |
| 311 | } |
Jan Tattermusch | 998eb9b | 2015-07-20 22:12:53 -0700 | [diff] [blame] | 312 | |
Jan Tattermusch | 1cf8d42 | 2015-07-21 10:37:55 -0700 | [diff] [blame] | 313 | if (request == "THROW_UNAUTHENTICATED") |
| 314 | { |
| 315 | throw new RpcException(new Status(StatusCode.Unauthenticated, "")); |
| 316 | } |
| 317 | |
| 318 | if (request == "SET_UNAUTHENTICATED") |
| 319 | { |
| 320 | context.Status = new Status(StatusCode.Unauthenticated, ""); |
| 321 | } |
| 322 | |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 323 | return request; |
| 324 | } |
| 325 | |
Jan Tattermusch | 25bb2ef | 2015-07-21 12:15:53 -0700 | [diff] [blame] | 326 | private static async Task<string> ConcatAndEchoHandler(IAsyncStreamReader<string> requestStream, ServerCallContext context) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 327 | { |
| 328 | string result = ""; |
| 329 | await requestStream.ForEach(async (request) => |
| 330 | { |
| 331 | if (request == "THROW") |
| 332 | { |
| 333 | throw new Exception("This was thrown on purpose by a test"); |
| 334 | } |
| 335 | result += request; |
| 336 | }); |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 337 | // simulate processing takes some time. |
| 338 | await Task.Delay(250); |
| 339 | return result; |
Jan Tattermusch | 8ce5e8b | 2015-02-05 10:56:49 -0800 | [diff] [blame] | 340 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 341 | } |
| 342 | } |