diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs index 47147428379d5..9d6dc5ef6225c 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Threading.Tasks; using Microsoft.Win32.SafeHandles; using Xunit; @@ -91,15 +92,34 @@ protected override DateTime GetLastWriteTimeUtc(string path) return File.GetLastWriteTimeUtc(fileHandle); } + [Fact] + public async Task WritingShouldUpdateWriteTime_After_SetLastAccessTime() + { + string filePath = GetTestFilePath(); + using var handle = OpenFileHandle(filePath, FileAccess.ReadWrite); + + File.SetLastAccessTime(handle, DateTime.Now.Subtract(TimeSpan.FromDays(1))); + var 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); + + var timeAfterWrite = File.GetLastWriteTime(handle); + Assert.True(timeAfterWrite > timeBeforeWrite); + } + [Fact] public void NullArgumentValidation() { Assert.Throws("fileHandle", static () => File.GetCreationTime(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetCreationTime(default(SafeFileHandle)!, DateTime.Now)); - + Assert.Throws("fileHandle", static () => File.GetCreationTimeUtc(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetCreationTimeUtc(default(SafeFileHandle)!, DateTime.Now)); - + Assert.Throws("fileHandle", static () => File.GetLastAccessTime(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetLastAccessTime(default(SafeFileHandle)!, DateTime.Now));