-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds retry functionality to copy back the original assembly. #35
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,3 +1,4 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
@@ -79,7 +80,16 @@ public CoverageResult GetCoverageResult() | |
} | ||
|
||
modules.Add(result.ModulePath, documents); | ||
InstrumentationHelper.RestoreOriginalModule(result.ModulePath, _identifier); | ||
|
||
// Restore the original module - retry up to 10 times, since the destination file could be locked | ||
// See: https://github.com/tonerdo/coverlet/issues/25 | ||
var currentSleep = 6; | ||
Func<TimeSpan> retryStrategy = () => { | ||
var sleep = TimeSpan.FromMilliseconds(currentSleep); | ||
currentSleep *= 2; | ||
return sleep; | ||
}; | ||
RetryHelper.Retry(() => InstrumentationHelper.RestoreOriginalModule(result.ModulePath, _identifier), retryStrategy, 10); | ||
} | ||
|
||
return new CoverageResult | ||
|
@@ -120,7 +130,15 @@ private void CalculateCoverage() | |
} | ||
} | ||
|
||
File.Delete(result.HitsFilePath); | ||
// Restore the original module - retry up to 10 times, since the destination file could be locked | ||
// See: https://github.com/tonerdo/coverlet/issues/25 | ||
var currentSleep = 6; | ||
Func<TimeSpan> retryStrategy = () => { | ||
var sleep = TimeSpan.FromMilliseconds(currentSleep); | ||
currentSleep *= 2; | ||
return sleep; | ||
}; | ||
RetryHelper.Retry(() => File.Delete(result.HitsFilePath), retryStrategy, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. Create an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
} | ||
} | ||
} | ||
|
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,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
// A slightly amended version of the code found here: https://stackoverflow.com/a/1563234/186184 | ||
// This code allows for varying backoff strategies through the use of Func<TimeSpan>. | ||
public static class RetryHelper | ||
{ | ||
/// <summary> | ||
/// Retry a void method. | ||
/// </summary> | ||
/// <param name="action">The action to perform</param> | ||
/// <param name="backoffStrategy">A function returning a Timespan defining the backoff strategy to use.</param> | ||
/// <param name="maxAttemptCount">The maximum number of retries before bailing out. Defaults to 3.</param> | ||
public static void Retry( | ||
Action action, | ||
Func<TimeSpan> backoffStrategy, | ||
int maxAttemptCount = 3) | ||
{ | ||
Do<object>(() => | ||
{ | ||
action(); | ||
return null; | ||
}, backoffStrategy, maxAttemptCount); | ||
} | ||
|
||
/// <summary> | ||
/// Retry a method returning type T. | ||
/// </summary> | ||
/// <param name="action">The action to perform</param> | ||
/// <param name="backoffStrategy">A function returning a Timespan defining the backoff strategy to use.</param> | ||
/// <param name="maxAttemptCount">The maximum number of retries before bailing out. Defaults to 3.</param> | ||
public static T Do<T>( | ||
Func<T> action, | ||
Func<TimeSpan> backoffStrategy, | ||
int maxAttemptCount = 3) | ||
{ | ||
var exceptions = new List<Exception>(); | ||
|
||
for (int attempted = 0; attempted < maxAttemptCount; attempted++) | ||
{ | ||
try | ||
{ | ||
if (attempted > 0) | ||
{ | ||
Thread.Sleep(backoffStrategy()); | ||
} | ||
return action(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
exceptions.Add(ex); | ||
} | ||
} | ||
throw new AggregateException(exceptions); | ||
} | ||
} |
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,79 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using Xunit; | ||
using Coverlet.Core.Helpers; | ||
|
||
namespace Coverlet.Core.Helpers.Tests | ||
{ | ||
public class RetryHelperTests | ||
{ | ||
[Fact] | ||
public void TestRetryWithFixedRetryBackoff() | ||
{ | ||
Func<TimeSpan> retryStrategy = () => { | ||
return TimeSpan.FromMilliseconds(1); | ||
}; | ||
|
||
var target = new RetryTarget(); | ||
try | ||
{ | ||
RetryHelper.Retry(() => target.TargetActionThrows(), retryStrategy, 7); | ||
} | ||
catch | ||
{ | ||
Assert.Equal(7, target.Calls); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestRetryWithExponentialRetryBackoff() | ||
{ | ||
var currentSleep = 6; | ||
Func<TimeSpan> retryStrategy = () => { | ||
var sleep = TimeSpan.FromMilliseconds(currentSleep); | ||
currentSleep *= 2; | ||
return sleep; | ||
}; | ||
|
||
var target = new RetryTarget(); | ||
try | ||
{ | ||
RetryHelper.Retry(() => target.TargetActionThrows(), retryStrategy, 3); | ||
} | ||
catch | ||
{ | ||
Assert.Equal(3, target.Calls); | ||
Assert.Equal(24, currentSleep); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestRetryFinishesIfSuccessful() | ||
{ | ||
Func<TimeSpan> retryStrategy = () => { | ||
return TimeSpan.FromMilliseconds(1); | ||
}; | ||
|
||
var target = new RetryTarget(); | ||
RetryHelper.Retry(() => target.TargetActionThrows5Times(), retryStrategy, 20); | ||
Assert.Equal(6, target.Calls); | ||
} | ||
|
||
} | ||
|
||
public class RetryTarget | ||
{ | ||
public int Calls { get; set; } | ||
public void TargetActionThrows() | ||
{ | ||
Calls++; | ||
throw new Exception("Simulating Failure"); | ||
} | ||
public void TargetActionThrows5Times() | ||
{ | ||
Calls++; | ||
if (Calls < 6) throw new Exception("Simulating Failure"); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move all this logic into
InstrumentationHelper.RestoreOriginalModule
. You can put the code in that function into theRetry(() => {}
delegateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done