-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Added IAsyncDisposable to TextReader #89477
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e1c8dfd
added IAsyncDisposable to TextReader. Implemnted IAsyncDisposable for…
dersia 4c3b8cf
fixed comment on StreamReader.CloseStreamFromDispose
dersia 888f957
Reverted Change to StreamWriter.CloseStreamFromDispose
dersia a4e615c
Fixed some Typos. Removed bool disposing from AsyncMethods. Removed u…
dersia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,13 @@ public static void TestNullStream_Dispose() | |
| Stream.Null.Dispose(); // Dispose shouldn't have any side effects | ||
| } | ||
|
|
||
| [Fact] | ||
| public static async Task TestNullStream_DisposeAsync() | ||
| { | ||
| await Stream.Null.DisposeAsync(); | ||
| await Stream.Null.DisposeAsync(); // Dispose shouldn't have any side effects | ||
| } | ||
|
|
||
| [Fact] | ||
| public static async Task TestNullStream_CopyTo() | ||
| { | ||
|
|
@@ -133,6 +140,16 @@ public static void TestNullTextReaderDispose(TextReader input) | |
| Assert.Equal("", input.ReadToEnd()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static async Task TestNullTextReaderDisposeAsync(TextReader input) | ||
| { | ||
| // dispose should be a no-op | ||
| await input.DisposeAsync(); | ||
| await input.DisposeAsync(); | ||
| Assert.Equal("", input.ReadToEnd()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static void TestNullTextReader(TextReader input) | ||
|
|
@@ -172,6 +189,22 @@ public static async Task TestNullTextReaderAsync(TextReader input) | |
| input.Dispose(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static async Task TestNullTextReaderAsync_DisposeAsync(TextReader input) | ||
| { | ||
| var chars = new char[2]; | ||
| Assert.Equal(0, await input.ReadAsync(chars, 0, chars.Length)); | ||
| Assert.Equal(0, await input.ReadAsync(chars.AsMemory(), default)); | ||
| Assert.Equal(0, await input.ReadBlockAsync(chars, 0, chars.Length)); | ||
| Assert.Equal(0, await input.ReadBlockAsync(chars.AsMemory(), default)); | ||
| Assert.Null(await input.ReadLineAsync()); | ||
| Assert.Null(await input.ReadLineAsync(default)); | ||
| Assert.Equal("", await input.ReadToEndAsync()); | ||
| Assert.Equal("", await input.ReadToEndAsync(default)); | ||
| await input.DisposeAsync(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static async Task TestCanceledNullTextReaderAsync(TextReader input) | ||
|
|
@@ -192,6 +225,76 @@ public static async Task TestCanceledNullTextReaderAsync(TextReader input) | |
| input.Dispose(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static async Task TestCanceledNullTextReaderAsync_DisposeAsync(TextReader input) | ||
| { | ||
| using CancellationTokenSource tokenSource = new CancellationTokenSource(); | ||
| tokenSource.Cancel(); | ||
| var token = tokenSource.Token; | ||
| var chars = new char[2]; | ||
| OperationCanceledException ex; | ||
| ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await input.ReadAsync(chars.AsMemory(), token)); | ||
| Assert.Equal(token, ex.CancellationToken); | ||
| ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await input.ReadBlockAsync(chars.AsMemory(), token)); | ||
| Assert.Equal(token, ex.CancellationToken); | ||
| ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await input.ReadLineAsync(token)); | ||
| Assert.Equal(token, ex.CancellationToken); | ||
| ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await input.ReadToEndAsync(token)); | ||
| Assert.Equal(token, ex.CancellationToken); | ||
| await input.DisposeAsync(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static void TestNullTextReader_Disposed(TextReader input) | ||
| { | ||
| StreamReader sr = input as StreamReader; | ||
|
|
||
| input.Dispose(); | ||
|
|
||
| if (sr != null) | ||
| Assert.True(sr.EndOfStream, "EndOfStream property didn't return true"); | ||
| Assert.Null(input.ReadLine()); | ||
| if (sr != null) | ||
| Assert.True(sr.EndOfStream, "EndOfStream property didn't return true"); | ||
|
|
||
| Assert.Equal(-1, input.Read()); | ||
| Assert.Equal(-1, input.Peek()); | ||
| var chars = new char[2]; | ||
| Assert.Equal(0, input.Read(chars, 0, chars.Length)); | ||
| Assert.Equal(0, input.Read(chars.AsSpan())); | ||
| Assert.Equal(0, input.ReadBlock(chars, 0, chars.Length)); | ||
| Assert.Equal(0, input.ReadBlock(chars.AsSpan())); | ||
| Assert.Equal("", input.ReadToEnd()); | ||
| input.Dispose(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullReaders))] | ||
| public static async Task TestNullTextReader_DisposedAsync(TextReader input) | ||
| { | ||
| StreamReader sr = input as StreamReader; | ||
|
|
||
| await input.DisposeAsync(); | ||
|
|
||
| if (sr != null) | ||
| Assert.True(sr.EndOfStream, "EndOfStream property didn't return true"); | ||
| Assert.Null(input.ReadLine()); | ||
| if (sr != null) | ||
| Assert.True(sr.EndOfStream, "EndOfStream property didn't return true"); | ||
|
|
||
| Assert.Equal(-1, input.Read()); | ||
| Assert.Equal(-1, input.Peek()); | ||
| var chars = new char[2]; | ||
| Assert.Equal(0, await input.ReadAsync(chars, 0, chars.Length)); | ||
| Assert.Equal(0, input.Read(chars.AsSpan())); | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Assert.Equal(0, await input.ReadBlockAsync(chars, 0, chars.Length)); | ||
| Assert.Equal(0, input.ReadBlock(chars.AsSpan())); | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Assert.Equal("", await input.ReadToEndAsync()); | ||
| await input.DisposeAsync(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullWriters))] | ||
| public static void TextNullTextWriter(TextWriter output) | ||
|
|
@@ -206,6 +309,20 @@ public static void TextNullTextWriter(TextWriter output) | |
| output.Dispose(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullWriters))] | ||
| public static async Task TextNullTextWriterAsync(TextWriter output) | ||
| { | ||
| await output.FlushAsync(); | ||
| await output.DisposeAsync(); | ||
|
|
||
| await output.WriteLineAsync(decimal.MinValue.ToString()); | ||
| await output.WriteLineAsync(Math.PI.ToString()); | ||
| await output.WriteLineAsync(output.NewLine); | ||
| await output.FlushAsync(); | ||
| await output.DisposeAsync(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(NullStream_ReadWriteData))] | ||
| public void TestNullStream_ReadSpan(byte[] buffer, int offset, int count) | ||
|
|
||
77 changes: 77 additions & 0 deletions
77
src/libraries/System.IO/tests/StreamReader/StreamReader.DisposeAsync.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace System.IO.Tests | ||
| { | ||
| public class StreamReader_disposeAsyncTest | ||
| { | ||
|
|
||
| [Fact] | ||
| public void DisposeAsync_CanInvokeMultipleTimes() | ||
| { | ||
| var ms = new MemoryStream(); | ||
| var sr = new StreamReader(ms); | ||
| Assert.True(sr.DisposeAsync().IsCompletedSuccessfully); | ||
| Assert.True(sr.DisposeAsync().IsCompletedSuccessfully); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DisposeAsync_CanDisposeAsyncAfterDispose() | ||
| { | ||
| var ms = new MemoryStream(); | ||
| var sr = new StreamReader(ms); | ||
| sr.Dispose(); | ||
| Assert.True(sr.DisposeAsync().IsCompletedSuccessfully); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_LeaveOpenTrue_LeftOpen() | ||
| { | ||
| var ms = new MemoryStream(); | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var sr = new StreamReader(ms, Encoding.ASCII, false, 0x1000, leaveOpen: true); | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await sr.DisposeAsync(); | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Assert.Equal(0, ms.Position); // doesn't throw | ||
dersia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_DerivedTypeForcesDisposeToBeUsedUnlessOverridden() | ||
| { | ||
| var ms = new MemoryStream(); | ||
| var sr = new OverrideDisposeStreamReader(ms); | ||
| Assert.False(sr.DisposeInvoked); | ||
| await sr.DisposeAsync(); | ||
| Assert.True(sr.DisposeInvoked); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_DerivedTypeDisposeAsyncInvoked() | ||
| { | ||
| var ms = new MemoryStream(); | ||
| var sr = new OverrideDisposeAndDisposeAsyncStreamReader(ms); | ||
| Assert.False(sr.DisposeInvoked); | ||
| Assert.False(sr.DisposeAsyncInvoked); | ||
| await sr.DisposeAsync(); | ||
| Assert.False(sr.DisposeInvoked); | ||
| Assert.True(sr.DisposeAsyncInvoked); | ||
| } | ||
|
|
||
| private sealed class OverrideDisposeStreamReader : StreamReader | ||
| { | ||
| public bool DisposeInvoked; | ||
| public OverrideDisposeStreamReader(Stream output) : base(output) { } | ||
| protected override void Dispose(bool disposing) => DisposeInvoked = true; | ||
| } | ||
|
|
||
| private sealed class OverrideDisposeAndDisposeAsyncStreamReader : StreamReader | ||
| { | ||
| public bool DisposeInvoked, DisposeAsyncInvoked; | ||
| public OverrideDisposeAndDisposeAsyncStreamReader(Stream output) : base(output) { } | ||
| protected override void Dispose(bool disposing) => DisposeInvoked = true; | ||
| public override ValueTask DisposeAsync() { DisposeAsyncInvoked = true; return default; } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add this line to the existing test above and remove this one test to avoid duplication?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these to methods are testing different behavior, since Dispose is calling "Close()" on the underlying stream and DisposeAsync() is calling "DisposeAsync()" on the underlying stream. But if this isn't a concern I will happily change this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jozkee what do you think we should do here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think you should apply my suggestion here because
TestNullTextReaderAsyncabove is already testing Async conterparts.