Skip to content

Commit

Permalink
Added DomainUnloaded event.
Browse files Browse the repository at this point in the history
When using the NUnit3 test adapter, the process doesn't exit, the app domain is simply unloaded. This means that the  ProcessExit event is never raised. The DomainUnload event is called always. To make sure we don't add double hits, we clear the _markers collection after writing (in case the event handler is triggered twice).
  • Loading branch information
Maarten van Sambeek committed May 18, 2018
1 parent e3cf833 commit f0b7463
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/coverlet.core/CoverageTracker.cs
Expand Up @@ -18,6 +18,7 @@ static CoverageTracker()
_markers = new Dictionary<string, List<string>>();
_markerFileCount = new Dictionary<string, int>();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_ProcessExit);
}

[ExcludeFromCoverage]
Expand Down Expand Up @@ -48,17 +49,22 @@ public static void MarkExecuted(string path, string marker)
[ExcludeFromCoverage]
public static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
foreach (var kvp in _markers)
lock (_markers)
{
using (var fs = new FileStream($"{kvp.Key}_compressed_{_markerFileCount[kvp.Key]}", FileMode.OpenOrCreate))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var sw = new StreamWriter(gz))
foreach (var kvp in _markers)
{
foreach(var line in kvp.Value)
using (var fs = new FileStream($"{kvp.Key}_compressed_{_markerFileCount[kvp.Key]}", FileMode.OpenOrCreate))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var sw = new StreamWriter(gz))
{
sw.WriteLine(line);
foreach(var line in kvp.Value)
{
sw.WriteLine(line);
}
}
}

_markers.Clear();
}
}
}
Expand Down

0 comments on commit f0b7463

Please sign in to comment.