Skip to content

Commit 03194f7

Browse files
authored
Don't create multiple large files at the same time (#62519)
* move existing large file tests into a separate type (no code changes) * don't run the large file tests in parallel * use FileOptions.DeleteOnClose to ensure that each test removes it's own file use single large file to test File.ReadAllBytes and ile.ReadAllBytesAsync for both limits
1 parent 8d8ebca commit 03194f7

File tree

6 files changed

+63
-96
lines changed

6 files changed

+63
-96
lines changed

src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytes.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,6 @@ public void ValidWrite(int size)
5757
File.Delete(path);
5858
}
5959

60-
[Fact]
61-
[OuterLoop]
62-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
63-
public void ReadFileOver2GB()
64-
{
65-
string path = GetTestFilePath();
66-
using (FileStream fs = File.Create(path))
67-
{
68-
fs.SetLength(int.MaxValue + 1L);
69-
}
70-
71-
// File is too large for ReadAllBytes at once
72-
Assert.Throws<IOException>(() => File.ReadAllBytes(path));
73-
}
74-
75-
[Fact]
76-
[OuterLoop]
77-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
78-
public void ReadFileOverMaxArrayLength()
79-
{
80-
string path = GetTestFilePath();
81-
using (FileStream fs = File.Create(path))
82-
{
83-
fs.SetLength(Array.MaxLength + 1L);
84-
}
85-
86-
// File is too large for ReadAllBytes at once
87-
Assert.Throws<IOException>(() => File.ReadAllBytes(path));
88-
}
89-
9060
[Fact]
9161
public void Overwrite()
9262
{

src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytesAsync.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using Xunit;
88
using System.IO.Pipes;
9-
using Microsoft.DotNet.XUnitExtensions;
109

1110
namespace System.IO.Tests
1211
{
@@ -70,36 +69,6 @@ public Task AlreadyCanceledAsync()
7069
async () => await File.WriteAllBytesAsync(path, new byte[0], token));
7170
}
7271

73-
[Fact]
74-
[OuterLoop]
75-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
76-
public async Task ReadFileOver2GBAsync()
77-
{
78-
string path = GetTestFilePath();
79-
using (FileStream fs = File.Create(path))
80-
{
81-
fs.SetLength(int.MaxValue + 1L);
82-
}
83-
84-
// File is too large for ReadAllBytesAsync at once
85-
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(path));
86-
}
87-
88-
[Fact]
89-
[OuterLoop]
90-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
91-
public async Task ReadFileOverMaxArrayLengthAsync()
92-
{
93-
string path = GetTestFilePath();
94-
using (FileStream fs = File.Create(path))
95-
{
96-
fs.SetLength(Array.MaxLength + 1L);
97-
}
98-
99-
// File is too large for ReadAllBytesAsync at once
100-
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(path));
101-
}
102-
10372
[Fact]
10473
public async Task OverwriteAsync()
10574
{

src/libraries/System.IO.FileSystem/tests/FileStream/Read.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,5 @@ public void NegativeReadRootThrows()
1414
Assert.Throws<UnauthorizedAccessException>(() =>
1515
new FileStream(Path.GetPathRoot(Directory.GetCurrentDirectory()), FileMode.Open, FileAccess.Read));
1616
}
17-
18-
[Fact]
19-
[OuterLoop]
20-
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
21-
public void NoInt32OverflowInTheBufferingLogic()
22-
{
23-
const long position1 = 10;
24-
const long position2 = (1L << 32) + position1;
25-
26-
string filePath = GetTestFilePath();
27-
byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
28-
byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
29-
byte[] buffer = new byte[5];
30-
31-
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
32-
{
33-
stream.Seek(position1, SeekOrigin.Begin);
34-
stream.Write(data1);
35-
36-
stream.Seek(position2, SeekOrigin.Begin);
37-
stream.Write(data2);
38-
}
39-
40-
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
41-
{
42-
stream.Seek(position1, SeekOrigin.Begin);
43-
Assert.Equal(buffer.Length, stream.Read(buffer));
44-
Assert.Equal(data1, buffer);
45-
46-
stream.Seek(position2, SeekOrigin.Begin);
47-
Assert.Equal(buffer.Length, stream.Read(buffer));
48-
Assert.Equal(data2, buffer);
49-
}
50-
}
5117
}
5218
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO.Tests;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace System.IO.FileSystem.Tests
9+
{
10+
[OuterLoop]
11+
[ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
12+
[Collection(nameof(DisableParallelization))] // don't create multiple large files at the same time
13+
public class LargeFileTests : FileSystemTest
14+
{
15+
[Fact]
16+
public async Task ReadAllBytesOverLimit()
17+
{
18+
using FileStream fs = new (GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.DeleteOnClose);
19+
20+
foreach (long lengthOverLimit in new long[] { Array.MaxLength + 1L, int.MaxValue + 1L })
21+
{
22+
fs.SetLength(lengthOverLimit);
23+
24+
Assert.Throws<IOException>(() => File.ReadAllBytes(fs.Name));
25+
await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(fs.Name));
26+
}
27+
}
28+
29+
[Fact]
30+
public void NoInt32OverflowInTheBufferingLogic()
31+
{
32+
const long position1 = 10;
33+
const long position2 = (1L << 32) + position1;
34+
35+
string filePath = GetTestFilePath();
36+
byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
37+
byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
38+
byte[] buffer = new byte[5];
39+
40+
using (FileStream stream = File.Create(filePath))
41+
{
42+
stream.Seek(position1, SeekOrigin.Begin);
43+
stream.Write(data1);
44+
45+
stream.Seek(position2, SeekOrigin.Begin);
46+
stream.Write(data2);
47+
}
48+
49+
using (FileStream stream = new (filePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose))
50+
{
51+
stream.Seek(position1, SeekOrigin.Begin);
52+
Assert.Equal(buffer.Length, stream.Read(buffer));
53+
Assert.Equal(data1, buffer);
54+
55+
stream.Seek(position2, SeekOrigin.Begin);
56+
Assert.Equal(buffer.Length, stream.Read(buffer));
57+
Assert.Equal(data2, buffer);
58+
}
59+
}
60+
}
61+
}

src/libraries/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="Enumeration\ExampleTests.cs" />
5757
<Compile Include="Enumeration\RemovedDirectoryTests.cs" />
5858
<Compile Include="Enumeration\SymbolicLinksTests.cs" />
59+
<Compile Include="LargeFileTests.cs" />
5960
<Compile Include="PathInternalTests.cs" />
6061
<Compile Include="RandomAccess\Base.cs" />
6162
<Compile Include="RandomAccess\GetLength.cs" />

src/libraries/System.IO/tests/BufferedStream/BufferedStreamTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public void NoInt32OverflowInTheBufferingLogic()
346346
stream.Write(data2);
347347
}
348348

349-
using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0)))
349+
using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0, FileOptions.DeleteOnClose)))
350350
{
351351
stream.Seek(position1, SeekOrigin.Begin);
352352
Assert.Equal(buffer.Length, stream.Read(buffer));

0 commit comments

Comments
 (0)