mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[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:
@@ -7,7 +7,7 @@ using System.Diagnostics;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Abstractions;
|
using System.IO.Abstractions;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Plugin.Program.Storage;
|
using Microsoft.Plugin.Program.Storage;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using Moq;
|
using Moq;
|
||||||
@@ -114,7 +114,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow("directory", "oldpath.appref-ms", "newpath.appref-ms")]
|
[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
|
// Arrange
|
||||||
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
||||||
@@ -130,6 +130,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
// Act
|
// Act
|
||||||
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
|
_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
|
||||||
Assert.AreEqual(1, win32ProgramRepository.Count());
|
Assert.AreEqual(1, win32ProgramRepository.Count());
|
||||||
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
||||||
@@ -183,7 +186,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow("directory", "oldpath.appref-ms", "newpath.appref-ms")]
|
[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
|
// Arrange
|
||||||
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
||||||
@@ -204,6 +207,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
// Act
|
// Act
|
||||||
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
|
_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
|
||||||
Assert.AreEqual(1, win32ProgramRepository.Count());
|
Assert.AreEqual(1, win32ProgramRepository.Count());
|
||||||
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
||||||
@@ -287,7 +293,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow("directory", "oldpath.url", "newpath.url")]
|
[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
|
// Arrange
|
||||||
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
||||||
@@ -308,6 +314,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
// Act
|
// Act
|
||||||
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
|
_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
|
||||||
Assert.AreEqual(1, win32ProgramRepository.Count());
|
Assert.AreEqual(1, win32ProgramRepository.Count());
|
||||||
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
||||||
@@ -347,7 +356,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow("directory", "oldpath.lnk", "path.lnk")]
|
[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
|
// Arrange
|
||||||
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
Win32ProgramRepository win32ProgramRepository = new Win32ProgramRepository(_fileSystemWatchers, _settings, _pathsToWatch);
|
||||||
@@ -382,6 +391,9 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|||||||
// Act
|
// Act
|
||||||
_fileSystemMocks[0].Raise(m => m.Renamed += null, e);
|
_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
|
||||||
Assert.AreEqual(1, win32ProgramRepository.Count());
|
Assert.AreEqual(1, win32ProgramRepository.Count());
|
||||||
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
Assert.IsTrue(win32ProgramRepository.Contains(newitem));
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ namespace Microsoft.Plugin.Program.Storage
|
|||||||
|
|
||||||
private static ConcurrentQueue<string> commonEventHandlingQueue = new ConcurrentQueue<string>();
|
private static ConcurrentQueue<string> commonEventHandlingQueue = new ConcurrentQueue<string>();
|
||||||
|
|
||||||
|
public static readonly int OnRenamedEventWaitTime = 1000;
|
||||||
|
|
||||||
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, ProgramPluginSettings settings, string[] pathsToWatch)
|
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, ProgramPluginSettings settings, string[] pathsToWatch)
|
||||||
{
|
{
|
||||||
_fileSystemWatcherHelpers = fileSystemWatcherHelpers;
|
_fileSystemWatcherHelpers = fileSystemWatcherHelpers;
|
||||||
@@ -91,7 +93,7 @@ namespace Microsoft.Plugin.Program.Storage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnAppRenamed(object sender, RenamedEventArgs e)
|
private async Task DoOnAppRenamedAsync(object sender, RenamedEventArgs e)
|
||||||
{
|
{
|
||||||
string oldPath = e.OldFullPath;
|
string oldPath = e.OldFullPath;
|
||||||
string newPath = e.FullPath;
|
string newPath = e.FullPath;
|
||||||
@@ -100,7 +102,7 @@ namespace Microsoft.Plugin.Program.Storage
|
|||||||
// the msi installer creates a shortcut, which is detected by the PT Run and ends up in calling this OnAppRenamed method
|
// the msi installer creates a shortcut, which is detected by the PT Run and ends up in calling this OnAppRenamed method
|
||||||
// the thread needs to be halted for a short time to avoid locking the new shortcut file as we read it, otherwise the lock causes
|
// the thread needs to be halted for a short time to avoid locking the new shortcut file as we read it, otherwise the lock causes
|
||||||
// in the issue scenario that a warning is popping up during the msi install process.
|
// in the issue scenario that a warning is popping up during the msi install process.
|
||||||
System.Threading.Thread.Sleep(1000);
|
await Task.Delay(OnRenamedEventWaitTime).ConfigureAwait(false);
|
||||||
|
|
||||||
string extension = Path.GetExtension(newPath);
|
string extension = Path.GetExtension(newPath);
|
||||||
Win32Program.ApplicationType oldAppType = Win32Program.GetAppTypeFromPath(oldPath);
|
Win32Program.ApplicationType oldAppType = Win32Program.GetAppTypeFromPath(oldPath);
|
||||||
@@ -124,7 +126,7 @@ namespace Microsoft.Plugin.Program.Storage
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Exception($"OnAppRenamed-{extension} Program|{e.OldName}|Unable to create program from {oldPath}", ex, GetType());
|
Log.Exception($"DoOnAppRenamedAsync-{extension} Program|{e.OldName}|Unable to create program from {oldPath}", ex, GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
// To remove the old app which has been renamed and to add the new application.
|
// To remove the old app which has been renamed and to add the new application.
|
||||||
@@ -146,6 +148,21 @@ namespace Microsoft.Plugin.Program.Storage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnAppRenamed(object sender, RenamedEventArgs e)
|
||||||
|
{
|
||||||
|
Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DoOnAppRenamedAsync(sender, e).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception($"OnAppRenamed throw exception.", e, e.GetType());
|
||||||
|
}
|
||||||
|
}).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnAppDeleted(object sender, FileSystemEventArgs e)
|
private void OnAppDeleted(object sender, FileSystemEventArgs e)
|
||||||
{
|
{
|
||||||
string path = e.FullPath;
|
string path = e.FullPath;
|
||||||
|
|||||||
Reference in New Issue
Block a user