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 MarshallerTest 35 { 36 [Test] ContextualSerializerEmulation()37 public void ContextualSerializerEmulation() 38 { 39 Func<string, byte[]> simpleSerializer = System.Text.Encoding.UTF8.GetBytes; 40 Func<byte[], string> simpleDeserializer = System.Text.Encoding.UTF8.GetString; 41 var marshaller = new Marshaller<string>(simpleSerializer, 42 simpleDeserializer); 43 44 Assert.AreSame(simpleSerializer, marshaller.Serializer); 45 Assert.AreSame(simpleDeserializer, marshaller.Deserializer); 46 47 // test that emulated contextual serializer and deserializer work 48 string origMsg = "abc"; 49 var serializationContext = new FakeSerializationContext(); 50 marshaller.ContextualSerializer(origMsg, serializationContext); 51 52 var deserializationContext = new FakeDeserializationContext(serializationContext.Payload); 53 Assert.AreEqual(origMsg, marshaller.ContextualDeserializer(deserializationContext)); 54 } 55 56 [Test] SimpleSerializerEmulation()57 public void SimpleSerializerEmulation() 58 { 59 Action<string, SerializationContext> contextualSerializer = (str, context) => 60 { 61 var bytes = System.Text.Encoding.UTF8.GetBytes(str); 62 context.Complete(bytes); 63 }; 64 Func<DeserializationContext, string> contextualDeserializer = (context) => 65 { 66 return System.Text.Encoding.UTF8.GetString(context.PayloadAsNewBuffer()); 67 }; 68 var marshaller = new Marshaller<string>(contextualSerializer, contextualDeserializer); 69 70 Assert.AreSame(contextualSerializer, marshaller.ContextualSerializer); 71 Assert.AreSame(contextualDeserializer, marshaller.ContextualDeserializer); 72 Assert.Throws(typeof(NotImplementedException), () => marshaller.Serializer("abc")); 73 Assert.Throws(typeof(NotImplementedException), () => marshaller.Deserializer(new byte[] {1, 2, 3})); 74 } 75 76 class FakeSerializationContext : SerializationContext 77 { 78 public byte[] Payload; Complete(byte[] payload)79 public override void Complete(byte[] payload) 80 { 81 this.Payload = payload; 82 } 83 } 84 85 class FakeDeserializationContext : DeserializationContext 86 { 87 public byte[] payload; 88 FakeDeserializationContext(byte[] payload)89 public FakeDeserializationContext(byte[] payload) 90 { 91 this.payload = payload; 92 } 93 94 public override int PayloadLength => payload.Length; 95 PayloadAsNewBuffer()96 public override byte[] PayloadAsNewBuffer() 97 { 98 return payload; 99 } 100 } 101 } 102 } 103