Checking that the lock file exists before trying to acquire the lock for it. (#4797)
* Checking that the lock file exists before trying to acquire a lock for it, which takes up to 30 seconds. * Adding a test for failing when reading the lock file and it does not exists.
This commit is contained in:
parent
2cf0675270
commit
9862fbb4d7
2 changed files with 58 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -19,6 +20,14 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
|
|
||||||
public static async Task<LockFile> ReadWithLock(this LockFileFormat subject, string path)
|
public static async Task<LockFile> ReadWithLock(this LockFileFormat subject, string path)
|
||||||
{
|
{
|
||||||
|
if(!File.Exists(path))
|
||||||
|
{
|
||||||
|
throw new GracefulException(string.Join(
|
||||||
|
Environment.NewLine,
|
||||||
|
$"File not found `{path}`.",
|
||||||
|
"The project may not have been restored or restore failed - run `dotnet restore`"));
|
||||||
|
}
|
||||||
|
|
||||||
return await ConcurrencyUtilities.ExecuteWithFileLockedAsync(
|
return await ConcurrencyUtilities.ExecuteWithFileLockedAsync(
|
||||||
path,
|
path,
|
||||||
lockedToken =>
|
lockedToken =>
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
// 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.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Microsoft.DotNet.Cli;
|
||||||
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
|
using Microsoft.DotNet.InternalAbstractions;
|
||||||
|
using Microsoft.DotNet.TestFramework;
|
||||||
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||||
|
using NuGet.Frameworks;
|
||||||
|
using NuGet.ProjectModel;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
||||||
|
{
|
||||||
|
public class GivenThatWeWantToReadLockFilesQuickly : TestBase
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ItFailsInLessThanOneSecondWhenTheProjectAssetsJsonDoesNotExist()
|
||||||
|
{
|
||||||
|
var testInstance = TestAssets.Get("TestAppWithProjDepTool")
|
||||||
|
.CreateInstance()
|
||||||
|
.WithSourceFiles();
|
||||||
|
|
||||||
|
var assetsFile = testInstance.Root.GetDirectory("obj").GetFile("project.assets.json").FullName;
|
||||||
|
var expectedMessage = string.Join(
|
||||||
|
Environment.NewLine,
|
||||||
|
$"File not found `{assetsFile}`.",
|
||||||
|
"The project may not have been restored or restore failed - run `dotnet restore`");
|
||||||
|
|
||||||
|
Action action = () =>
|
||||||
|
{
|
||||||
|
var lockFile = new LockFileFormat()
|
||||||
|
.ReadWithLock(assetsFile)
|
||||||
|
.Result;
|
||||||
|
};
|
||||||
|
|
||||||
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
|
action.ShouldThrow<GracefulException>().WithMessage(expectedMessage);
|
||||||
|
|
||||||
|
stopWatch.Stop();
|
||||||
|
stopWatch.ElapsedMilliseconds.Should().BeLessThan(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue