[release/6.0] Don't create multiple large files at the same time (#63032)
authorDavid Cantú <dacantu@microsoft.com>
Fri, 31 Dec 2021 04:10:51 +0000 (20:10 -0800)
committerGitHub <noreply@github.com>
Fri, 31 Dec 2021 04:10:51 +0000 (21:10 -0700)
* Move NoInt32OverflowInTheBufferingLogic to OuterLoop and address pending feedback (#60606)

* 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

* Fix DisableParallelization build issue

* Apply suggestions from code review

Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
* Update src/libraries/System.IO.FileSystem/tests/LargeFileTests.cs

Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
* Notepad with uppercase (#62487)

Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
Co-authored-by: Dan Moseley <danmose@microsoft.com>
src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytes.cs
src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytesAsync.cs
src/libraries/System.IO.FileSystem/tests/FileStream/Read.cs
src/libraries/System.IO.FileSystem/tests/LargeFileTests.cs [new file with mode: 0644]
src/libraries/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj
src/libraries/System.IO/tests/BufferedStream/BufferedStreamTests.cs

index 3806277..6f304b0 100644 (file)
@@ -1318,7 +1318,7 @@ namespace System.Diagnostics.Tests
             string expected = Path.GetFileNameWithoutExtension(filename);
 
             process.WaitForInputIdle(); // Give the file a chance to load
-            Assert.Equal("notepad", process.ProcessName);
+            Assert.Equal("notepad", process.ProcessName.ToLower());
 
             // Notepad calls CreateWindowEx with pWindowName of empty string, then calls SetWindowTextW
             // with "Untitled - Notepad" then finally if you're opening a file, calls SetWindowTextW
index 880da43..71e27ea 100644 (file)
@@ -267,11 +267,16 @@ namespace System.Diagnostics.Tests
                 {
                     if (px != null) // sometimes process is null
                     {
-                        Assert.Equal("notepad", px.ProcessName);
-
-                        px.Kill();
-                        Assert.True(px.WaitForExit(WaitInMS));
-                        px.WaitForExit(); // wait for event handlers to complete
+                        try
+                        {
+                            Assert.Equal("notepad", px.ProcessName.ToLower());
+                        }
+                        finally
+                        {
+                            px.Kill();
+                            Assert.True(px.WaitForExit(WaitInMS));
+                            px.WaitForExit(); // wait for event handlers to complete
+                        }
                     }
                 }
             }
index 5dd2e3f..a7815dd 100644 (file)
@@ -58,21 +58,6 @@ namespace System.IO.Tests
         }
 
         [Fact]
-        [OuterLoop]
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
-        public void ReadFileOver2GB()
-        {
-            string path = GetTestFilePath();
-            using (FileStream fs = File.Create(path))
-            {
-                fs.SetLength(int.MaxValue + 1L);
-            }
-
-            // File is too large for ReadAllBytes at once
-            Assert.Throws<IOException>(() => File.ReadAllBytes(path));
-        }
-
-        [Fact]
         public void Overwrite()
         {
             string path = GetTestFilePath();
index be562f1..c4f147c 100644 (file)
@@ -6,7 +6,6 @@ using System.Threading;
 using System.Threading.Tasks;
 using Xunit;
 using System.IO.Pipes;
-using Microsoft.DotNet.XUnitExtensions;
 
 namespace System.IO.Tests
 {
@@ -71,21 +70,6 @@ namespace System.IO.Tests
         }
 
         [Fact]
-        [OuterLoop]
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
-        public Task ReadFileOver2GBAsync()
-        {
-            string path = GetTestFilePath();
-            using (FileStream fs = File.Create(path))
-            {
-                fs.SetLength(int.MaxValue + 1L);
-            }
-
-            // File is too large for ReadAllBytes at once
-            return Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(path));
-        }
-
-        [Fact]
         public async Task OverwriteAsync()
         {
             string path = GetTestFilePath();
index fec3a7a..b6c5559 100644 (file)
@@ -14,49 +14,5 @@ namespace System.IO.Tests
             Assert.Throws<UnauthorizedAccessException>(() =>
                 new FileStream(Path.GetPathRoot(Directory.GetCurrentDirectory()), FileMode.Open, FileAccess.Read));
         }
-
-        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
-        [ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
-        public void NoInt32OverflowInTheBufferingLogic()
-        {
-            const long position1 = 10;
-            const long position2 = (1L << 32) + position1;
-
-            string filePath = GetTestFilePath();
-            byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
-            byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
-            byte[] buffer = new byte[5];
-
-            using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
-            {
-                stream.Seek(position1, SeekOrigin.Begin);
-                stream.Write(data1, 0, data1.Length);
-
-                stream.Seek(position2, SeekOrigin.Begin);
-                stream.Write(data2, 0, data2.Length);
-            }
-
-            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
-            {
-                stream.Seek(position1, SeekOrigin.Begin);
-                Assert.Equal(buffer.Length, stream.Read(buffer));
-                Assert.Equal(data1, buffer);
-
-                stream.Seek(position2, SeekOrigin.Begin);
-                Assert.Equal(buffer.Length, stream.Read(buffer));
-                Assert.Equal(data2, buffer);
-            }
-
-            using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0)))
-            {
-                stream.Seek(position1, SeekOrigin.Begin);
-                Assert.Equal(buffer.Length, stream.Read(buffer));
-                Assert.Equal(data1, buffer);
-
-                stream.Seek(position2, SeekOrigin.Begin);
-                Assert.Equal(buffer.Length, stream.Read(buffer));
-                Assert.Equal(data2, buffer);
-            }
-        }
     }
 }
