예제 #1
0
        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));
        }
예제 #2
0
        public void FailuresIsNotIncreasedWhenProtectedCodeSucceeds()
        {
            Action protectedCode = () => { return; };

            var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5));

            circuitBreaker.AttemptCall(protectedCode);
            Assert.AreEqual(0, circuitBreaker.Failures);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void AttemptCallCallsProtectedCode()
        {
            bool   protectedCodeWasCalled = false;
            Action protectedCode          = () => protectedCodeWasCalled = true;

            var circuitBreaker = new CircuitBreaker(10, TimeSpan.FromMinutes(5));

            circuitBreaker.AttemptCall(protectedCode);
            Assert.That(protectedCodeWasCalled);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
 public string TestCirciutBreaker()
 {
     if (mycircuitBreaker.AttemptCall(() => { DoSomething(); }).IsClosed)
     {
         return("Called code");
     }
     else
     {
         return("Too many failures ,Resource not available");
     }
 }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }