Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing test coverage for AbandonedMutexException in NuGet migrations logic #4982

Merged
merged 7 commits into from
Dec 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,38 @@ public void Run_WhenExecutedInParallelThenOnlyOneMigrationFileIsCreated_Success(
Assert.Equal(1, files.Length);
Assert.Equal(Path.Combine(directory, "1"), files[0]);
}

[Fact]
public void Run_WhenAThreadAbandonsMutexThenNextMigrationRunReleasesMutexAndCreatesMigrationFile_Success()
{
Mutex _orphan = new Mutex(false, "NuGet-Migrations");
bool signal = false;

// Arrange
Thread t = new Thread(new ThreadStart(AbandonMutex));
t.Start();
t.Join();
Assert.True(signal, userMessage: "Failed to acquire the mutex.");

string directory = MigrationRunner.GetMigrationsDirectory();
if (Directory.Exists(directory))
Directory.Delete(path: directory, recursive: true);
dominoFire marked this conversation as resolved.
Show resolved Hide resolved

// Act
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
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]);


void AbandonMutex()
{
signal = _orphan.WaitOne(TimeSpan.FromMinutes(1), false);
// Abandon the mutex by exiting the method without releasing
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}