Skip to content

skip the ReadToEndAsync_WithCancellation test if there is not enough disk space #66397

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 2 commits into from
Mar 9, 2022
Merged
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
33 changes: 29 additions & 4 deletions src/libraries/System.IO/tests/StreamReader/StreamReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.IO.Tests
Expand Down Expand Up @@ -131,17 +132,16 @@ public async Task ReadToEndAsync_WithCanceledCancellationToken()
Assert.Equal(token, ex.CancellationToken);
}

[OuterLoop("It creates 1GB file")]
[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser.")]
public async Task ReadToEndAsync_WithCancellation()
{
string path = GetTestFilePath();

// create large (~1GB) file
File.WriteAllLines(path, Enumerable.Repeat("A very large file used for testing StreamReader cancellation. 0123456789012345678901234567890123456789.", 10_000_000));
CreateLargeFile(path);

using StreamReader reader = File.OpenText(path);
using CancellationTokenSource cts = new ();
using CancellationTokenSource cts = new();
var token = cts.Token;

var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
Expand All @@ -162,6 +162,31 @@ public async Task ReadToEndAsync_WithCancellation()
Assert.Equal(token, ex.CancellationToken);
}

private void CreateLargeFile(string path)
{
const string sentence = "A very large file used for testing StreamReader cancellation. 0123456789012345678901234567890123456789.";
const int repeatCount = 10_000_000;
Encoding encoding = Encoding.UTF8;

using FileStream fs = File.OpenWrite(path);
long fileSize = encoding.GetByteCount(sentence) * repeatCount;

try
{
fs.SetLength(fileSize);
}
catch (IOException)
{
throw new SkipTestException($"Unable to run {ReadToEndAsync_WithCancellation} due to lack of available disk space");
}

using StreamWriter streamWriter = new StreamWriter(fs, encoding);
for (int i = 0; i < repeatCount; i++)
{
streamWriter.WriteLine(sentence);
}
}

[Fact]
public void GetBaseStream()
{
Expand Down