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.Diagnostics; |
| 37 using System.IO; |
| 38 using System.Linq; |
| 39 using System.Threading; |
| 40 using System.Threading.Tasks; |
| 41 |
| 42 using Grpc.Core; |
| 43 using Grpc.Core.Internal; |
| 44 using Grpc.Core.Utils; |
| 45 using NUnit.Framework; |
| 46 |
| 47 namespace Grpc.Core.Tests |
| 48 { |
| 49 public class MarshallingErrorsTest |
| 50 { |
| 51 const string Host = "127.0.0.1"; |
| 52 |
| 53 MockServiceHelper helper; |
| 54 Server server; |
| 55 Channel channel; |
| 56 |
| 57 [SetUp] |
| 58 public void Init() |
| 59 { |
| 60 var marshaller = new Marshaller<string>( |
| 61 (str) => |
| 62 { |
| 63 if (str == "UNSERIALIZABLE_VALUE") |
| 64 { |
| 65 // Google.Protobuf throws exception inherited from IOExc
eption |
| 66 throw new IOException("Error serializing the message."); |
| 67 } |
| 68 return System.Text.Encoding.UTF8.GetBytes(str); |
| 69 }, |
| 70 (payload) => |
| 71 { |
| 72 var s = System.Text.Encoding.UTF8.GetString(payload); |
| 73 if (s == "UNPARSEABLE_VALUE") |
| 74 { |
| 75 // Google.Protobuf throws exception inherited from IOExc
eption |
| 76 throw new IOException("Error parsing the message."); |
| 77 } |
| 78 return s; |
| 79 }); |
| 80 helper = new MockServiceHelper(Host, marshaller); |
| 81 server = helper.GetServer(); |
| 82 server.Start(); |
| 83 channel = helper.GetChannel(); |
| 84 } |
| 85 |
| 86 [TearDown] |
| 87 public void Cleanup() |
| 88 { |
| 89 channel.ShutdownAsync().Wait(); |
| 90 server.ShutdownAsync().Wait(); |
| 91 } |
| 92 |
| 93 [Test] |
| 94 public void ResponseParsingError_UnaryResponse() |
| 95 { |
| 96 helper.UnaryHandler = new UnaryServerMethod<string, string>((request
, context) => |
| 97 { |
| 98 return Task.FromResult("UNPARSEABLE_VALUE"); |
| 99 }); |
| 100 |
| 101 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(h
elper.CreateUnaryCall(), "REQUEST")); |
| 102 Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode); |
| 103 } |
| 104 |
| 105 [Test] |
| 106 public void ResponseParsingError_StreamingResponse() |
| 107 { |
| 108 helper.ServerStreamingHandler = new ServerStreamingServerMethod<stri
ng, string>(async (request, responseStream, context) => |
| 109 { |
| 110 await responseStream.WriteAsync("UNPARSEABLE_VALUE"); |
| 111 await Task.Delay(10000); |
| 112 }); |
| 113 |
| 114 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreami
ngCall(), "REQUEST"); |
| 115 var ex = Assert.Throws<RpcException>(async () => await call.Response
Stream.MoveNext()); |
| 116 Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode); |
| 117 } |
| 118 |
| 119 [Test] |
| 120 public void RequestParsingError_UnaryRequest() |
| 121 { |
| 122 helper.UnaryHandler = new UnaryServerMethod<string, string>((request
, context) => |
| 123 { |
| 124 return Task.FromResult("RESPONSE"); |
| 125 }); |
| 126 |
| 127 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(h
elper.CreateUnaryCall(), "UNPARSEABLE_VALUE")); |
| 128 // Spec doesn't define the behavior. With the current implementation
server handler throws exception which results in StatusCode.Unknown. |
| 129 Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode); |
| 130 } |
| 131 |
| 132 [Test] |
| 133 public async Task RequestParsingError_StreamingRequest() |
| 134 { |
| 135 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri
ng, string>(async (requestStream, context) => |
| 136 { |
| 137 Assert.Throws<IOException>(async () => await requestStream.MoveN
ext()); |
| 138 return "RESPONSE"; |
| 139 }); |
| 140 |
| 141 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreami
ngCall()); |
| 142 await call.RequestStream.WriteAsync("UNPARSEABLE_VALUE"); |
| 143 |
| 144 Assert.AreEqual("RESPONSE", await call); |
| 145 } |
| 146 |
| 147 [Test] |
| 148 public void RequestSerializationError_BlockingUnary() |
| 149 { |
| 150 Assert.Throws<IOException>(() => Calls.BlockingUnaryCall(helper.Crea
teUnaryCall(), "UNSERIALIZABLE_VALUE")); |
| 151 } |
| 152 |
| 153 [Test] |
| 154 public void RequestSerializationError_AsyncUnary() |
| 155 { |
| 156 Assert.Throws<IOException>(async () => await Calls.AsyncUnaryCall(he
lper.CreateUnaryCall(), "UNSERIALIZABLE_VALUE")); |
| 157 } |
| 158 |
| 159 [Test] |
| 160 public async Task RequestSerializationError_ClientStreaming() |
| 161 { |
| 162 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri
ng, string>(async (requestStream, context) => |
| 163 { |
| 164 CollectionAssert.AreEqual(new[] { "A", "B" }, await requestStrea
m.ToListAsync()); |
| 165 return "RESPONSE"; |
| 166 }); |
| 167 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreami
ngCall()); |
| 168 await call.RequestStream.WriteAsync("A"); |
| 169 Assert.Throws<IOException>(async () => await call.RequestStream.Writ
eAsync("UNSERIALIZABLE_VALUE")); |
| 170 await call.RequestStream.WriteAsync("B"); |
| 171 await call.RequestStream.CompleteAsync(); |
| 172 |
| 173 Assert.AreEqual("RESPONSE", await call); |
| 174 } |
| 175 } |
| 176 } |
OLD | NEW |