public void ResetContextAndReinitialize()
        {
            var origBuffer = GetTestBuffer(100);
            var context    = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer));

            Assert.AreEqual(origBuffer.Length, context.PayloadLength);

            // Reset invalidates context
            context.Reset();

            Assert.AreEqual(0, context.PayloadLength);
            Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsNewBuffer());
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY
            Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsReadOnlySequence());
#endif

            // Previously reset context can be initialized again
            var origBuffer2 = GetTestBuffer(50);
            context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer2));

            Assert.AreEqual(origBuffer2.Length, context.PayloadLength);
            CollectionAssert.AreEqual(origBuffer2, context.PayloadAsNewBuffer());
        }
        public void PayloadAsReadOnlySequence_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3)
        {
            var origBuffer1 = GetTestBuffer(segmentLen1);
            var origBuffer2 = GetTestBuffer(segmentLen2);
            var origBuffer3 = GetTestBuffer(segmentLen3);
            int totalLen    = origBuffer1.Length + origBuffer2.Length + origBuffer3.Length;

            var context = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List <byte[]> {
                origBuffer1, origBuffer2, origBuffer3
            }));

            Assert.AreEqual(totalLen, context.PayloadLength);

            var sequence = context.PayloadAsReadOnlySequence();

            Assert.AreEqual(totalLen, sequence.Length);

            var segmentEnumerator = sequence.GetEnumerator();

            Assert.IsTrue(segmentEnumerator.MoveNext());
            CollectionAssert.AreEqual(origBuffer1, segmentEnumerator.Current.ToArray());

            Assert.IsTrue(segmentEnumerator.MoveNext());
            CollectionAssert.AreEqual(origBuffer2, segmentEnumerator.Current.ToArray());

            Assert.IsTrue(segmentEnumerator.MoveNext());
            CollectionAssert.AreEqual(origBuffer3, segmentEnumerator.Current.ToArray());

            Assert.IsFalse(segmentEnumerator.MoveNext());
        }
        public void PayloadAsNewByteBuffer_SingleSegmentPayload(int segmentLength)
        {
            var origBuffer = GetTestBuffer(segmentLength);
            var context    = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer));

            Assert.AreEqual(origBuffer.Length, context.PayloadLength);

            var payload = context.PayloadAsNewBuffer();

            CollectionAssert.AreEqual(origBuffer, payload);
        }
        public void PayloadAsNewByteBuffer_ZeroSegmentPayload()
        {
            var context = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List <byte[]> {
            }));

            Assert.AreEqual(0, context.PayloadLength);

            var payload = context.PayloadAsNewBuffer();

            Assert.AreEqual(0, payload.Length);
        }
        public void PayloadAsReadOnlySequence_ZeroSegmentPayload()
        {
            var context = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List <byte[]> {
            }));

            Assert.AreEqual(0, context.PayloadLength);

            var sequence = context.PayloadAsReadOnlySequence();

            Assert.AreEqual(ReadOnlySequence <byte> .Empty, sequence);
            Assert.IsTrue(sequence.IsEmpty);
            Assert.IsTrue(sequence.IsSingleSegment);
        }
        public void PayloadAsReadOnlySequence_SingleSegmentPayload(int segmentLength)
        {
            var origBuffer = GetTestBuffer(segmentLength);
            var context    = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer));

            Assert.AreEqual(origBuffer.Length, context.PayloadLength);

            var sequence = context.PayloadAsReadOnlySequence();

            Assert.AreEqual(origBuffer.Length, sequence.Length);
            Assert.AreEqual(origBuffer.Length, sequence.First.Length);
            Assert.IsTrue(sequence.IsSingleSegment);
            CollectionAssert.AreEqual(origBuffer, sequence.First.ToArray());
        }
        public void GetPayloadMultipleTimesIsIllegal()
        {
            var origBuffer = GetTestBuffer(100);
            var context    = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer));

            Assert.AreEqual(origBuffer.Length, context.PayloadLength);

            var payload = context.PayloadAsNewBuffer();

            CollectionAssert.AreEqual(origBuffer, payload);

            // Getting payload multiple times is illegal
            Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsNewBuffer());
            Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsReadOnlySequence());
        }
        public void PayloadAsNewByteBuffer_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3)
        {
            var origBuffer1 = GetTestBuffer(segmentLen1);
            var origBuffer2 = GetTestBuffer(segmentLen2);
            var origBuffer3 = GetTestBuffer(segmentLen3);

            var context = new DefaultDeserializationContext();

            context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List <byte[]> {
                origBuffer1, origBuffer2, origBuffer3
            }));

            var payload = context.PayloadAsNewBuffer();

            var concatenatedOrigBuffers = new List <byte>();

            concatenatedOrigBuffers.AddRange(origBuffer1);
            concatenatedOrigBuffers.AddRange(origBuffer2);
            concatenatedOrigBuffers.AddRange(origBuffer3);

            Assert.AreEqual(concatenatedOrigBuffers.Count, context.PayloadLength);
            Assert.AreEqual(concatenatedOrigBuffers.Count, payload.Length);
            CollectionAssert.AreEqual(concatenatedOrigBuffers, payload);
        }
        public void NullPayloadNotAllowed()
        {
            var context = new DefaultDeserializationContext();

            Assert.Throws(typeof(InvalidOperationException), () => context.Initialize(fakeBufferReaderManager.CreateNullPayloadBufferReader()));
        }