Clean up pipes usage in TestHost

This commit is contained in:
Chris Ross (ASP.NET) 2018-04-10 12:46:04 -07:00
parent 78904e70c7
commit d69798d2c1
1 changed files with 12 additions and 26 deletions

View File

@ -108,36 +108,22 @@ namespace Microsoft.AspNetCore.TestHost
CheckAborted(); CheckAborted();
var registration = cancellationToken.Register(Cancel); var registration = cancellationToken.Register(Cancel);
try try
{
// TODO: Usability issue. dotnet/corefx#27732 Flush or zero byte write causes ReadAsync to complete without data so I have to call ReadAsync in a loop.
while (true)
{ {
var result = await _pipe.Reader.ReadAsync(cancellationToken); var result = await _pipe.Reader.ReadAsync(cancellationToken);
var readableBuffer = result.Buffer; if (result.Buffer.IsEmpty && result.IsCompleted)
if (!readableBuffer.IsEmpty)
{ {
_pipe.Reader.Complete();
return 0;
}
var readableBuffer = result.Buffer;
var actual = Math.Min(readableBuffer.Length, count); var actual = Math.Min(readableBuffer.Length, count);
readableBuffer = readableBuffer.Slice(0, actual); readableBuffer = readableBuffer.Slice(0, actual);
readableBuffer.CopyTo(new Span<byte>(buffer, offset, count)); readableBuffer.CopyTo(new Span<byte>(buffer, offset, count));
_pipe.Reader.AdvanceTo(readableBuffer.End, readableBuffer.End); _pipe.Reader.AdvanceTo(readableBuffer.End, readableBuffer.End);
return (int)actual; return (int)actual;
} }
if (result.IsCompleted)
{
_pipe.Reader.AdvanceTo(readableBuffer.End, readableBuffer.End); // TODO: Remove after https://github.com/dotnet/corefx/pull/27596
_pipe.Reader.Complete();
return 0;
}
cancellationToken.ThrowIfCancellationRequested();
Debug.Assert(!result.IsCanceled); // It should only be canceled by cancellationToken.
// Try again. TODO: dotnet/corefx#27732 I shouldn't need to do this, there wasn't any data.
_pipe.Reader.AdvanceTo(readableBuffer.End, readableBuffer.End);
}
}
finally finally
{ {
registration.Dispose(); registration.Dispose();