Skip to content

Commit

Permalink
skip the ReadToEndAsync_WithCancellation test if there is not enough …
Browse files Browse the repository at this point in the history
…disk space (#66397)

* skip the ReadToEndAsync_WithCancellation test if there is not enough disk space to create 1GB test file

* move the test to OuterLoop
  • Loading branch information
adamsitnik authored Mar 9, 2022
1 parent 4e75015 commit 0e8422d
Showing 1 changed file with 29 additions and 4 deletions.
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

0 comments on commit 0e8422d

Please sign in to comment.