Skip to content

Commit

Permalink
make MarkExecuted method thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tonerdo committed Mar 24, 2018
1 parent 42eff34 commit c055f95
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/coverlet.core/Attributes/ExcludeFromCoverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Coverlet.Core.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
public class ExcludeFromCoverageAttribute : Attribute { }
}
30 changes: 12 additions & 18 deletions src/coverlet.core/CoverageTracker.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;

using Coverlet.Core.Attributes;
using Coverlet.Core.Extensions;

namespace Coverlet.Core
{
public static class CoverageTracker
{
private static Dictionary<string, List<string>> _markers;
private static bool _registered;

[ExcludeFromCoverage]
public static void MarkExecuted(string path, string marker)
static CoverageTracker()
{
if (_markers == null)
{
_markers = new Dictionary<string, List<string>>();
}

if (!_markers.ContainsKey(path))
{
_markers.Add(path, new List<string>());
}
_markers = new Dictionary<string, List<string>>();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}

if (!_registered)
[ExcludeFromCoverage]
public static void MarkExecuted(string path, string marker)
{
lock (_markers)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
_registered = true;
_markers.TryAdd(path, new List<string>());
_markers[path].Add(marker);
}

_markers[path].Add(marker);
}

public static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
foreach (var kvp in _markers)
{
File.WriteAllLines(kvp.Key, kvp.Value);
}
}
}
}
2 changes: 2 additions & 0 deletions src/coverlet.core/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using Coverlet.Core.Attributes;

namespace Coverlet.Core.Extensions
{
internal static class DictionaryExtensions
{
[ExcludeFromCoverage]
public static bool TryAdd<T, U>(this Dictionary<T, U> dictionary, T key, U value)
{
if (dictionary.ContainsKey(key))
Expand Down

0 comments on commit c055f95

Please sign in to comment.