1 #region Copyright notice and license 2 3 // Copyright 2018 The gRPC Authors 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Collections.Generic; 21 using System.Diagnostics; 22 using System.IO; 23 using System.Linq; 24 using System.Threading; 25 using System.Threading.Tasks; 26 27 using Grpc.Core; 28 using Grpc.Core.Internal; 29 using Grpc.Core.Utils; 30 using NUnit.Framework; 31 32 namespace Grpc.Core.Tests 33 { 34 public class ContextualMarshallerTest 35 { 36 const string Host = "127.0.0.1"; 37 38 MockServiceHelper helper; 39 Server server; 40 Channel channel; 41 42 [SetUp] Init()43 public void Init() 44 { 45 var contextualMarshaller = new Marshaller<string>( 46 (str, serializationContext) => 47 { 48 if (str == "UNSERIALIZABLE_VALUE") 49 { 50 // Google.Protobuf throws exception inherited from IOException 51 throw new IOException("Error serializing the message."); 52 } 53 if (str == "SERIALIZE_TO_NULL") 54 { 55 // for contextual marshaller, serializing to null payload corresponds 56 // to not calling the Complete() method in the serializer. 57 return; 58 } 59 var bytes = System.Text.Encoding.UTF8.GetBytes(str); 60 serializationContext.Complete(bytes); 61 }, 62 (deserializationContext) => 63 { 64 var buffer = deserializationContext.PayloadAsNewBuffer(); 65 Assert.AreEqual(buffer.Length, deserializationContext.PayloadLength); 66 var s = System.Text.Encoding.UTF8.GetString(buffer); 67 if (s == "UNPARSEABLE_VALUE") 68 { 69 // Google.Protobuf throws exception inherited from IOException 70 throw new IOException("Error parsing the message."); 71 } 72 return s; 73 }); 74 helper = new MockServiceHelper(Host, contextualMarshaller); 75 server = helper.GetServer(); 76 server.Start(); 77 channel = helper.GetChannel(); 78 } 79 80 [TearDown] Cleanup()81 public void Cleanup() 82 { 83 channel.ShutdownAsync().Wait(); 84 server.ShutdownAsync().Wait(); 85 } 86 87 [Test] UnaryCall()88 public void UnaryCall() 89 { 90 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => 91 { 92 return Task.FromResult(request); 93 }); 94 Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC")); 95 } 96 97 [Test] ResponseParsingError_UnaryResponse()98 public void ResponseParsingError_UnaryResponse() 99 { 100 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => 101 { 102 return Task.FromResult("UNPARSEABLE_VALUE"); 103 }); 104 105 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "REQUEST")); 106 Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode); 107 } 108 109 [Test] RequestSerializationError_BlockingUnary()110 public void RequestSerializationError_BlockingUnary() 111 { 112 Assert.Throws<IOException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "UNSERIALIZABLE_VALUE")); 113 } 114 115 [Test] SerializationResultIsNull_BlockingUnary()116 public void SerializationResultIsNull_BlockingUnary() 117 { 118 Assert.Throws<NullReferenceException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "SERIALIZE_TO_NULL")); 119 } 120 } 121 } 122