-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from lucaslorentz/fix-missed-hits
Fix some missed hits
- Loading branch information
Showing
28 changed files
with
255 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "3.1.201" | ||
"version": "3.1.403" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,14 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
|
||
namespace MiniCover.HitServices | ||
namespace MiniCover.HitServices | ||
{ | ||
public static class HitService | ||
{ | ||
public static MethodContext EnterMethod( | ||
public static MethodScope EnterMethod( | ||
string hitsPath, | ||
string assemblyName, | ||
string className, | ||
string methodName) | ||
{ | ||
return new MethodContext(hitsPath, assemblyName, className, methodName); | ||
} | ||
|
||
public class MethodContext : IDisposable | ||
{ | ||
private static ConcurrentDictionary<string, Stream> _filesStream = new ConcurrentDictionary<string, Stream>(); | ||
|
||
private readonly string _hitsPath; | ||
private readonly HitContext _hitContext; | ||
private readonly bool _saveHitContext; | ||
|
||
public MethodContext( | ||
string hitsPath, | ||
string assemblyName, | ||
string className, | ||
string methodName) | ||
{ | ||
_hitsPath = hitsPath; | ||
|
||
if (HitContext.Current == null) | ||
{ | ||
_hitContext = new HitContext(assemblyName, className, methodName); | ||
HitContext.Current = _hitContext; | ||
_saveHitContext = true; | ||
} | ||
else | ||
{ | ||
_hitContext = HitContext.Current; | ||
} | ||
} | ||
|
||
public void Hit(int id) | ||
{ | ||
_hitContext.RecordHit(id); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_saveHitContext) | ||
{ | ||
var fileStream = _filesStream.GetOrAdd(_hitsPath, CreateOutputFile); | ||
lock (fileStream) | ||
{ | ||
_hitContext.Serialize(fileStream); | ||
fileStream.Flush(); | ||
} | ||
HitContext.Current = null; | ||
} | ||
} | ||
|
||
private static FileStream CreateOutputFile(string hitsPath) | ||
{ | ||
Directory.CreateDirectory(hitsPath); | ||
var filePath = Path.Combine(hitsPath, $"{Guid.NewGuid()}.hits"); | ||
return File.Open(filePath, FileMode.CreateNew); | ||
} | ||
return new MethodScope(hitsPath, assemblyName, className, methodName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace MiniCover.HitServices | ||
{ | ||
public class MethodScope : IDisposable | ||
{ | ||
private readonly HitContext _hitContext; | ||
private readonly string _hitsPath; | ||
private readonly bool _isEntryMethod; | ||
|
||
public MethodScope( | ||
string hitsPath, | ||
string assemblyName, | ||
string className, | ||
string methodName) | ||
{ | ||
_hitContext = HitContext.Current; | ||
|
||
if (_hitContext == null) | ||
{ | ||
_hitContext = new HitContext(assemblyName, className, methodName); | ||
_isEntryMethod = true; | ||
HitContext.Current = _hitContext; | ||
} | ||
|
||
_hitsPath = hitsPath; | ||
|
||
lock (_hitContext) | ||
{ | ||
_hitContext.EnterMethod(); | ||
} | ||
} | ||
|
||
public void Hit(int id) | ||
{ | ||
lock (_hitContext) | ||
{ | ||
_hitContext.RecordHit(id); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_isEntryMethod) | ||
HitContext.Current = null; | ||
|
||
lock (_hitContext) | ||
{ | ||
if (_hitContext.ExitMethod() == 0) | ||
SaveHitContext(); | ||
} | ||
} | ||
|
||
private void SaveHitContext() | ||
{ | ||
lock (_hitContext) | ||
{ | ||
if (_hitContext.Hits.Count == 0) | ||
return; | ||
|
||
Directory.CreateDirectory(_hitsPath); | ||
|
||
var fileName = Path.Combine(_hitsPath, $"{_hitContext.Id}.hits"); | ||
|
||
using (var fileStream = File.Open(fileName, FileMode.Create)) | ||
{ | ||
_hitContext.Serialize(fileStream); | ||
fileStream.Flush(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,4 @@ public void BinarySerializationShouldWork() | |
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.