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

Fixing what looks to be a small bug #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Thumbs.db
*.obj
*.exe
*.pdb
*.user
*.ncb
*.suo
[Bb]in
[Dd]ebug*/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
*.csproj.user
*.orig
[Bb]in/
[Oo]bj/
[Oo]utput/
6 changes: 0 additions & 6 deletions .hgignore

This file was deleted.

7 changes: 7 additions & 0 deletions ReliabilityPatterns/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public TResult Execute<TResult>(Func<TResult> operation)
Interlocked.Increment(ref failureCount);

OnServiceLevelChanged(new EventArgs());

//Re-evalutate the failure count to determine if the breaker should be tripped
if (failureCount >= threshold)
{
// Failure count has reached threshold, so trip circuit breaker
Trip();
}
}
else if (failureCount >= threshold)
{
Expand Down
61 changes: 61 additions & 0 deletions Tests/CircuitBreakerTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using ReliabilityPatterns;

namespace Tests
{
public class CircuitBreakerTestHelper
{
readonly ushort _allowedRetries;
public CircuitBreaker Breaker { get; private set; }
public int CallCount { get; private set; }

public CircuitBreakerTestHelper(uint threshold = 4, ushort allowedRetries = 0)
{
_allowedRetries = allowedRetries;
Breaker = new CircuitBreaker(threshold, TimeSpan.FromSeconds(120));
}

public void ExecuteFailingAction()
{
Console.WriteLine("Before: " + Breaker.State);
try
{
CallCount = 0;
if (_allowedRetries > 0)
{
Breaker.ExecuteWithRetries(() =>
{
CallCount++;
throw new Exception("foo");
}, new RetryOptions
{
AllowedRetries = _allowedRetries,
RetryInterval = TimeSpan.FromMilliseconds(10)
});
}
else
{
Breaker.Execute(() =>
{
CallCount++;
throw new Exception("foo");
});
}
}
catch (OpenCircuitException)
{
Console.WriteLine("Tripped due to open circuit");
}
catch (OperationFailedException)
{
Console.WriteLine("Tripped due to unhandled exception");
}
catch (AggregateException ex)
{
Console.WriteLine("Tripped due to unhandled exception. InnerExceptionCount: " + ex.InnerExceptions.Count);
}

Console.WriteLine("After {0} attempt/s: {1}", CallCount, Breaker.State + Environment.NewLine);
}
}
}
22 changes: 22 additions & 0 deletions Tests/CircuitBreakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,27 @@ public double ServiceLevel(string callPattern)

return circuitBreaker.ServiceLevel;
}

[Test]
public void Execute_WhenThresholdIsSetTo4AndCalledWith4FailingActions_ShouldInvokeEachActionOnceAndTheCircuitBreakerShouldFinishOpen()
{
var testHelper = new CircuitBreakerTestHelper(4);

testHelper.ExecuteFailingAction();
Assert.AreEqual(1, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Closed, testHelper.Breaker.State);

testHelper.ExecuteFailingAction();
Assert.AreEqual(1, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Closed, testHelper.Breaker.State);

testHelper.ExecuteFailingAction();
Assert.AreEqual(1, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Closed, testHelper.Breaker.State);

testHelper.ExecuteFailingAction();
Assert.AreEqual(1, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Open, testHelper.Breaker.State);
}
}
}
14 changes: 14 additions & 0 deletions Tests/RetryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,19 @@ public void ShouldNotCallActionWhenCircuitBreakerIsOpen()
}
Assert.IsFalse(actionWasCalled);
}

[Test]
public void ExecuteWithRetries_WhenThresholdIsSetTo4AndAllowedRetriesIsSetTo3AndCalledWith2FailingActions_InvokesTheFirstAction3TimesAndInvokesTheSecondAction1TimeAndTheCircuitBreakerShouldFinishOpen()
{
var testHelper = new CircuitBreakerTestHelper(4, 3);

testHelper.ExecuteFailingAction();
Assert.AreEqual(3, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Closed, testHelper.Breaker.State);

testHelper.ExecuteFailingAction();
Assert.AreEqual(1, testHelper.CallCount);
Assert.AreEqual(CircuitBreakerState.Open, testHelper.Breaker.State);
}
}
}
1 change: 1 addition & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CircuitBreakerTestHelper.cs" />
<Compile Include="CircuitBreakerTests.cs" />
<Compile Include="ReliableParallelForEachTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down