From 08a7a7bd17e504d808329dcc1ffc866d9f59d040 Mon Sep 17 00:00:00 2001 From: Kartheek Penagamuri <52756182+kartheekp-ms@users.noreply.github.com> Date: Mon, 19 Dec 2022 08:25:14 -0800 Subject: [PATCH] add missing test coverage for AbandonedMutexException in NuGet migrations logic (#4982) --- .../NuGet.Common.Test/MigrationRunnerTests.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs b/test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs index c2560e1fb54..c672b9a56d3 100644 --- a/test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs @@ -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); + + // 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]); + + + void AbandonMutex() + { + signal = _orphan.WaitOne(TimeSpan.FromMinutes(1), false); + // Abandon the mutex by exiting the method without releasing + } + } } }