Skip to content

Commit ab5ea14

Browse files
committed
Extract method for creating a retry strategy.
1 parent 7c8f5d0 commit ab5ea14

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ public static void RestoreOriginalModule(string module, string identifier)
6363

6464
// Restore the original module - retry up to 10 times, since the destination file could be locked
6565
// See: https://github.com/tonerdo/coverlet/issues/25
66-
var currentSleep = 6;
67-
TimeSpan retryStrategy()
68-
{
69-
var sleep = TimeSpan.FromMilliseconds(currentSleep);
70-
currentSleep *= 2;
71-
return sleep;
72-
}
66+
var retryStrategy = CreateRetryStrategy();
7367

7468
RetryHelper.Retry(() => {
7569
File.Copy(backupPath, module, true);
@@ -81,13 +75,7 @@ public static IEnumerable<string> ReadHitsFile(string path)
8175
{
8276
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
8377
// See: https://github.com/tonerdo/coverlet/issues/25
84-
var currentSleep = 6;
85-
TimeSpan retryStrategy()
86-
{
87-
var sleep = TimeSpan.FromMilliseconds(currentSleep);
88-
currentSleep *= 2;
89-
return sleep;
90-
}
78+
var retryStrategy = CreateRetryStrategy();
9179

9280
return RetryHelper.Do(() => File.ReadLines(path), retryStrategy, 10);
9381
}
@@ -96,13 +84,7 @@ public static void DeleteHitsFile(string path)
9684
{
9785
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
9886
// See: https://github.com/tonerdo/coverlet/issues/25
99-
var currentSleep = 6;
100-
TimeSpan retryStrategy()
101-
{
102-
var sleep = TimeSpan.FromMilliseconds(currentSleep);
103-
currentSleep *= 2;
104-
return sleep;
105-
}
87+
var retryStrategy = CreateRetryStrategy();
10688

10789
RetryHelper.Retry(() => File.Delete(path), retryStrategy, 10);
10890
}
@@ -164,6 +146,18 @@ private static string GetBackupPath(string module, string identifier)
164146
Path.GetFileNameWithoutExtension(module) + "_" + identifier + ".dll"
165147
);
166148
}
149+
150+
private static Func<TimeSpan> CreateRetryStrategy(int initialSleepSeconds = 6)
151+
{
152+
TimeSpan retryStrategy()
153+
{
154+
var sleep = TimeSpan.FromMilliseconds(initialSleepSeconds);
155+
initialSleepSeconds *= 2;
156+
return sleep;
157+
}
158+
159+
return retryStrategy;
160+
}
167161
}
168162
}
169163

0 commit comments

Comments
 (0)