Skip to content

Commit

Permalink
add missing test coverage for AbandonedMutexException in NuGet migrat…
Browse files Browse the repository at this point in the history
…ions logic (#4982)
  • Loading branch information
kartheekp-ms authored and Kartheek Penagamuri committed Jan 5, 2023
1 parent 3392a5c commit a5ec14e
Showing 1 changed file with 33 additions and 0 deletions.
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);

// 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
}
}
}
}

0 comments on commit a5ec14e

Please sign in to comment.