Use task delay instead of thread sleep (#7401)

* Use task delay instead of thread.sleep to free up the blocked thread

* fix formatting
This commit is contained in:
Alekhya
2020-10-20 11:13:53 -07:00
committed by GitHub
parent 58cf165eb4
commit 14e74376d9
3 changed files with 15 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.Plugin.Program.Storage; using Microsoft.Plugin.Program.Storage;
using NUnit.Framework; using NUnit.Framework;
@@ -12,14 +13,14 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
public class ConcurrentQueueEventHandlerTest public class ConcurrentQueueEventHandlerTest
{ {
[TestCase] [TestCase]
public void EventHandlerMustReturnEmptyPathForEmptyQueue() public async Task EventHandlerMustReturnEmptyPathForEmptyQueueAsync()
{ {
// Arrange // Arrange
int dequeueDelay = 0; int dequeueDelay = 0;
ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>(); ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>();
// Act // Act
string appPath = EventHandler.GetAppPathFromQueue(eventHandlingQueue, dequeueDelay); string appPath = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
// Assert // Assert
Assert.IsEmpty(appPath); Assert.IsEmpty(appPath);
@@ -27,7 +28,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
[TestCase(1)] [TestCase(1)]
[TestCase(10)] [TestCase(10)]
public void EventHandlerMustReturnPathForConcurrentQueueWithSameFilePaths(int itemCount) public async Task EventHandlerMustReturnPathForConcurrentQueueWithSameFilePathsAsync(int itemCount)
{ {
// Arrange // Arrange
int dequeueDelay = 0; int dequeueDelay = 0;
@@ -39,7 +40,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
} }
// Act // Act
string pathFromQueue = EventHandler.GetAppPathFromQueue(eventHandlingQueue, dequeueDelay); string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
// Assert // Assert
Assert.AreEqual(appPath, pathFromQueue); Assert.AreEqual(appPath, pathFromQueue);
@@ -47,7 +48,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
} }
[TestCase(5)] [TestCase(5)]
public void EventHandlerMustReturnPathAndRetainDifferentFilePathsInQueue(int itemCount) public async Task EventHandlerMustReturnPathAndRetainDifferentFilePathsInQueueAsync(int itemCount)
{ {
// Arrange // Arrange
int dequeueDelay = 0; int dequeueDelay = 0;
@@ -65,7 +66,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
} }
// Act // Act
string pathFromQueue = EventHandler.GetAppPathFromQueue(eventHandlingQueue, dequeueDelay); string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
// Assert // Assert
Assert.AreEqual(firstAppPath, pathFromQueue); Assert.AreEqual(firstAppPath, pathFromQueue);
@@ -73,7 +74,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
} }
[TestCase(5)] [TestCase(5)]
public void EventHandlerMustReturnPathAndRetainAllPathsAfterEncounteringADifferentPath(int itemCount) public async Task EventHandlerMustReturnPathAndRetainAllPathsAfterEncounteringADifferentPathAsync(int itemCount)
{ {
// Arrange // Arrange
int dequeueDelay = 0; int dequeueDelay = 0;
@@ -96,7 +97,7 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
} }
// Act // Act
string pathFromQueue = EventHandler.GetAppPathFromQueue(eventHandlingQueue, dequeueDelay); string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
// Assert // Assert
Assert.AreEqual(firstAppPath, pathFromQueue); Assert.AreEqual(firstAppPath, pathFromQueue);

View File

@@ -4,7 +4,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading; using System.Threading.Tasks;
namespace Microsoft.Plugin.Program.Storage namespace Microsoft.Plugin.Program.Storage
{ {
@@ -12,7 +12,7 @@ namespace Microsoft.Plugin.Program.Storage
{ {
// To obtain the path of the app when multiple events are added to the Concurrent queue across multiple threads. // To obtain the path of the app when multiple events are added to the Concurrent queue across multiple threads.
// On the first occurence of a different file path, the existing app path is to be returned without removing any more elements from the queue. // On the first occurence of a different file path, the existing app path is to be returned without removing any more elements from the queue.
public static string GetAppPathFromQueue(ConcurrentQueue<string> eventHandlingQueue, int dequeueDelay) public static async Task<string> GetAppPathFromQueueAsync(ConcurrentQueue<string> eventHandlingQueue, int dequeueDelay)
{ {
if (eventHandlingQueue == null) if (eventHandlingQueue == null)
{ {
@@ -36,7 +36,7 @@ namespace Microsoft.Plugin.Program.Storage
} }
// This delay has been added to account for the delay in events being triggered during app installation. // This delay has been added to account for the delay in events being triggered during app installation.
Thread.Sleep(dequeueDelay); await Task.Delay(dequeueDelay).ConfigureAwait(false);
} }
return previousAppPath; return previousAppPath;

View File

@@ -8,7 +8,6 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
@@ -40,15 +39,15 @@ namespace Microsoft.Plugin.Program.Storage
InitializeFileSystemWatchers(); InitializeFileSystemWatchers();
// This task would always run in the background trying to dequeue file paths from the queue at regular intervals. // This task would always run in the background trying to dequeue file paths from the queue at regular intervals.
Task.Run(() => _ = Task.Run(async () =>
{ {
while (true) while (true)
{ {
int dequeueDelay = 500; int dequeueDelay = 500;
string appPath = EventHandler.GetAppPathFromQueue(commonEventHandlingQueue, dequeueDelay); string appPath = await EventHandler.GetAppPathFromQueueAsync(commonEventHandlingQueue, dequeueDelay).ConfigureAwait(false);
// To allow for the installation process to finish. // To allow for the installation process to finish.
Thread.Sleep(5000); await Task.Delay(5000).ConfigureAwait(false);
if (!string.IsNullOrEmpty(appPath)) if (!string.IsNullOrEmpty(appPath))
{ {