Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartheek Penagamuri committed Oct 20, 2022
1 parent 1f4771d commit 2c5ddbb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ void AddAllParentDirectoriesUpToHome(string path)
{
pathsToCheck.Add(path);

// Unix-based systems support only the forward slash which is returned by Path.AltDirectorySeparatorChar
if (!path.StartsWith(homePath + Path.AltDirectorySeparatorChar, StringComparison.Ordinal))
if (!path.StartsWith(homePath + Path.DirectorySeparatorChar, StringComparison.Ordinal))
{
return;
}
Expand Down
41 changes: 22 additions & 19 deletions src/NuGet.Core/NuGet.Common/Migrations/MigrationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,36 @@ public static void Run()
// so use a global mutex and then check if someone else already did the work.
using (var mutex = new Mutex(false, "NuGet-Migrations"))
{
bool captured;
try
{
if (mutex.WaitOne(TimeSpan.FromMinutes(1), false))
captured = mutex.WaitOne(TimeSpan.FromMinutes(1), false);
}
catch (AbandonedMutexException ex)
{
ex.Mutex?.ReleaseMutex();
captured = true;
}
if (captured)
{
if (!File.Exists(expectedMigrationFilename))
{
if (!File.Exists(expectedMigrationFilename))
// Only run migrations that have not already been run
int highestMigrationRun = GetHighestMigrationRun(migrationsDirectory);
for (int i = highestMigrationRun + 1; i < Migrations.Count; i++)
{
// Only run migrations that have not already been run
int highestMigrationRun = GetHighestMigrationRun(migrationsDirectory);
for (int i = highestMigrationRun + 1; i < Migrations.Count; i++)
try
{
try
{
Migrations[i]();
// Create file for every migration run, so that if an older version of NuGet is run, it doesn't try to run
// migrations again.
string migrationFile = Path.Combine(migrationsDirectory, (i + 1).ToString(CultureInfo.InvariantCulture));
File.WriteAllText(migrationFile, string.Empty);
}
catch { }
Migrations[i]();
// Create file for every migration run, so that if an older version of NuGet is run, it doesn't try to run
// migrations again.
string migrationFile = Path.Combine(migrationsDirectory, (i + 1).ToString(CultureInfo.InvariantCulture));
File.WriteAllText(migrationFile, string.Empty);
}
catch { }
}
mutex.ReleaseMutex();
}
}
catch (AbandonedMutexException ex)
{
ex.Mutex?.ReleaseMutex();
mutex.ReleaseMutex();
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions test/NuGet.Core.Tests/NuGet.Common.Test/MigrationRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// 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.Tasks;
using System.Threading;
using NuGet.Common.Migrations;
using NuGet.PackageManagement;
using Xunit;

namespace NuGet.Common.Test
Expand All @@ -15,25 +13,29 @@ namespace NuGet.Common.Test
public class MigrationRunnerTests
{
[Fact]
public async Task WhenExecutedInParallelOnlyOneFileIsCreatedForEveryMigration_SuccessAsync()
public void WhenExecutedInParallelOnlyOneFileIsCreatedForEveryMigration_Success()
{
var tasks = new List<Task>();
var threads = new List<Thread>();

// Arrange
string directory = MigrationRunner.GetMigrationsDirectory();
if (Directory.Exists(directory))
Directory.Delete(path: directory, recursive: true);

// Act
for(int count = 0; count < 5; count++)
tasks.Add(Task.Run(() => MigrationRunner.Run()));
for (int count = 0; count < 5; count++)
{
var thread = new Thread(MigrationRunner.Run);
thread.Start();
threads.Add(thread);
}

Task t = Task.WhenAll(tasks);
await t;
foreach (var thread in threads)
{
thread.Join();
}

// Assert
Assert.True(t.IsCompleted);
Assert.Equal(TaskStatus.RanToCompletion, t.Status);
Assert.True(Directory.Exists(directory));
Assert.Equal(1, Directory.GetFiles(directory).Length);
}
Expand Down

0 comments on commit 2c5ddbb

Please sign in to comment.