Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 09b4ad8

Browse files
wtgodbedanmoseley
authored andcommitted
Stop FileSystem WriteAsync test from writing too much data to disk (#27387)
* Stop FileSystem WriteAsync test from writing too much data to disk * Fix TotalBytesWritten * Move bytes written cap to inner loop * Fix do/while syntax, add cancellation token back * Remove token
1 parent ec2e02a commit 09b4ad8

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,35 +388,30 @@ public async Task WriteAsyncMiniStress()
388388
string writeFileName = GetTestFilePath();
389389
do
390390
{
391-
// Create a new token that expires between 100-1000ms
392-
CancellationTokenSource tokenSource = new CancellationTokenSource();
393-
tokenSource.CancelAfter(rand.Next(100, 1000));
391+
392+
int totalBytesWritten = 0;
394393

395394
using (var stream = new FileStream(writeFileName, FileMode.Create, FileAccess.Write))
396395
{
397396
do
398397
{
399-
try
398+
// 20%: random write size
399+
int bytesToWrite = (rand.NextDouble() < 0.2 ? rand.Next(16, MaximumWriteSize) : NormalWriteSize);
400+
401+
if (rand.NextDouble() < 0.1)
400402
{
401-
// 20%: random write size
402-
int bytesToWrite = (rand.NextDouble() < 0.2 ? rand.Next(16, MaximumWriteSize) : NormalWriteSize);
403-
404-
if (rand.NextDouble() < 0.1)
405-
{
406-
// 10%: Sync write
407-
stream.Write(dataToWrite, 0, bytesToWrite);
408-
}
409-
else
410-
{
411-
// 90%: Async write
412-
await WriteAsync(stream, dataToWrite, 0, bytesToWrite, tokenSource.Token);
413-
}
403+
// 10%: Sync write
404+
stream.Write(dataToWrite, 0, bytesToWrite);
414405
}
415-
catch (TaskCanceledException)
406+
else
416407
{
417-
Assert.True(tokenSource.Token.IsCancellationRequested, "Received cancellation exception before token expired");
408+
// 90%: Async write
409+
await WriteAsync(stream, dataToWrite, 0, bytesToWrite);
418410
}
419-
} while (!tokenSource.Token.IsCancellationRequested);
411+
412+
totalBytesWritten += bytesToWrite;
413+
// Cap written bytes at 10 million to avoid writing too much to disk
414+
} while (totalBytesWritten < 10_000_000);
420415
}
421416
} while (DateTime.UtcNow - testStartTime <= testRunTime);
422417
}

0 commit comments

Comments
 (0)