-
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 (release-5.7.x) (#4917)
- Loading branch information
1 parent
f02b6ab
commit 3392a5c
Showing
6 changed files
with
144 additions
and
45 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
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
// 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 NuGet.Common.Migrations; | ||
using Xunit; | ||
|
||
namespace NuGet.Common.Test | ||
{ | ||
[CollectionDefinition("MigrationRunner", DisableParallelization = true)] | ||
public class MigrationRunnerTests | ||
{ | ||
[Fact] | ||
public void Run_WhenExecutedOnSingleThreadThenOneMigrationFileIsCreated_Success() | ||
{ | ||
// Arrange | ||
string directory = MigrationRunner.GetMigrationsDirectory(); | ||
if (Directory.Exists(directory)) | ||
Directory.Delete(path: directory, recursive: true); | ||
|
||
// Act | ||
MigrationRunner.Run(); | ||
|
||
// Assert | ||
Assert.True(Directory.Exists(directory)); | ||
var files = Directory.GetFiles(directory); | ||
Assert.Equal(1, files.Length); | ||
Assert.Equal(Path.Combine(directory, "1"), files[0]); | ||
} | ||
|
||
[Fact] | ||
public void Run_WhenExecutedInParallelThenOnlyOneMigrationFileIsCreated_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)); | ||
var files = Directory.GetFiles(directory); | ||
Assert.Equal(1, files.Length); | ||
Assert.Equal(Path.Combine(directory, "1"), files[0]); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |