Call StartAsync in CompleteAsync (#24058)

This commit is contained in:
Kahbazi 2020-07-20 19:44:51 +04:30 committed by GitHub
parent 0889a62250
commit 4573f6894e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 1 deletions

View File

@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.Http
}
/// <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>
@ -128,6 +128,11 @@ namespace Microsoft.AspNetCore.Http
return;
}
if (!_started)
{
await StartAsync();
}
_completed = true;
if (_pipeWriter != null)

View File

@ -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; }
}
}