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 Grpc.Core; 22 using Grpc.Core.Internal; 23 using Grpc.Core.Utils; 24 using NUnit.Framework; 25 26 namespace Grpc.Core.Internal.Tests 27 { 28 public class FakeBufferReaderManagerTest 29 { 30 FakeBufferReaderManager fakeBufferReaderManager; 31 32 [SetUp] Init()33 public void Init() 34 { 35 fakeBufferReaderManager = new FakeBufferReaderManager(); 36 } 37 38 [TearDown] Cleanup()39 public void Cleanup() 40 { 41 fakeBufferReaderManager.Dispose(); 42 } 43 44 [TestCase] NullPayload()45 public void NullPayload() 46 { 47 var fakeBufferReader = fakeBufferReaderManager.CreateNullPayloadBufferReader(); 48 Assert.IsFalse(fakeBufferReader.TotalLength.HasValue); 49 Assert.Throws(typeof(ArgumentNullException), () => fakeBufferReader.TryGetNextSlice(out Slice slice)); 50 } 51 [TestCase] ZeroSegmentPayload()52 public void ZeroSegmentPayload() 53 { 54 var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {}); 55 Assert.AreEqual(0, fakeBufferReader.TotalLength.Value); 56 Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice)); 57 } 58 59 [TestCase(0)] 60 [TestCase(1)] 61 [TestCase(10)] 62 [TestCase(30)] 63 [TestCase(100)] 64 [TestCase(1000)] SingleSegmentPayload(int bufferLen)65 public void SingleSegmentPayload(int bufferLen) 66 { 67 var origBuffer = GetTestBuffer(bufferLen); 68 var fakeBufferReader = fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer); 69 Assert.AreEqual(origBuffer.Length, fakeBufferReader.TotalLength.Value); 70 71 Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice)); 72 AssertSliceDataEqual(origBuffer, slice); 73 74 Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice2)); 75 } 76 77 [TestCase(0, 5, 10)] 78 [TestCase(1, 1, 1)] 79 [TestCase(10, 100, 1000)] 80 [TestCase(100, 100, 10)] 81 [TestCase(1000, 1000, 1000)] MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3)82 public void MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) 83 { 84 var origBuffer1 = GetTestBuffer(segmentLen1); 85 var origBuffer2 = GetTestBuffer(segmentLen2); 86 var origBuffer3 = GetTestBuffer(segmentLen3); 87 var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 }); 88 89 Assert.AreEqual(origBuffer1.Length + origBuffer2.Length + origBuffer3.Length, fakeBufferReader.TotalLength.Value); 90 91 Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice1)); 92 AssertSliceDataEqual(origBuffer1, slice1); 93 94 Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice2)); 95 AssertSliceDataEqual(origBuffer2, slice2); 96 97 Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice3)); 98 AssertSliceDataEqual(origBuffer3, slice3); 99 100 Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice4)); 101 } 102 AssertSliceDataEqual(byte[] expected, Slice actual)103 private void AssertSliceDataEqual(byte[] expected, Slice actual) 104 { 105 var actualSliceData = new byte[actual.Length]; 106 actual.ToSpanUnsafe().CopyTo(actualSliceData); 107 CollectionAssert.AreEqual(expected, actualSliceData); 108 } 109 110 // create a buffer of given size and fill it with some data GetTestBuffer(int length)111 private byte[] GetTestBuffer(int length) 112 { 113 var testBuffer = new byte[length]; 114 for (int i = 0; i < testBuffer.Length; i++) 115 { 116 testBuffer[i] = (byte) i; 117 } 118 return testBuffer; 119 } 120 } 121 } 122