Skip to content

Call StartAsync in CompleteAsync #24058

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 5 commits into from
Jul 20, 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
7 changes: 6 additions & 1 deletion src/Http/Http/src/StreamResponseBodyFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public virtual Task StartAsync(CancellationToken cancellationToken = default)
}

/// <summary>
/// This calls StartAsync if it has not previoulsy been called.
/// This calls StartAsync if it has not previously been called.
/// It will complete the adapted pipe if it exists.
/// </summary>
/// <returns></returns>
Expand All @@ -128,6 +128,11 @@ public virtual async Task CompleteAsync()
return;
}

if (!_started)
{
await StartAsync();
}

_completed = true;

if (_pipeWriter != null)
Expand Down
62 changes: 62 additions & 0 deletions src/Http/Http/test/Features/StreamResponseBodyFeatureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Buffers;
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.AspNetCore.Http.Features
{
public class StreamResponseBodyFeatureTests
{
[Fact]
public async Task CompleteAsyncCallsStartAsync()
{
// Arrange
var stream = new MemoryStream();
var streamResponseBodyFeature = new TestStreamResponseBodyFeature(stream);

// Act
await streamResponseBodyFeature.CompleteAsync();

//Assert
Assert.Equal(1, streamResponseBodyFeature.StartCalled);
}

[Fact]
public async Task CompleteAsyncWontCallsStartAsyncIfAlreadyStarted()
{
// Arrange
var stream = new MemoryStream();
var streamResponseBodyFeature = new TestStreamResponseBodyFeature(stream);
await streamResponseBodyFeature.StartAsync();

// Act
await streamResponseBodyFeature.CompleteAsync();

//Assert
Assert.Equal(1, streamResponseBodyFeature.StartCalled);
}
}

public class TestStreamResponseBodyFeature : StreamResponseBodyFeature
{
public TestStreamResponseBodyFeature(Stream stream)
: base(stream)
{

}

public override Task StartAsync(CancellationToken cancellationToken = default)
{
StartCalled++;
return base.StartAsync(cancellationToken);
}

public int StartCalled { get; private set; }
}
}