public void ThrowsOpenCircuitExceptionWhenCallIsAttemptedIfCircuitBreakerIsOpen() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)), 10); AssertThatExceptionIsThrown <OpenCircuitException>(() => circuitBreaker.AttemptCall(protectedCode)); }
public void FailuresIsNotIncreasedWhenProtectedCodeSucceeds() { Action protectedCode = () => { return; }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); circuitBreaker.AttemptCall(protectedCode); Assert.AreEqual(0, circuitBreaker.Failures); }
public void FailuresIncreasesWhenProtectedCodeFails() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); Assert.AreEqual(0, circuitBreaker.Failures); AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)); Assert.AreEqual(1, circuitBreaker.Failures); }
public void AttemptCallCallsProtectedCode() { bool protectedCodeWasCalled = false; Action protectedCode = () => protectedCodeWasCalled = true; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); circuitBreaker.AttemptCall(protectedCode); Assert.That(protectedCodeWasCalled); }
public void ClosesIfProtectedCodeSucceedsInHalfOpenState() { var stub = new Stub(10); var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(stub.DoStuff)), 10); Thread.Sleep(100); circuitBreaker.AttemptCall(stub.DoStuff); Assert.That(circuitBreaker.IsClosed); }
public void FailuresIsResetWhenCircuitBreakerCloses() { var stub = new Stub(10); var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(stub.DoStuff)), 10); Assert.AreEqual(10, circuitBreaker.Failures); Thread.Sleep(100); circuitBreaker.AttemptCall(stub.DoStuff); Assert.AreEqual(0, circuitBreaker.Failures); }
public void OpensIfExceptionIsThrownInProtectedCodeWhenInHalfOpenState() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)), 10); Thread.Sleep(100); AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)); Assert.That(circuitBreaker.IsOpen); }
public string TestCirciutBreaker() { if (mycircuitBreaker.AttemptCall(() => { DoSomething(); }).IsClosed) { return("Called code"); } else { return("Too many failures ,Resource not available"); } }
public void OpensWhenThresholdIsReached() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)), 10); Assert.That(circuitBreaker.IsOpen); }
public void CanCloseCircuitBreaker() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)), 10); Assert.That(circuitBreaker.IsOpen); circuitBreaker.Close(); Assert.That(circuitBreaker.IsClosed); }
public void SwitchesToHalfOpenWhenTimeOutIsReachedAfterOpening() { Action protectedCode = () => { throw new ApplicationException("blah"); }; var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMilliseconds(50)); CallXAmountOfTimes(() => AssertThatExceptionIsThrown <ApplicationException>(() => circuitBreaker.AttemptCall(protectedCode)), 10); Thread.Sleep(100); Assert.That(circuitBreaker.IsHalfOpen); }