Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make WritingShouldUpdateWriteTime_After_SetLastAccessTime more resilient #95336

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,23 @@ protected override DateTime GetLastWriteTimeUtc(string path)
public async Task WritingShouldUpdateWriteTime_After_SetLastAccessTime()
{
string filePath = GetTestFilePath();
using var handle = OpenFileHandle(filePath, FileAccess.ReadWrite);
using SafeFileHandle handle = OpenFileHandle(filePath, FileAccess.ReadWrite);

File.SetLastAccessTime(handle, DateTime.Now.Subtract(TimeSpan.FromDays(1)));
var timeBeforeWrite = File.GetLastWriteTime(handle);
DateTime timeAfterWrite = default, timeBeforeWrite = File.GetLastWriteTime(handle);

using var writer = new StreamWriter(new FileStream(handle, FileAccess.ReadWrite));
writer.AutoFlush = true;
writer.WriteLine("now: " + DateTime.Now);
await Task.Delay(2000);
writer.WriteLine("now: " + DateTime.Now);
// According to https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-win32_file_attribute_data
// write time has a resolution of 2 seconds on FAT. Let's wait a little bit longer.
jozkee marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i <= 5 && timeBeforeWrite >= timeAfterWrite; i++)
{
await Task.Delay(TimeSpan.FromSeconds(i));

var timeAfterWrite = File.GetLastWriteTime(handle);
Assert.True(timeAfterWrite > timeBeforeWrite);
await RandomAccess.WriteAsync(handle, new byte[1] { 1 }, fileOffset: i);

timeAfterWrite = File.GetLastWriteTime(handle);
}

AssertExtensions.GreaterThan(timeAfterWrite, timeBeforeWrite);
}

[Fact]
Expand Down