1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Diagnostics ;
4
5
using System . IO ;
@@ -14,11 +15,11 @@ namespace Coverlet.Core.Helpers
14
15
{
15
16
internal static class InstrumentationHelper
16
17
{
17
- private static readonly Dictionary < string , string > _backupList = new Dictionary < string , string > ( ) ;
18
+ private static readonly ConcurrentDictionary < string , string > _backupList = new ConcurrentDictionary < string , string > ( ) ;
18
19
19
20
static InstrumentationHelper ( )
20
21
{
21
- AppDomain . CurrentDomain . ProcessExit += ( s , e ) => RestoreOriginalModules ( ) ;
22
+ AppDomain . CurrentDomain . ProcessExit += ( s , e ) => RestoreOriginalModules ( ) ;
22
23
}
23
24
24
25
public static string [ ] GetCoverableModules ( string module , string [ ] directories , bool includeTestAssembly )
@@ -94,13 +95,19 @@ public static void BackupOriginalModule(string module, string identifier)
94
95
var backupPath = GetBackupPath ( module , identifier ) ;
95
96
var backupSymbolPath = Path . ChangeExtension ( backupPath , ".pdb" ) ;
96
97
File . Copy ( module , backupPath , true ) ;
97
- _backupList . Add ( module , backupPath ) ;
98
+ if ( ! _backupList . TryAdd ( module , backupPath ) )
99
+ {
100
+ throw new ArgumentException ( $ "Key already added '{ module } '") ;
101
+ }
98
102
99
103
var symbolFile = Path . ChangeExtension ( module , ".pdb" ) ;
100
104
if ( File . Exists ( symbolFile ) )
101
105
{
102
106
File . Copy ( symbolFile , backupSymbolPath , true ) ;
103
- _backupList . Add ( symbolFile , backupSymbolPath ) ;
107
+ if ( ! _backupList . TryAdd ( symbolFile , backupSymbolPath ) )
108
+ {
109
+ throw new ArgumentException ( $ "Key already added '{ module } '") ;
110
+ }
104
111
}
105
112
}
106
113
@@ -117,7 +124,7 @@ public static void RestoreOriginalModule(string module, string identifier)
117
124
{
118
125
File . Copy ( backupPath , module , true ) ;
119
126
File . Delete ( backupPath ) ;
120
- _backupList . Remove ( module ) ;
127
+ _backupList . TryRemove ( module , out string _ ) ;
121
128
} , retryStrategy , 10 ) ;
122
129
123
130
RetryHelper . Retry ( ( ) =>
@@ -127,7 +134,7 @@ public static void RestoreOriginalModule(string module, string identifier)
127
134
string symbolFile = Path . ChangeExtension ( module , ".pdb" ) ;
128
135
File . Copy ( backupSymbolPath , symbolFile , true ) ;
129
136
File . Delete ( backupSymbolPath ) ;
130
- _backupList . Remove ( symbolFile ) ;
137
+ _backupList . TryRemove ( symbolFile , out string _ ) ;
131
138
}
132
139
} , retryStrategy , 10 ) ;
133
140
}
@@ -145,7 +152,7 @@ public static void RestoreOriginalModules()
145
152
{
146
153
File . Copy ( backupPath , key , true ) ;
147
154
File . Delete ( backupPath ) ;
148
- _backupList . Remove ( key ) ;
155
+ _backupList . TryRemove ( key , out string _ ) ;
149
156
} , retryStrategy , 10 ) ;
150
157
}
151
158
}
0 commit comments