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 all 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
3 changes: 2 additions & 1 deletion 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 @@ -120,7 +121,7 @@ private void CalculateCoverage()
}
}

File.Delete(result.HitsFilePath);
InstrumentationHelper.DeleteHitsFile(result.HitsFilePath);
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/coverlet.core/Helpers/InstrumentationHelper.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 @@ -62,8 +63,31 @@ public static void RestoreOriginalModule(string module, string identifier)
Path.GetFileNameWithoutExtension(module) + "_" + identifier + ".dll"
);

File.Copy(backupPath, module, true);
File.Delete(backupPath);
// 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.Copy(backupPath, module, true);
File.Delete(backupPath);
}, retryStrategy, 10);
}

public static void DeleteHitsFile(string path)
{
// Retry hitting the hits file - retry up to 10 times, since the 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(path), retryStrategy, 10);
}
}
}
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");
}
}
}