diff options
author | William Godbe <wigodbe@microsoft.com> | 2018-03-01 09:01:06 +0300 |
---|---|---|
committer | Dan Moseley <danmose@microsoft.com> | 2018-03-01 09:01:06 +0300 |
commit | 09b4ad86c48485426feb2536d7d48dd36ba9c884 (patch) | |
tree | 10b262942bd668d587a4ad440e90dc952f67df10 /src/System.IO.FileSystem/tests | |
parent | ec2e02aca14595c16c58f6aec6239823245fb98b (diff) |
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
Diffstat (limited to 'src/System.IO.FileSystem/tests')
-rw-r--r-- | src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs b/src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs index fa1f45095d..a80eb79fbb 100644 --- a/src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs +++ b/src/System.IO.FileSystem/tests/FileStream/WriteAsync.cs @@ -388,35 +388,30 @@ namespace System.IO.Tests string writeFileName = GetTestFilePath(); do { - // Create a new token that expires between 100-1000ms - CancellationTokenSource tokenSource = new CancellationTokenSource(); - tokenSource.CancelAfter(rand.Next(100, 1000)); + + int totalBytesWritten = 0; using (var stream = new FileStream(writeFileName, FileMode.Create, FileAccess.Write)) { do { - try + // 20%: random write size + int bytesToWrite = (rand.NextDouble() < 0.2 ? rand.Next(16, MaximumWriteSize) : NormalWriteSize); + + if (rand.NextDouble() < 0.1) { - // 20%: random write size - int bytesToWrite = (rand.NextDouble() < 0.2 ? rand.Next(16, MaximumWriteSize) : NormalWriteSize); - - if (rand.NextDouble() < 0.1) - { - // 10%: Sync write - stream.Write(dataToWrite, 0, bytesToWrite); - } - else - { - // 90%: Async write - await WriteAsync(stream, dataToWrite, 0, bytesToWrite, tokenSource.Token); - } + // 10%: Sync write + stream.Write(dataToWrite, 0, bytesToWrite); } - catch (TaskCanceledException) + else { - Assert.True(tokenSource.Token.IsCancellationRequested, "Received cancellation exception before token expired"); + // 90%: Async write + await WriteAsync(stream, dataToWrite, 0, bytesToWrite); } - } while (!tokenSource.Token.IsCancellationRequested); + + totalBytesWritten += bytesToWrite; + // Cap written bytes at 10 million to avoid writing too much to disk + } while (totalBytesWritten < 10_000_000); } } while (DateTime.UtcNow - testStartTime <= testRunTime); } |