defaullt(MemoryPoolIterator) test coverage

This commit is contained in:
Ben Adams 2016-11-19 12:37:11 +00:00
parent 2011a27bde
commit ef5ad3deea
2 changed files with 70 additions and 39 deletions

View File

@ -136,9 +136,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
public void Skip(int bytesToSkip) public void Skip(int bytesToSkip)
{ {
var block = _block; 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 // 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; bytesScanned = 0;
if (IsDefault || limit <= 0) var block = _block;
if (block == null || limit <= 0)
{ {
return -1; return -1;
} }
var block = _block;
var index = _index; var index = _index;
var wasLastBlock = block.Next == null; var wasLastBlock = block.Next == null;
var following = block.End - index; var following = block.End - index;
@ -413,12 +413,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
byte byte0, byte byte0,
ref MemoryPoolIterator limit) ref MemoryPoolIterator limit)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
return -1; return -1;
} }
var block = _block;
var index = _index; var index = _index;
var wasLastBlock = block.Next == null; var wasLastBlock = block.Next == null;
var following = block.End - index; var following = block.End - index;
@ -520,12 +520,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
byte byte1, byte byte1,
ref MemoryPoolIterator limit) ref MemoryPoolIterator limit)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
return -1; return -1;
} }
var block = _block;
var index = _index; var index = _index;
var wasLastBlock = block.Next == null; var wasLastBlock = block.Next == null;
var following = block.End - index; var following = block.End - index;
@ -650,12 +650,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
byte byte2, byte byte2,
ref MemoryPoolIterator limit) ref MemoryPoolIterator limit)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
return -1; return -1;
} }
var block = _block;
var index = _index; var index = _index;
var wasLastBlock = block.Next == null; var wasLastBlock = block.Next == null;
var following = block.End - index; var following = block.End - index;
@ -800,12 +800,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Put(byte data) public bool Put(byte data)
{ {
if (_block == null) var block = _block;
if (block == null)
{ {
return false; return false;
} }
var block = _block;
var index = _index; var index = _index;
// Always set wasLastBlock before checking .End to avoid race which may cause data loss // 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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetLength(MemoryPoolIterator end) public int GetLength(MemoryPoolIterator end)
{ {
if (IsDefault || end.IsDefault) var block = _block;
if (block == null || end.IsDefault)
{ {
return -1; return -1;
} }
if (_block == end._block) if (block == end._block)
{ {
return end._index - _index; 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) public MemoryPoolIterator CopyTo(byte[] array, int offset, int count, out int actual)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
actual = 0; actual = 0;
return this; return this;
} }
var block = _block;
var index = _index; var index = _index;
var remaining = count; var remaining = count;
while (true) while (true)
@ -955,17 +956,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
public void CopyFrom(byte[] data, int offset, int count) public void CopyFrom(byte[] data, int offset, int count)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
return; return;
} }
Debug.Assert(_block != null); Debug.Assert(block.Next == null);
Debug.Assert(_block.Next == null); Debug.Assert(block.End == _index);
Debug.Assert(_block.End == _index);
var pool = _block.Pool; var pool = block.Pool;
var block = _block;
var blockIndex = _index; var blockIndex = _index;
var bufferIndex = offset; var bufferIndex = offset;
@ -1002,17 +1002,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
public unsafe void CopyFromAscii(string data) public unsafe void CopyFromAscii(string data)
{ {
if (IsDefault) var block = _block;
if (block == null)
{ {
return; return;
} }
Debug.Assert(_block != null); Debug.Assert(block.Next == null);
Debug.Assert(_block.Next == null); Debug.Assert(block.End == _index);
Debug.Assert(_block.End == _index);
var pool = _block.Pool; var pool = block.Pool;
var block = _block;
var blockIndex = _index; var blockIndex = _index;
var length = data.Length; var length = data.Length;
@ -1068,7 +1067,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
public unsafe string GetAsciiString(ref MemoryPoolIterator end) public unsafe string GetAsciiString(ref MemoryPoolIterator end)
{ {
if (IsDefault || end.IsDefault) var block = _block;
if (block == null || end.IsDefault)
{ {
return null; return null;
} }
@ -1080,8 +1080,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
return null; return null;
} }
var inputOffset = Index; var inputOffset = _index;
var block = Block;
var asciiString = new string('\0', length); var asciiString = new string('\0', length);
@ -1124,13 +1123,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
public string GetUtf8String(ref MemoryPoolIterator end) public string GetUtf8String(ref MemoryPoolIterator end)
{ {
if (IsDefault || end.IsDefault) var block = _block;
if (block == null || end.IsDefault)
{ {
return default(string); 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(); var decoder = Encoding.UTF8.GetDecoder();
@ -1141,8 +1143,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
var chars = new char[charLength]; var chars = new char[charLength];
var charIndex = 0; var charIndex = 0;
var block = Block;
var index = Index;
var remaining = length; var remaining = length;
while (true) while (true)
{ {
@ -1204,13 +1204,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ArraySegment<byte> GetArraySegment(MemoryPoolIterator end) public ArraySegment<byte> GetArraySegment(MemoryPoolIterator end)
{ {
if (IsDefault || end.IsDefault) var block = _block;
if (block == null || end.IsDefault)
{ {
return default(ArraySegment<byte>); return default(ArraySegment<byte>);
} }
if (end.Block == Block)
var index = _index;
if (end.Block == block)
{ {
return new ArraySegment<byte>(Block.Array, Index, end.Index - Index); return new ArraySegment<byte>(block.Array, index, end.Index - index);
} }
return GetArraySegmentMultiBlock(ref end); return GetArraySegmentMultiBlock(ref end);

View File

@ -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<byte>)));
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<byte>));
default(MemoryPoolIterator).CopyFromAscii("");
Assert.Equal(default(MemoryPoolIterator).GetLength(end), -1);
Assert.ThrowsAny<InvalidOperationException>(() => default(MemoryPoolIterator).Skip(1));
}
[Theory] [Theory]
[InlineData("a", "a", 1)] [InlineData("a", "a", 1)]
[InlineData("ab", "a...", 1)] [InlineData("ab", "a...", 1)]