2020-09-25 10:36:38 -07:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Concurrent;
|
2020-10-20 11:13:53 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2020-09-25 10:36:38 -07:00
|
|
|
|
using Microsoft.Plugin.Program.Storage;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Plugin.Program.UnitTests.Storage
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class ConcurrentQueueEventHandlerTest
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestCase]
|
2020-10-20 11:13:53 -07:00
|
|
|
|
public async Task EventHandlerMustReturnEmptyPathForEmptyQueueAsync()
|
2020-09-25 10:36:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
int dequeueDelay = 0;
|
|
|
|
|
|
ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-10-20 11:13:53 -07:00
|
|
|
|
string appPath = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.IsEmpty(appPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestCase(1)]
|
|
|
|
|
|
[TestCase(10)]
|
2020-10-20 11:13:53 -07:00
|
|
|
|
public async Task EventHandlerMustReturnPathForConcurrentQueueWithSameFilePathsAsync(int itemCount)
|
2020-09-25 10:36:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
int dequeueDelay = 0;
|
|
|
|
|
|
string appPath = "appPath";
|
|
|
|
|
|
ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>();
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(appPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-10-20 11:13:53 -07:00
|
|
|
|
string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
2020-10-07 22:12:59 +02:00
|
|
|
|
Assert.AreEqual(appPath, pathFromQueue);
|
|
|
|
|
|
Assert.AreEqual(0, eventHandlingQueue.Count);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestCase(5)]
|
2020-10-20 11:13:53 -07:00
|
|
|
|
public async Task EventHandlerMustReturnPathAndRetainDifferentFilePathsInQueueAsync(int itemCount)
|
2020-09-25 10:36:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
int dequeueDelay = 0;
|
|
|
|
|
|
string firstAppPath = "appPath1";
|
|
|
|
|
|
string secondAppPath = "appPath2";
|
|
|
|
|
|
ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>();
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(firstAppPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(secondAppPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-10-20 11:13:53 -07:00
|
|
|
|
string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
2020-10-07 22:12:59 +02:00
|
|
|
|
Assert.AreEqual(firstAppPath, pathFromQueue);
|
|
|
|
|
|
Assert.AreEqual(itemCount, eventHandlingQueue.Count);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TestCase(5)]
|
2020-10-20 11:13:53 -07:00
|
|
|
|
public async Task EventHandlerMustReturnPathAndRetainAllPathsAfterEncounteringADifferentPathAsync(int itemCount)
|
2020-09-25 10:36:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
int dequeueDelay = 0;
|
|
|
|
|
|
string firstAppPath = "appPath1";
|
|
|
|
|
|
string secondAppPath = "appPath2";
|
|
|
|
|
|
ConcurrentQueue<string> eventHandlingQueue = new ConcurrentQueue<string>();
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(firstAppPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(secondAppPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
eventHandlingQueue.Enqueue(firstAppPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2020-10-20 11:13:53 -07:00
|
|
|
|
string pathFromQueue = await EventHandler.GetAppPathFromQueueAsync(eventHandlingQueue, dequeueDelay).ConfigureAwait(false);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
2020-10-07 22:12:59 +02:00
|
|
|
|
Assert.AreEqual(firstAppPath, pathFromQueue);
|
|
|
|
|
|
Assert.AreEqual(itemCount * 2, eventHandlingQueue.Count);
|
2020-09-25 10:36:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|