diff --git a/src/libraries/System.IO.FileSystem/tests/LargeFileTests.cs b/src/libraries/System.IO.FileSystem/tests/LargeFileTests.cs
new file mode 100644 (file)
index 0000000..c8158f8
--- /dev/null
@@ -0,0 +1,64 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.IO.Tests;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace System.IO.FileSystem.Tests
+{
+    [OuterLoop]
+    [ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
+    [Collection(nameof(NoParallelTests))] // don't create multiple large files at the same time
+    public class LargeFileTests : FileSystemTest
+    {
+        [Fact]
+        public async Task ReadAllBytesOverLimit()
+        {
+            using FileStream fs = new (GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.DeleteOnClose);
+
+            foreach (long lengthOverLimit in new long[] { int.MaxValue + 1L })
+            {
+                fs.SetLength(lengthOverLimit);
+
+                Assert.Throws<IOException>(() => File.ReadAllBytes(fs.Name));
+                await Assert.ThrowsAsync<IOException>(async () => await File.ReadAllBytesAsync(fs.Name));
+            }
+        }
+
+        [Fact]
+        public void NoInt32OverflowInTheBufferingLogic()
+        {
+            const long position1 = 10;
+            const long position2 = (1L << 32) + position1;
+
+            string filePath = GetTestFilePath();
+            byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
+            byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
+            byte[] buffer = new byte[5];
+
+            using (FileStream stream = File.Create(filePath))
+            {
+                stream.Seek(position1, SeekOrigin.Begin);
+                stream.Write(data1);
+
+                stream.Seek(position2, SeekOrigin.Begin);
+                stream.Write(data2);
+            }
+
+            using (FileStream stream = new (filePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose))
+            {
+                stream.Seek(position1, SeekOrigin.Begin);
+                Assert.Equal(buffer.Length, stream.Read(buffer));
+                Assert.Equal(data1, buffer);
+
+                stream.Seek(position2, SeekOrigin.Begin);
+                Assert.Equal(buffer.Length, stream.Read(buffer));
+                Assert.Equal(data2, buffer);
+            }
+        }
+    }
+
+    [CollectionDefinition(nameof(NoParallelTests), DisableParallelization = true)]
+    public partial class NoParallelTests { }
+}
index ea17b39..1ffb30f 100644 (file)
@@ -55,6 +55,7 @@
     <Compile Include="Enumeration\ExampleTests.cs" />
     <Compile Include="Enumeration\RemovedDirectoryTests.cs" />
     <Compile Include="Enumeration\SymbolicLinksTests.cs" />
+    <Compile Include="LargeFileTests.cs" />
     <Compile Include="PathInternalTests.cs" />
     <Compile Include="RandomAccess\Base.cs" />
     <Compile Include="RandomAccess\GetLength.cs" />
index 0826875..8d11c80 100644 (file)
@@ -323,6 +323,40 @@ namespace System.IO.Tests
             Array.Copy(data, 1, expected, 0, expected.Length);
             Assert.Equal(expected, dst.ToArray());
         }
+
+        [Fact]
+        [OuterLoop]
+        [ActiveIssue("https://github.com/dotnet/runtime/issues/45954", TestPlatforms.Browser)]
+        public void NoInt32OverflowInTheBufferingLogic()
+        {
+            const long position1 = 10;
+            const long position2 = (1L << 32) + position1;
+
+            string filePath = Path.GetTempFileName();
+            byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
+            byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
+            byte[] buffer = new byte[5];
+
+            using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
+            {
+                stream.Seek(position1, SeekOrigin.Begin);
+                stream.Write(data1);
+
+                stream.Seek(position2, SeekOrigin.Begin);
+                stream.Write(data2);
+            }
+
+            using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0, FileOptions.DeleteOnClose)))
+            {
+                stream.Seek(position1, SeekOrigin.Begin);
+                Assert.Equal(buffer.Length, stream.Read(buffer));
+                Assert.Equal(data1, buffer);
+
+                stream.Seek(position2, SeekOrigin.Begin);
+                Assert.Equal(buffer.Length, stream.Read(buffer));
+                Assert.Equal(data2, buffer);
+            }
+        }
     }
 
     public class BufferedStream_TestLeaveOpen : TestLeaveOpen