Skip to content

Undo use of pipewriter in FileBufferingWriteStream #21833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ protected override void Dispose(bool disposing) { }
[System.Diagnostics.DebuggerStepThroughAttribute]
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
[System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task DrainBufferAsync(System.IO.Pipelines.PipeWriter destination, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
[System.Diagnostics.DebuggerStepThroughAttribute]
public System.Threading.Tasks.Task DrainBufferAsync(System.IO.Stream destination, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override void Flush() { }
public override System.Threading.Tasks.Task FlushAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
Expand Down
19 changes: 1 addition & 18 deletions src/Http/WebUtilities/src/FileBufferingWriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,8 @@ public async Task DrainBufferAsync(Stream destination, CancellationToken cancell
// unspooled content. Copy the FileStream content first when available.
if (FileStream != null)
{
// We make a new stream for async reads from disk and async writes to the destination
await using var readStream = new FileStream(FileStream.Name, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite, bufferSize: 1, useAsync: true);

await readStream.CopyToAsync(destination, cancellationToken);

// This is created with delete on close
await FileStream.DisposeAsync();
FileStream = null;
}
await FileStream.FlushAsync(cancellationToken);

await PagedByteBuffer.MoveToAsync(destination, cancellationToken);
}

public async Task DrainBufferAsync(PipeWriter destination, CancellationToken cancellationToken = default)
{
// When not null, FileStream always has "older" spooled content. The PagedByteBuffer always has "newer"
// unspooled content. Copy the FileStream content first when available.
if (FileStream != null)
{
// We make a new stream for async reads from disk and async writes to the destination
await using var readStream = new FileStream(FileStream.Name, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite, bufferSize: 1, useAsync: true);

Expand Down
18 changes: 18 additions & 0 deletions src/Http/WebUtilities/test/FileBufferingWriteStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@ public async Task DrainBufferAsync_WithContentInDisk_CopiesContentFromMemoryStre
Assert.Equal(0, bufferingStream.Length);
}

[Fact]
public async Task DrainBufferAsync_IncludesContentPossiblyBufferedByFileStream()
{
// We want to ensure that the FileStream (which has a 1-byte buffer) flushes prior to the other read stream reading input.
// Arrange
var input = new byte[] { 3, };
using var bufferingStream = new FileBufferingWriteStream(0, tempFileDirectoryAccessor: () => TempDirectory);
bufferingStream.Write(input, 0, input.Length);
var memoryStream = new MemoryStream();

// Act
await bufferingStream.DrainBufferAsync(memoryStream, default);

// Assert
Assert.Equal(input, memoryStream.ToArray());
Assert.Equal(0, bufferingStream.Length);
}

public void Dispose()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
if (fileBufferingWriteStream != null)
{
response.ContentLength = fileBufferingWriteStream.Length;
await fileBufferingWriteStream.DrainBufferAsync(response.BodyWriter);
await fileBufferingWriteStream.DrainBufferAsync(response.Body);
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
if (fileBufferingWriteStream != null)
{
response.ContentLength = fileBufferingWriteStream.Length;
await fileBufferingWriteStream.DrainBufferAsync(response.BodyWriter);
await fileBufferingWriteStream.DrainBufferAsync(response.Body);
}
}
finally
Expand Down
10 changes: 6 additions & 4 deletions src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,17 @@ public virtual async Task Formatting_DictionaryType()
Assert.Equal(expected, await response.Content.ReadAsStringAsync());
}

[Fact]
public virtual async Task Formatting_LargeObject()
[Theory]
[InlineData(65 * 1024)]
[InlineData(2 * 1024 * 1024)]
public virtual async Task Formatting_LargeObject(int size)
{
// Arrange
var expectedName = "This is long so we can test large objects " + new string('a', 1024 * 65);
var expectedName = "This is long so we can test large objects " + new string('a', size);
var expected = $"{{\"id\":10,\"name\":\"{expectedName}\",\"streetName\":null}}";

// Act
var response = await Client.GetAsync($"/JsonOutputFormatter/{nameof(JsonOutputFormatterController.LargeObjectResult)}");
var response = await Client.GetAsync($"/JsonOutputFormatter/{nameof(JsonOutputFormatterController.LargeObjectResult)}/{size}");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public ActionResult<Dictionary<string, string>> DictionaryResult() =>
["Key3"] = null,
};

[HttpGet]
public ActionResult<SimpleModel> LargeObjectResult() =>
[HttpGet("{size:int}")]
public ActionResult<SimpleModel> LargeObjectResult(int size) =>
new SimpleModel
{
Id = 10,
Name = "This is long so we can test large objects " + new string('a', 1024 * 65),
Name = "This is long so we can test large objects " + new string('a', size),
};

[HttpGet]
Expand Down