From ebfff1f15ef2442605d53ac4d7f14fc65ee80c0d Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Thu, 2 May 2019 21:51:23 +0200 Subject: [PATCH 1/2] use ConcurrentDictionary --- .../Helpers/InstrumentationHelper.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/coverlet.core/Helpers/InstrumentationHelper.cs b/src/coverlet.core/Helpers/InstrumentationHelper.cs index 075d6c75f..1db3c1081 100644 --- a/src/coverlet.core/Helpers/InstrumentationHelper.cs +++ b/src/coverlet.core/Helpers/InstrumentationHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -14,11 +15,11 @@ namespace Coverlet.Core.Helpers { internal static class InstrumentationHelper { - private static readonly Dictionary _backupList = new Dictionary(); + private static readonly ConcurrentDictionary _backupList = new ConcurrentDictionary(); static InstrumentationHelper() { - AppDomain.CurrentDomain.ProcessExit += (s,e) => RestoreOriginalModules(); + AppDomain.CurrentDomain.ProcessExit += (s, e) => RestoreOriginalModules(); } public static string[] GetCoverableModules(string module, string[] directories, bool includeTestAssembly) @@ -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 found '{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 found '{module}'"); + } } } @@ -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(() => @@ -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); } @@ -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); } } From 74f3eadf7e52bcde5c936aaa51b71e1610533fbc Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Thu, 2 May 2019 21:52:25 +0200 Subject: [PATCH 2/2] updates --- src/coverlet.core/Helpers/InstrumentationHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coverlet.core/Helpers/InstrumentationHelper.cs b/src/coverlet.core/Helpers/InstrumentationHelper.cs index 1db3c1081..c2d497c4b 100644 --- a/src/coverlet.core/Helpers/InstrumentationHelper.cs +++ b/src/coverlet.core/Helpers/InstrumentationHelper.cs @@ -97,7 +97,7 @@ public static void BackupOriginalModule(string module, string identifier) File.Copy(module, backupPath, true); if (!_backupList.TryAdd(module, backupPath)) { - throw new ArgumentException($"Key already found '{module}'"); + throw new ArgumentException($"Key already added '{module}'"); } var symbolFile = Path.ChangeExtension(module, ".pdb"); @@ -106,7 +106,7 @@ public static void BackupOriginalModule(string module, string identifier) File.Copy(symbolFile, backupSymbolPath, true); if (!_backupList.TryAdd(symbolFile, backupSymbolPath)) { - throw new ArgumentException($"Key already found '{module}'"); + throw new ArgumentException($"Key already added '{module}'"); } } }