Skip to content

Commit c055f95

Browse files
committed
make MarkExecuted method thread safe
1 parent 42eff34 commit c055f95

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/coverlet.core/Attributes/ExcludeFromCoverage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Coverlet.Core.Attributes
44
{
5-
[AttributeUsage(AttributeTargets.Method)]
5+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
66
public class ExcludeFromCoverageAttribute : Attribute { }
77
}

src/coverlet.core/CoverageTracker.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
45
using Coverlet.Core.Attributes;
6+
using Coverlet.Core.Extensions;
57

68
namespace Coverlet.Core
79
{
810
public static class CoverageTracker
911
{
1012
private static Dictionary<string, List<string>> _markers;
11-
private static bool _registered;
1213

1314
[ExcludeFromCoverage]
14-
public static void MarkExecuted(string path, string marker)
15+
static CoverageTracker()
1516
{
16-
if (_markers == null)
17-
{
18-
_markers = new Dictionary<string, List<string>>();
19-
}
20-
21-
if (!_markers.ContainsKey(path))
22-
{
23-
_markers.Add(path, new List<string>());
24-
}
17+
_markers = new Dictionary<string, List<string>>();
18+
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
19+
}
2520

26-
if (!_registered)
21+
[ExcludeFromCoverage]
22+
public static void MarkExecuted(string path, string marker)
23+
{
24+
lock (_markers)
2725
{
28-
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
29-
_registered = true;
26+
_markers.TryAdd(path, new List<string>());
27+
_markers[path].Add(marker);
3028
}
31-
32-
_markers[path].Add(marker);
3329
}
3430

3531
public static void CurrentDomain_ProcessExit(object sender, EventArgs e)
3632
{
3733
foreach (var kvp in _markers)
38-
{
3934
File.WriteAllLines(kvp.Key, kvp.Value);
40-
}
4135
}
4236
}
4337
}

src/coverlet.core/Extensions/DictionaryExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Collections.Generic;
2+
using Coverlet.Core.Attributes;
23

34
namespace Coverlet.Core.Extensions
45
{
56
internal static class DictionaryExtensions
67
{
8+
[ExcludeFromCoverage]
79
public static bool TryAdd<T, U>(this Dictionary<T, U> dictionary, T key, U value)
810
{
911
if (dictionary.ContainsKey(key))

0 commit comments

Comments
 (0)