Skip to content

Commit e734d1c

Browse files
authored
Use TempFile in a test instead of Path.GetTempFileName() (#68576)
Addresses #68171 (comment)
1 parent 151a737 commit e734d1c

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<Compile Include="RegisteredWaitTests.cs" />
1010
</ItemGroup>
1111
<ItemGroup>
12+
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs"
13+
Link="CommonTest\System\IO\TempFile.cs" />
1214
<Compile Include="$(CommonTestPath)System\Threading\ThreadTestHelpers.cs"
1315
Link="CommonTest\System\Threading\ThreadTestHelpers.cs" />
1416
</ItemGroup>

src/libraries/System.Threading.ThreadPool/tests/ThreadPoolTests.cs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,49 +1034,31 @@ public void FileStreamFlushAsyncThreadPoolDeadlockTest()
10341034
const int FourKibibytes = OneKibibyte << 2;
10351035
const int FileSize = 1024;
10361036

1037-
string destinationFilePath = null;
1038-
try
1039-
{
1040-
destinationFilePath = CreateFileWithRandomContent(FileSize);
1041-
1042-
static string CreateFileWithRandomContent(int fileSize)
1043-
{
1044-
string filePath = Path.GetTempFileName();
1045-
File.WriteAllBytes(filePath, CreateArray(fileSize));
1046-
return filePath;
1047-
}
1048-
1049-
static byte[] CreateArray(int count)
1050-
{
1051-
var result = new byte[count];
1052-
const int Seed = 12345;
1053-
var random = new Random(Seed);
1054-
random.NextBytes(result);
1055-
return result;
1056-
}
1037+
using var destinationTempFile = TempFile.Create(CreateArray(FileSize));
10571038

1058-
for (int j = 0; j < 100; j++)
1059-
{
1060-
using var fileStream =
1061-
new FileStream(
1062-
destinationFilePath,
1063-
FileMode.Create,
1064-
FileAccess.Write,
1065-
FileShare.Read,
1066-
FourKibibytes,
1067-
FileOptions.None);
1068-
for (int i = 0; i < FileSize; i++)
1069-
{
1070-
fileStream.WriteByte(default);
1071-
await fileStream.FlushAsync();
1072-
}
1073-
}
1039+
static byte[] CreateArray(int count)
1040+
{
1041+
var result = new byte[count];
1042+
const int Seed = 12345;
1043+
var random = new Random(Seed);
1044+
random.NextBytes(result);
1045+
return result;
10741046
}
1075-
finally
1047+
1048+
for (int j = 0; j < 100; j++)
10761049
{
1077-
if (!string.IsNullOrEmpty(destinationFilePath) && File.Exists(destinationFilePath))
1050+
using var fileStream =
1051+
new FileStream(
1052+
destinationTempFile.Path,
1053+
FileMode.Create,
1054+
FileAccess.Write,
1055+
FileShare.Read,
1056+
FourKibibytes,
1057+
FileOptions.None);
1058+
for (int i = 0; i < FileSize; i++)
10781059
{
1079-
File.Delete(destinationFilePath);
1060+
fileStream.WriteByte(default);
1061+
await fileStream.FlushAsync();
10801062
}
10811063
}
10821064
}).Dispose();

0 commit comments

Comments
 (0)