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 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 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 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);
		}
		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);
		}
Exemplo n.º 10
0
		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);
			});
		}