Skip to content
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 3 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/coverlet.core/Coverage.cs
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;
Expand Down Expand Up @@ -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);
Copy link
Collaborator

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 the Retry(() => {} delegate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

return new CoverageResult
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. Create an InstrumentationHelper.DeleteHitsFile and basically move this block into that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/coverlet.core/Helpers/RetryHelper.cs
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);
}
}
79 changes: 79 additions & 0 deletions test/coverlet.core.tests/Helpers/RetryHelperTests.cs
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");
}
}
}