Skip to content

Commit 75bc655

Browse files
committed
retry reading from hits file
1 parent fafb19b commit 75bc655

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/coverlet.core/Coverage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void CalculateCoverage()
9595
foreach (var result in _results)
9696
{
9797
if (!File.Exists(result.HitsFilePath)) { continue; }
98-
var lines = File.ReadAllLines(result.HitsFilePath);
98+
var lines = InstrumentationHelper.ReadHitsFile(result.HitsFilePath);
9999
for (int i = 0; i < lines.Length; i++)
100100
{
101101
var info = lines[i].Split(',');

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public static void RestoreOriginalModule(string module, string identifier)
7878
}, retryStrategy, 10);
7979
}
8080

81+
public static string[] ReadHitsFile(string path)
82+
{
83+
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
84+
// See: https://github.com/tonerdo/coverlet/issues/25
85+
var currentSleep = 6;
86+
Func<TimeSpan> retryStrategy = () =>
87+
{
88+
var sleep = TimeSpan.FromMilliseconds(currentSleep);
89+
currentSleep *= 2;
90+
return sleep;
91+
};
92+
93+
return RetryHelper.Do(() => File.ReadAllLines(path), retryStrategy, 10);
94+
}
95+
8196
public static void DeleteHitsFile(string path)
8297
{
8398
// Retry hitting the hits file - retry up to 10 times, since the file could be locked

test/coverlet.core.tests/Helpers/InstrumentationHelperTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public void TestDontCopyCoverletDependency()
6060
Directory.Delete(directory.FullName, true);
6161
}
6262

63+
[Fact]
64+
public void TestReadHitsFile()
65+
{
66+
var tempFile = Path.GetTempFileName();
67+
Assert.True(File.Exists(tempFile));
68+
69+
var lines = InstrumentationHelper.ReadHitsFile(tempFile);
70+
Assert.NotNull(lines);
71+
}
72+
6373
[Fact]
6474
public void TestDeleteHitsFile()
6575
{

0 commit comments

Comments
 (0)