public void FailuresIsNotIncreasedWhenProtectedCodeSucceeds() { Action protectedCode = () => { return; }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); circuitBreaker.AttemptCall(protectedCode); Assert.Equal(0, circuitBreaker.Failures); }
public void AttemptCallCallsProtectedCode() { Boolean protectedCodeWasCalled = false; Action protectedCode = () => protectedCodeWasCalled = true; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); circuitBreaker.AttemptCall(protectedCode); Assert.True(protectedCodeWasCalled); }
public ClosedState(CircuitBreaker circuitBreaker) : base(circuitBreaker) { if (circuitBreaker == null) { throw new ArgumentNullException("circuitBreaker"); } circuitBreaker.ResetFailureCount(); }
public OpenState(CircuitBreaker circuitBreaker) : base(circuitBreaker) { if (circuitBreaker == null) { throw new ArgumentNullException("circuitBreaker"); } m_timer = new Timer(TimeoutHasBeenReached, null, circuitBreaker.Timeout, TimeSpan.FromMilliseconds(-1)); }
public void FailuresIncreasesWhenProtectedCodeFails() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); Assert.Equal(0, circuitBreaker.Failures); Assert.Throws<Exception>(() => { circuitBreaker.AttemptCall(protectedCode); }); Assert.Equal(1, circuitBreaker.Failures); }
public void CanOpenCircuitBreaker() { var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); Assert.True(circuitBreaker.IsClosed); circuitBreaker.Open(); Assert.True(circuitBreaker.IsOpen); }
public void CanCloseCircuitBreaker() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(protectedCode); } catch { // Do nothing. } }, 10); Assert.True(circuitBreaker.IsOpen); circuitBreaker.Close(); Assert.True(circuitBreaker.IsClosed); }
public void FailuresIsResetWhenCircuitBreakerCloses() { var stub = new Stub(10); var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(stub.DoStuff); } catch { // Do nothing. } }, 10); Assert.Equal(10, circuitBreaker.Failures); Thread.Sleep(100); circuitBreaker.AttemptCall(stub.DoStuff); Assert.Equal(0, circuitBreaker.Failures); }
public void ClosesIfProtectedCodeSucceedsInHalfOpenState() { var stub = new Stub(10); var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(stub.DoStuff); } catch { // Do nothing. } }, 10); Thread.Sleep(100); circuitBreaker.AttemptCall(stub.DoStuff); Assert.True(circuitBreaker.IsClosed); }
protected CircuitBreakerState(CircuitBreaker circuitBreaker) { m_circuitBreaker = circuitBreaker; }
public void OpensIfExceptionIsThrownInProtectedCodeWhenInHalfOpenState() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(protectedCode); } catch { // Do nothing. } }, 10); Thread.Sleep(100); Assert.Throws<Exception>(() => { circuitBreaker.AttemptCall(protectedCode); }); Assert.True(circuitBreaker.IsOpen); }
public void SwitchesToHalfOpenWhenTimeOutIsReachedAfterOpening() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(protectedCode); } catch { // Do nothing. } }, 10); Thread.Sleep(100); Assert.True(circuitBreaker.IsHalfOpen); }
public void ThrowsOpenCircuitExceptionWhenCallIsAttemptedIfCircuitBreakerIsOpen() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(protectedCode); } catch { // Do nothing. } }, 10); Assert.Throws<Exception<OpenCircuitExceptionArgs>>(() => { circuitBreaker.AttemptCall(protectedCode); }); }
public void TestConstructorWithValidArguments() { var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); Assert.Equal(10, circuitBreaker.Threshold); Assert.Equal(TimeSpan.FromMinutes(5), circuitBreaker.Timeout); }
public void NewCircuitBreakerIsClosed() { var circuitBreaker = new CircuitBreaker(5, TimeSpan.FromMinutes(5)); Assert.True(circuitBreaker.IsClosed); }
public void OpensWhenThresholdIsReached() { Action protectedCode = () => { throw new Exception("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); CallXAmountOfTimes(() => { try { circuitBreaker.AttemptCall(protectedCode); } catch { // Do nothing. } }, 10); Assert.True(circuitBreaker.IsOpen); }
public HalfOpenState(CircuitBreaker circuitBreaker) : base(circuitBreaker) { }