Skip to content

Commit

Permalink
Only dispose the stream pipe wrapper and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotalik committed Feb 8, 2019
1 parent 1a7b2c2 commit 67023fa
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ PipeWriter IResponseBodyPipeFeature.ResponseBodyPipe
{
var responseBody = new WriteOnlyPipeStream(ResponsePipeWriter);
ResponseBody = responseBody;

RegisterWrapperForDisposal(responseBody);
_cachedResponsePipeWriter = ResponsePipeWriter;
}
}
}
Expand All @@ -225,18 +224,15 @@ Stream IHttpResponseFeature.Body
var responsePipeWriter = new StreamPipeWriter(ResponseBody, minimumSegmentSize: 4096, _context.MemoryPool);
ResponsePipeWriter = responsePipeWriter;
_cachedResponseBodyStream = ResponseBody;
RegisterWrapperForDisposal(responsePipeWriter);
}
}
}

private void RegisterWrapperForDisposal(IDisposable responseBody)
{
if (_wrapperObjectsToDispose == null)
{
_wrapperObjectsToDispose = new List<IDisposable>();
// The StreamPipeWrapper needs to be disposed as it hold onto blocks of memory
if (_wrapperObjectsToDispose == null)
{
_wrapperObjectsToDispose = new List<IDisposable>();
}
_wrapperObjectsToDispose.Add(responsePipeWriter);
}
}
_wrapperObjectsToDispose.Add(responseBody);
}

protected void ResetHttp1Features()
Expand Down
68 changes: 68 additions & 0 deletions src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3517,6 +3517,74 @@ await connection.Receive(
}
}

[Fact]
public async Task ResponseSetBodyToSameValueTwiceGetPipeMultipleTimesSameObject()
{
using (var server = new TestServer(async httpContext =>
{
var memoryStream = new MemoryStream();
httpContext.Response.Body = memoryStream;
var bodyPipe1 = httpContext.Response.BodyPipe;
httpContext.Response.Body = memoryStream;
var bodyPipe2 = httpContext.Response.BodyPipe;
Assert.Equal(bodyPipe1, bodyPipe2);
await Task.CompletedTask;
}, new TestServiceContext(LoggerFactory)))
{
using (var connection = server.CreateConnection())
{
await connection.Send(
"GET / HTTP/1.1",
"Host:",
"",
"");
await connection.Receive(
"HTTP/1.1 200 OK",
$"Date: {server.Context.DateHeaderValue}",
"Content-Length: 0",
"",
"");
}
await server.StopAsync();
}
}

[Fact]
public async Task ResponseSetPipeToSameValueTwiceGetBodyMultipleTimesSameObject()
{
using (var server = new TestServer(async httpContext =>
{
var pipeWriter = new Pipe().Writer;
httpContext.Response.BodyPipe = pipeWriter;
var body1 = httpContext.Response.Body;
httpContext.Response.BodyPipe = pipeWriter;
var body2 = httpContext.Response.Body;
Assert.Equal(body1, body2);
await Task.CompletedTask;
}, new TestServiceContext(LoggerFactory)))
{
using (var connection = server.CreateConnection())
{
await connection.Send(
"GET / HTTP/1.1",
"Host:",
"",
"");
await connection.Receive(
"HTTP/1.1 200 OK",
$"Date: {server.Context.DateHeaderValue}",
"Content-Length: 0",
"",
"");
}
await server.StopAsync();
}
}

[Fact]
public async Task ResponseSetPipeAndBodyPipeIsWrapped()
{
Expand Down

0 comments on commit 67023fa

Please sign in to comment.