2016-11-09 07:23:13 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NuGet.Common;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static partial class FileInfoExtensions
|
|
|
|
|
{
|
|
|
|
|
private class FileInfoNuGetLock : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
|
|
|
|
|
|
private Task _task;
|
|
|
|
|
|
|
|
|
|
public FileInfoNuGetLock(FileInfo fileInfo)
|
|
|
|
|
{
|
2016-12-13 22:15:35 +00:00
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>();
|
|
|
|
|
|
2016-11-09 07:23:13 +00:00
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
2016-12-13 22:15:35 +00:00
|
|
|
|
_task = Task.Run(async () => await ConcurrencyUtilities.ExecuteWithFileLockedAsync<int>(
|
2016-11-09 07:23:13 +00:00
|
|
|
|
fileInfo.FullName,
|
2016-12-13 22:15:35 +00:00
|
|
|
|
cancellationToken =>
|
2016-11-09 07:23:13 +00:00
|
|
|
|
{
|
2016-12-13 22:15:35 +00:00
|
|
|
|
taskCompletionSource.SetResult("Lock is taken so test can continue");
|
|
|
|
|
|
|
|
|
|
Task.Delay(60000, cancellationToken).Wait();
|
2016-11-09 07:23:13 +00:00
|
|
|
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
},
|
2016-12-13 22:15:35 +00:00
|
|
|
|
_cancellationTokenSource.Token));
|
|
|
|
|
|
|
|
|
|
taskCompletionSource.Task.Wait();
|
2016-11-09 07:23:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_task.Wait();
|
|
|
|
|
}
|
|
|
|
|
catch (AggregateException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|