From ef5ad3deeaaec690cf0ee315e11d75b35f220ec7 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 19 Nov 2016 12:37:11 +0000 Subject: [PATCH] defaullt(MemoryPoolIterator) test coverage --- .../Infrastructure/MemoryPoolIterator.cs | 81 ++++++++++--------- .../MemoryPoolIteratorTests.cs | 28 +++++++ 2 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs index 97b71e3a2d..4cceb50bf7 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/MemoryPoolIterator.cs @@ -136,9 +136,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public void Skip(int bytesToSkip) { var block = _block; - if (block == null) + if (block == null && bytesToSkip > 0) { - return; + ThrowInvalidOperationException_SkipMoreThanAvailable(); } // Always set wasLastBlock before checking .End to avoid race which may cause data loss @@ -307,12 +307,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure { bytesScanned = 0; - if (IsDefault || limit <= 0) + var block = _block; + if (block == null || limit <= 0) { return -1; } - var block = _block; var index = _index; var wasLastBlock = block.Next == null; var following = block.End - index; @@ -413,12 +413,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure byte byte0, ref MemoryPoolIterator limit) { - if (IsDefault) + var block = _block; + if (block == null) { return -1; } - var block = _block; var index = _index; var wasLastBlock = block.Next == null; var following = block.End - index; @@ -520,12 +520,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure byte byte1, ref MemoryPoolIterator limit) { - if (IsDefault) + var block = _block; + if (block == null) { return -1; } - var block = _block; var index = _index; var wasLastBlock = block.Next == null; var following = block.End - index; @@ -650,12 +650,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure byte byte2, ref MemoryPoolIterator limit) { - if (IsDefault) + var block = _block; + if (block == null) { return -1; } - var block = _block; var index = _index; var wasLastBlock = block.Next == null; var following = block.End - index; @@ -800,12 +800,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Put(byte data) { - if (_block == null) + var block = _block; + if (block == null) { return false; } - var block = _block; var index = _index; // Always set wasLastBlock before checking .End to avoid race which may cause data loss @@ -851,12 +851,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetLength(MemoryPoolIterator end) { - if (IsDefault || end.IsDefault) + var block = _block; + if (block == null || end.IsDefault) { return -1; } - if (_block == end._block) + if (block == end._block) { return end._index - _index; } @@ -894,13 +895,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public MemoryPoolIterator CopyTo(byte[] array, int offset, int count, out int actual) { - if (IsDefault) + var block = _block; + if (block == null) { actual = 0; return this; } - var block = _block; var index = _index; var remaining = count; while (true) @@ -955,17 +956,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public void CopyFrom(byte[] data, int offset, int count) { - if (IsDefault) + var block = _block; + if (block == null) { return; } - Debug.Assert(_block != null); - Debug.Assert(_block.Next == null); - Debug.Assert(_block.End == _index); + Debug.Assert(block.Next == null); + Debug.Assert(block.End == _index); - var pool = _block.Pool; - var block = _block; + var pool = block.Pool; var blockIndex = _index; var bufferIndex = offset; @@ -1002,17 +1002,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public unsafe void CopyFromAscii(string data) { - if (IsDefault) + var block = _block; + if (block == null) { return; } - Debug.Assert(_block != null); - Debug.Assert(_block.Next == null); - Debug.Assert(_block.End == _index); + Debug.Assert(block.Next == null); + Debug.Assert(block.End == _index); - var pool = _block.Pool; - var block = _block; + var pool = block.Pool; var blockIndex = _index; var length = data.Length; @@ -1068,7 +1067,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public unsafe string GetAsciiString(ref MemoryPoolIterator end) { - if (IsDefault || end.IsDefault) + var block = _block; + if (block == null || end.IsDefault) { return null; } @@ -1080,8 +1080,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure return null; } - var inputOffset = Index; - var block = Block; + var inputOffset = _index; var asciiString = new string('\0', length); @@ -1124,13 +1123,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure public string GetUtf8String(ref MemoryPoolIterator end) { - if (IsDefault || end.IsDefault) + var block = _block; + if (block == null || end.IsDefault) { return default(string); } - if (end.Block == Block) + + var index = _index; + if (end.Block == block) { - return Encoding.UTF8.GetString(Block.Array, Index, end.Index - Index); + return Encoding.UTF8.GetString(block.Array, index, end.Index - index); } var decoder = Encoding.UTF8.GetDecoder(); @@ -1141,8 +1143,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure var chars = new char[charLength]; var charIndex = 0; - var block = Block; - var index = Index; var remaining = length; while (true) { @@ -1204,13 +1204,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public ArraySegment GetArraySegment(MemoryPoolIterator end) { - if (IsDefault || end.IsDefault) + var block = _block; + if (block == null || end.IsDefault) { return default(ArraySegment); } - if (end.Block == Block) + + var index = _index; + if (end.Block == block) { - return new ArraySegment(Block.Array, Index, end.Index - Index); + return new ArraySegment(block.Array, index, end.Index - index); } return GetArraySegmentMultiBlock(ref end); diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/MemoryPoolIteratorTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/MemoryPoolIteratorTests.cs index 156832ca0f..f3ec36afe2 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/MemoryPoolIteratorTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/MemoryPoolIteratorTests.cs @@ -966,6 +966,34 @@ namespace Microsoft.AspNetCore.Server.KestrelTests } } + [Fact] + public void EmptyIteratorBehaviourIsValid() + { + const byte byteCr = (byte) '\n'; + ulong longValue; + var end = default(MemoryPoolIterator); + + Assert.False(default(MemoryPoolIterator).TryPeekLong(out longValue)); + Assert.False(default(MemoryPoolIterator).Put(byteCr)); + Assert.Null(default(MemoryPoolIterator).GetAsciiString(ref end)); + Assert.Null(default(MemoryPoolIterator).GetUtf8String(ref end)); + // Assert.Equal doesn't work for default(ArraySegments) + Assert.True(default(MemoryPoolIterator).GetArraySegment(end).Equals(default(ArraySegment))); + Assert.True(default(MemoryPoolIterator).IsDefault); + Assert.True(default(MemoryPoolIterator).IsEnd); + Assert.Equal(default(MemoryPoolIterator).Take(), -1); + Assert.Equal(default(MemoryPoolIterator).Peek(), -1); + Assert.Equal(default(MemoryPoolIterator).Seek(byteCr), -1); + Assert.Equal(default(MemoryPoolIterator).Seek(byteCr, ref end), -1); + Assert.Equal(default(MemoryPoolIterator).Seek(byteCr, byteCr), -1); + Assert.Equal(default(MemoryPoolIterator).Seek(byteCr, byteCr, byteCr), -1); + + default(MemoryPoolIterator).CopyFrom(default(ArraySegment)); + default(MemoryPoolIterator).CopyFromAscii(""); + Assert.Equal(default(MemoryPoolIterator).GetLength(end), -1); + Assert.ThrowsAny(() => default(MemoryPoolIterator).Skip(1)); + } + [Theory] [InlineData("a", "a", 1)] [InlineData("ab", "a...", 1)]