[PT Run] Async the OnRename to unblock thread (#37987)

* Rename name to async and try catch to handle exception.

* Fix ut

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
This commit is contained in:
moooyo
2025-03-18 15:51:34 +08:00
committed by GitHub
parent a298f67b81
commit abd6314b2e
2 changed files with 37 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Plugin.Program.Storage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@@ -114,7 +114,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
[DataTestMethod]
[DataRow("directory", "oldpath.appref-ms", "newpath.appref-ms")]
public void Win32ProgramRepositoryMustCallOnAppRenamedForApprefAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
public async Task Win32ProgramRepositoryMustCallOnAppRenamedForApprefAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
{
// Arrange
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
@@ -130,6 +130,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
// Act
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
// We need to wait more than one second to make sure our test can pass
await Task.Delay(2 * Win32ProgramRepository.OnRenamedEventWaitTime).ConfigureAwait(false);
// Assert
Assert.AreEqual(1, win32ProgramRepository.Count());
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
@@ -183,7 +186,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
[DataTestMethod]
[DataRow("directory", "oldpath.appref-ms", "newpath.appref-ms")]
public void Win32ProgramRepositoryMustCallOnAppRenamedForExeAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
public async Task Win32ProgramRepositoryMustCallOnAppRenamedForExeAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
{
// Arrange
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
@@ -204,6 +207,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
// Act
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
// We need to wait more than one second to make sure our test can pass
await Task.Delay(2 * Win32ProgramRepository.OnRenamedEventWaitTime).ConfigureAwait(false);
// Assert
Assert.AreEqual(1, win32ProgramRepository.Count());
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
@@ -287,7 +293,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
[DataTestMethod]
[DataRow("directory", "oldpath.url", "newpath.url")]
public void Win32ProgramRepositoryMustCallOnAppRenamedForUrlAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
public async Task Win32ProgramRepositoryMustCallOnAppRenamedForUrlAppsWhenRenamedEventIsRaised(string directory, string oldpath, string newpath)
{
// Arrange
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
@@ -308,6 +314,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
// Act
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
// We need to wait more than one second to make sure our test can pass
await Task.Delay(2 * Win32ProgramRepository.OnRenamedEventWaitTime).ConfigureAwait(false);
// Assert
Assert.AreEqual(1, win32ProgramRepository.Count());
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
@@ -347,7 +356,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
[DataTestMethod]
[DataRow("directory", "oldpath.lnk", "path.lnk")]
public void Win32ProgramRepositoryMustCallOnAppRenamedForLnkAppsWhenRenamedEventIsRaised(string directory, string oldpath, string path)
public async Task Win32ProgramRepositoryMustCallOnAppRenamedForLnkAppsWhenRenamedEventIsRaised(string directory, string oldpath, string path)
{
// Arrange
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
@@ -382,6 +391,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
// Act
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
// We need to wait more than one second to make sure our test can pass
await Task.Delay(2 * Win32ProgramRepository.OnRenamedEventWaitTime).ConfigureAwait(false);
// Assert
Assert.AreEqual(1, win32ProgramRepository.Count());
Assert.IsTrue(win32ProgramRepository.Contains(newitem));