Make Dispose not throw in StreamPipeWriter (#7376)

This commit is contained in:
Justin Kotalik 2019-02-08 08:44:34 -08:00 committed by GitHub
parent 3bd5f2c2ab
commit 9e5f09cb44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 15 deletions

View File

@ -129,20 +129,7 @@ namespace System.IO.Pipelines
return;
}
_isCompleted = true;
_internalTokenSource?.Dispose();
if (_completedSegments != null)
{
foreach (var segment in _completedSegments)
{
segment.Return();
}
}
_currentSegmentOwner?.Dispose();
Dispose();
// We still want to cleanup segments before throwing an exception.
if (_bytesWritten > 0 && exception == null)
{
@ -286,7 +273,24 @@ namespace System.IO.Pipelines
public void Dispose()
{
Complete();
if (_isCompleted)
{
return;
}
_isCompleted = true;
_internalTokenSource?.Dispose();
if (_completedSegments != null)
{
foreach (var segment in _completedSegments)
{
segment.Return();
}
}
_currentSegmentOwner?.Dispose();
}
/// <summary>

View File

@ -339,6 +339,25 @@ namespace System.IO.Pipelines.Tests
Assert.Throws<InvalidOperationException>(() => Writer.GetSpan());
}
[Fact]
public void DisposeDoesNotThrowIfUnflushedData()
{
var streamPipeWriter = new StreamPipeWriter(new MemoryStream());
streamPipeWriter.Write(new byte[1]);
streamPipeWriter.Dispose();
}
[Fact]
public void CompleteAfterDisposeDoesNotThrowIfUnflushedData()
{
var streamPipeWriter = new StreamPipeWriter(new MemoryStream());
streamPipeWriter.Write(new byte[1]);
streamPipeWriter.Dispose();
streamPipeWriter.Complete();
}
[Fact]
public void CallGetMemoryWithNegativeSizeHint_ThrowsArgException()
{