Skip to content
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
21 changes: 14 additions & 7 deletions src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -14,11 +15,11 @@ namespace Coverlet.Core.Helpers
{
internal static class InstrumentationHelper
{
private static readonly Dictionary<string, string> _backupList = new Dictionary<string, string>();
private static readonly ConcurrentDictionary<string, string> _backupList = new ConcurrentDictionary<string, string>();

static InstrumentationHelper()
{
AppDomain.CurrentDomain.ProcessExit += (s,e) => RestoreOriginalModules();
AppDomain.CurrentDomain.ProcessExit += (s, e) => RestoreOriginalModules();
}

public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly)
Expand Down Expand Up @@ -94,13 +95,19 @@ public static void BackupOriginalModule(string module, string identifier)
var backupPath = GetBackupPath(module, identifier);
var backupSymbolPath = Path.ChangeExtension(backupPath, ".pdb");
File.Copy(module, backupPath, true);
_backupList.Add(module, backupPath);
if (!_backupList.TryAdd(module, backupPath))
{
throw new ArgumentException($"Key already added '{module}'");
}

var symbolFile = Path.ChangeExtension(module, ".pdb");
if (File.Exists(symbolFile))
{
File.Copy(symbolFile, backupSymbolPath, true);
_backupList.Add(symbolFile, backupSymbolPath);
if (!_backupList.TryAdd(symbolFile, backupSymbolPath))
{
throw new ArgumentException($"Key already added '{module}'");
}
}
}

Expand All @@ -117,7 +124,7 @@ public static void RestoreOriginalModule(string module, string identifier)
{
File.Copy(backupPath, module, true);
File.Delete(backupPath);
_backupList.Remove(module);
_backupList.TryRemove(module, out string _);
}, retryStrategy, 10);

RetryHelper.Retry(() =>
Expand All @@ -127,7 +134,7 @@ public static void RestoreOriginalModule(string module, string identifier)
string symbolFile = Path.ChangeExtension(module, ".pdb");
File.Copy(backupSymbolPath, symbolFile, true);
File.Delete(backupSymbolPath);
_backupList.Remove(symbolFile);
_backupList.TryRemove(symbolFile, out string _);
}
}, retryStrategy, 10);
}
Expand All @@ -145,7 +152,7 @@ public static void RestoreOriginalModules()
{
File.Copy(backupPath, key, true);
File.Delete(backupPath);
_backupList.Remove(key);
_backupList.TryRemove(key, out string _);
}, retryStrategy, 10);
}
}
Expand Down