-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix a logic error that caused AbandonedMutexException while executing…
… migrations (#4859)
- Loading branch information
1 parent
a32bce3
commit 3a7670c
Showing
6 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace NuGet.Common | ||
{ | ||
internal static class StringExtensions | ||
{ | ||
internal static string FormatWithDoubleQuotes(this string s) | ||
{ | ||
return s == null ? s : $@"""{s}"""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Common.Migrations; | ||
using Xunit; | ||
|
||
namespace NuGet.Common.Test | ||
{ | ||
[CollectionDefinition("MigrationRunner", DisableParallelization = true)] | ||
public class MigrationRunnerTests | ||
{ | ||
[Fact] | ||
public void WhenExecutedInParallelOnlyOneFileIsCreatedForEveryMigration_Success() | ||
{ | ||
var threads = new List<Thread>(); | ||
int numThreads = 5; | ||
int timeoutInSeconds = 90; | ||
|
||
// Arrange | ||
string directory = MigrationRunner.GetMigrationsDirectory(); | ||
if (Directory.Exists(directory)) | ||
Directory.Delete(path: directory, recursive: true); | ||
|
||
// Act | ||
for (int i = 0; i < numThreads; i++) | ||
{ | ||
var thread = new Thread(MigrationRunner.Run); | ||
thread.Start(); | ||
threads.Add(thread); | ||
} | ||
|
||
foreach (var thread in threads) | ||
{ | ||
thread.Join(timeout: TimeSpan.FromSeconds(timeoutInSeconds)); | ||
} | ||
|
||
// Assert | ||
Assert.True(Directory.Exists(directory)); | ||
Assert.Equal(1, Directory.GetFiles(directory).Length); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test/NuGet.Core.Tests/NuGet.Common.Test/StringExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace NuGet.Common.Test | ||
{ | ||
public class StringExtensionsTests | ||
{ | ||
[Fact] | ||
public void FormatWithDoubleQuotes_WhenNullIsPassedReturnsNull_Success() | ||
{ | ||
string actual = null; | ||
string formatted = actual.FormatWithDoubleQuotes(); | ||
Assert.Equal(actual, formatted); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("/home/user/NuGet AppData/share")] | ||
[InlineData("/home/user/NuGet/share")] | ||
[InlineData(@"c:\users\NuGet App\Share")] | ||
[InlineData(@"c:\users\NuGet\Share")] | ||
public void FormatWithDoubleQuotes_WhenNonNullStringIsPassedReturnsFormattedString_Success(string actual) | ||
{ | ||
string formatted = actual.FormatWithDoubleQuotes(); | ||
Assert.Equal($@"""{actual}""", formatted); | ||
} | ||
} | ||
} |