/// <summary> /// CTOR /// </summary> /// <param name="strategy"></param> public CircutBreakerHandler(CircutBreakerStrategy strategy) { CircuitBreakerPolicy circutBreakerPolicy = Policy .Handle <TException>() .CircuitBreakerAsync(strategy.AllowedExceptionsCount, strategy.CooldownTimeout, (exception, count) => strategy.OnRetry?.Invoke(exception), strategy.OnReset); strategy.Reset = circutBreakerPolicy.Reset; strategy.Isolate = circutBreakerPolicy.Isolate; Policy = circutBreakerPolicy; }
public void CircutBreakerPolicy_WhenException_Break(bool isPositive, int executionTimes, int retriesConfiguration, int expectedExecutions) { var policyRegistry = new StrategyBuilder(); var timesExecuted = 0; var strategy = new CircutBreakerStrategy { AllowedExceptionsCount = retriesConfiguration, CooldownTimeout = TimeSpan.FromMilliseconds(100), }; var handler = policyRegistry.Add <Exception>(strategy).Build(); for (int i = 0; i < executionTimes; i++) { var executionResult = handler.Execute(() => { timesExecuted++; var num = GetNumber(isPositive); if (num <= 0) { throw new Exception(); } return(num); }); } Assert.Equal(expectedExecutions, timesExecuted); Thread.Sleep(100); var executionResult2 = handler.Execute(() => { timesExecuted++; var num = GetNumber(isPositive); if (num <= 0) { throw new Exception(); } return(num); }); Assert.Equal(++expectedExecutions, timesExecuted); }