public static IHystrixCommandProperties GetCommandProperties(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
        {
            if (commandKey == null)
            {
                throw new ArgumentNullException("commandKey");
            }

            IHystrixPropertiesStrategy strategy = HystrixPlugins.Instance.PropertiesStrategy;
            string cacheKey = strategy.GetCommandPropertiesCacheKey(commandKey, setter);

            if (String.IsNullOrEmpty(cacheKey))
            {
                return(strategy.GetCommandProperties(commandKey, setter));
            }
            else
            {
                return(commandProperties.GetOrAdd(cacheKey, w =>
                {
                    if (setter == null)
                    {
                        setter = new HystrixCommandPropertiesSetter();
                    }

                    return strategy.GetCommandProperties(commandKey, setter);
                }));
            }
        }
        public void CommandMetrics_GetErrorPercentage()
        {
            HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter();
            HystrixCommandMetrics          metrics    = GetMetrics(properties);

            metrics.MarkSuccess(100);
            Assert.AreEqual(0, metrics.GetHealthCounts().ErrorPercentage);

            metrics.MarkFailure(1000);
            Assert.AreEqual(50, metrics.GetHealthCounts().ErrorPercentage);

            metrics.MarkSuccess(100);
            metrics.MarkSuccess(100);
            Assert.AreEqual(25, metrics.GetHealthCounts().ErrorPercentage);

            metrics.MarkTimeout(5000);
            metrics.MarkTimeout(5000);
            Assert.AreEqual(50, metrics.GetHealthCounts().ErrorPercentage);

            metrics.MarkSuccess(100);
            metrics.MarkSuccess(100);
            metrics.MarkSuccess(100);

            // latent
            metrics.MarkSuccess(5000);

            // 6 success + 1 latent success + 1 failure + 2 timeout = 10 total
            // latent success not considered error
            // error percentage = 1 failure + 2 timeout / 10
            Assert.AreEqual(30, metrics.GetHealthCounts().ErrorPercentage);
        }
 public HystrixPropertiesCommandDefault(HystrixCommandPropertiesSetter setter)
 {
     CircuitBreakerEnabled = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerEnabled, DefaultCircuitBreakerEnabled);
     CircuitBreakerErrorThresholdPercentage = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerErrorThresholdPercentage, DefaultCircuitBreakerErrorThresholdPercentage);
     CircuitBreakerForceClosed = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerForceClosed, DefaultCircuitBreakerForceClosed);
     CircuitBreakerForceOpen = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerForceOpen, DefaultCircuitBreakerForceOpen);
     CircuitBreakerRequestVolumeThreshold = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerRequestVolumeThreshold, DefaultCircuitBreakerRequestVolumeThreshold);
     CircuitBreakerSleepWindow = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerSleepWindow, DefaultCircuitBreakerSleepWindow);
     ExecutionIsolationSemaphoreMaxConcurrentRequests = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationSemaphoreMaxConcurrentRequests, DefaultExecutionIsolationSemaphoreMaxConcurrentRequests);
     ExecutionIsolationStrategy = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationStrategy, DefaultExecutionIsolationStrategy);
     ExecutionIsolationThreadInterruptOnTimeout = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationThreadInterruptOnTimeout, DefaultExecutionIsolationThreadInterruptOnTimeout);
     ExecutionIsolationThreadPoolKeyOverride = HystrixPropertyFactory.AsProperty<string>((string)null);
     ExecutionIsolationThreadTimeout = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationThreadTimeout, DefaultExecutionIsolationThreadTimeout);
     FallbackIsolationSemaphoreMaxConcurrentRequests = HystrixPropertyFactory.AsProperty(setter.FallbackIsolationSemaphoreMaxConcurrentRequests, DefaultFallbackIsolationSemaphoreMaxConcurrentRequests);
     FallbackEnabled = HystrixPropertyFactory.AsProperty(setter.FallbackEnabled, DefaultFallbackEnabled);
     MetricsHealthSnapshotInterval = HystrixPropertyFactory.AsProperty(setter.MetricsHealthSnapshotInterval, DefaultMetricsHealthSnapshotInterval);
     MetricsRollingPercentileBucketSize = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileBucketSize, DefaultMetricsRollingPercentileBucketSize);
     MetricsRollingPercentileEnabled = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileEnabled, DefaultMetricsRollingPercentileEnabled);
     MetricsRollingPercentileWindowInMilliseconds = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileWindowInMilliseconds, DefaultMetricsRollingPercentileWindowInMilliseconds);
     MetricsRollingPercentileWindowBuckets = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileWindowBuckets, DefaultMetricsRollingPercentileWindowBuckets);
     MetricsRollingStatisticalWindowInMilliseconds = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowInMilliseconds, DefaultMetricsRollingStatisticalWindowInMilliseconds);
     MetricsRollingStatisticalWindowBuckets = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowBuckets, DefaultMetricsRollingStatisticalWindowBuckets);
     RequestCacheEnabled = HystrixPropertyFactory.AsProperty(setter.RequestCacheEnabled, DefaultRequestCacheEnabled);
     RequestLogEnabled = HystrixPropertyFactory.AsProperty(setter.RequestLogEnabled, DefaultRequestLogEnabled);
 }
        public void CircuitBreaker_TripCircuitOnFailuresAboveThreshold()
        {
            try
            {
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter();
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // this should start as allowing requests
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsFalse(cb.IsOpen());

                // success with high latency
                metrics.MarkSuccess(400);
                metrics.MarkSuccess(400);
                metrics.MarkFailure(10);
                metrics.MarkSuccess(400);
                metrics.MarkFailure(10);
                metrics.MarkFailure(10);
                metrics.MarkSuccess(400);
                metrics.MarkFailure(10);
                metrics.MarkFailure(10);

                // this should trip the circuit as the error percentage is above the threshold
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
        public void CircuitBreaker_LowVolumeDoesNotTripCircuit()
        {
            try
            {
                int sleepWindow = 200;
                int lowVolume   = 5;

                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter().WithCircuitBreakerSleepWindowInMilliseconds(sleepWindow).WithCircuitBreakerRequestVolumeThreshold(lowVolume);
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // fail
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);

                // even though it has all failed we won't trip the circuit because the volume is low
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsFalse(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
        public void CircuitBreaker_TripCircuit()
        {
            try
            {
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter();
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                metrics.MarkSuccess(1000);
                metrics.MarkSuccess(1000);
                metrics.MarkSuccess(1000);
                metrics.MarkSuccess(1000);

                // this should still allow requests as everything has been successful
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsFalse(cb.IsOpen());

                // fail
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);

                // everything has failed in the test window so we should return false now
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
        public void CircuitBreaker_SingleTestOnOpenCircuitAfterTimeWindow()
        {
            try
            {
                int sleepWindow = 200;
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter().WithCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // fail
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);

                // everything has failed in the test window so we should return false now
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());

                // wait for sleepWindow to pass
                Thread.Sleep(sleepWindow + 50);

                // we should now allow 1 request
                Assert.IsTrue(cb.AllowRequest());
                // but the circuit should still be open
                Assert.IsTrue(cb.IsOpen());
                // and further requests are still blocked
                Assert.IsFalse(cb.AllowRequest());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
        public void CircuitBreaker_TripCircuitOnTimeouts()
        {
            try
            {
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter();
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // this should start as allowing requests
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsFalse(cb.IsOpen());

                // timeouts
                metrics.MarkTimeout(2000);
                metrics.MarkTimeout(2000);
                metrics.MarkTimeout(2000);
                metrics.MarkTimeout(2000);

                // everything has been a timeout so we should not allow any requests
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
 public HystrixPropertiesCommandDefault(HystrixCommandPropertiesSetter setter)
 {
     CircuitBreakerEnabled = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerEnabled, DefaultCircuitBreakerEnabled);
     CircuitBreakerErrorThresholdPercentage = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerErrorThresholdPercentage, DefaultCircuitBreakerErrorThresholdPercentage);
     CircuitBreakerForceClosed                        = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerForceClosed, DefaultCircuitBreakerForceClosed);
     CircuitBreakerForceOpen                          = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerForceOpen, DefaultCircuitBreakerForceOpen);
     CircuitBreakerRequestVolumeThreshold             = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerRequestVolumeThreshold, DefaultCircuitBreakerRequestVolumeThreshold);
     CircuitBreakerSleepWindow                        = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerSleepWindow, DefaultCircuitBreakerSleepWindow);
     ExecutionIsolationSemaphoreMaxConcurrentRequests = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationSemaphoreMaxConcurrentRequests, DefaultExecutionIsolationSemaphoreMaxConcurrentRequests);
     ExecutionIsolationStrategy                       = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationStrategy, DefaultExecutionIsolationStrategy);
     ExecutionIsolationThreadInterruptOnTimeout       = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationThreadInterruptOnTimeout, DefaultExecutionIsolationThreadInterruptOnTimeout);
     ExecutionIsolationThreadPoolKeyOverride          = HystrixPropertyFactory.AsProperty <string>((string)null);
     ExecutionIsolationThreadTimeout                  = HystrixPropertyFactory.AsProperty(setter.ExecutionIsolationThreadTimeout, DefaultExecutionIsolationThreadTimeout);
     FallbackIsolationSemaphoreMaxConcurrentRequests  = HystrixPropertyFactory.AsProperty(setter.FallbackIsolationSemaphoreMaxConcurrentRequests, DefaultFallbackIsolationSemaphoreMaxConcurrentRequests);
     FallbackEnabled = HystrixPropertyFactory.AsProperty(setter.FallbackEnabled, DefaultFallbackEnabled);
     MetricsHealthSnapshotInterval                 = HystrixPropertyFactory.AsProperty(setter.MetricsHealthSnapshotInterval, DefaultMetricsHealthSnapshotInterval);
     MetricsRollingPercentileBucketSize            = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileBucketSize, DefaultMetricsRollingPercentileBucketSize);
     MetricsRollingPercentileEnabled               = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileEnabled, DefaultMetricsRollingPercentileEnabled);
     MetricsRollingPercentileWindowInMilliseconds  = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileWindowInMilliseconds, DefaultMetricsRollingPercentileWindowInMilliseconds);
     MetricsRollingPercentileWindowBuckets         = HystrixPropertyFactory.AsProperty(setter.MetricsRollingPercentileWindowBuckets, DefaultMetricsRollingPercentileWindowBuckets);
     MetricsRollingStatisticalWindowInMilliseconds = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowInMilliseconds, DefaultMetricsRollingStatisticalWindowInMilliseconds);
     MetricsRollingStatisticalWindowBuckets        = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowBuckets, DefaultMetricsRollingStatisticalWindowBuckets);
     RequestCacheEnabled = HystrixPropertyFactory.AsProperty(setter.RequestCacheEnabled, DefaultRequestCacheEnabled);
     RequestLogEnabled   = HystrixPropertyFactory.AsProperty(setter.RequestLogEnabled, DefaultRequestLogEnabled);
 }
        public MockingHystrixCommandProperties(HystrixCommandPropertiesSetter setter)
        {
            if (setter == null)
            {
                throw new ArgumentNullException("setter");
            }

            this.setter = setter;
        }
        public void CircuitBreaker_CircuitClosedAfterSuccessAndClearsStatisticalWindow()
        {
            try
            {
                int statisticalWindow = 200;
                int sleepWindow       = 10; // this is set very low so that returning from a retry still ends up having data in the buckets for the statisticalWindow
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter().WithCircuitBreakerSleepWindowInMilliseconds(sleepWindow).WithMetricsRollingStatisticalWindowInMilliseconds(statisticalWindow);
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // fail
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);

                // everything has failed in the test window so we should return false now
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());

                // wait for sleepWindow to pass
                Thread.Sleep(sleepWindow + 50);

                // we should now allow 1 request
                Assert.IsTrue(cb.AllowRequest());
                // but the circuit should still be open
                Assert.IsTrue(cb.IsOpen());
                // and further requests are still blocked
                Assert.IsFalse(cb.AllowRequest());

                // the 'singleTest' succeeds so should cause the circuit to be closed
                metrics.MarkSuccess(500);
                cb.MarkSuccess();

                // all requests should be open again
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsTrue(cb.AllowRequest());
                // and the circuit should be closed again
                Assert.IsFalse(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
 public HystrixPropertiesCommandDefault(HystrixCommandPropertiesSetter setter)
 {
     CircuitBreakerEnabled = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerEnabled, DefaultCircuitBreakerEnabled);
     CircuitBreakerErrorThresholdPercentage = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerErrorThresholdPercentage, DefaultCircuitBreakerErrorThresholdPercentage);
     // dynamic property can be updated at runtime
     CircuitBreakerForceClosed = HystrixPropertyFactory.AsDynamicProperty(setter.CircuitBreakerForceClosed, DefaultCircuitBreakerForceClosed);
     // dynamic property can be updated at runtime
     CircuitBreakerForceOpen = HystrixPropertyFactory.AsDynamicProperty(setter.CircuitBreakerForceOpen, DefaultCircuitBreakerForceOpen);
     CircuitBreakerRequestVolumeThreshold = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerRequestVolumeThreshold, DefaultCircuitBreakerRequestVolumeThreshold);
     CircuitBreakerSleepWindow            = HystrixPropertyFactory.AsProperty(setter.CircuitBreakerSleepWindow, DefaultCircuitBreakerSleepWindow);
     // dynamic property can be updated at runtime
     ExecutionIsolationThreadTimeout = HystrixPropertyFactory.AsDynamicProperty(setter.ExecutionIsolationThreadTimeout);
     MetricsHealthSnapshotInterval   = HystrixPropertyFactory.AsProperty(setter.MetricsHealthSnapshotInterval, DefaultMetricsHealthSnapshotInterval);
     MetricsRollingStatisticalWindowInMilliseconds = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowInMilliseconds, DefaultMetricsRollingStatisticalWindowInMilliseconds);
     MetricsRollingStatisticalWindowBuckets        = HystrixPropertyFactory.AsProperty(setter.MetricsRollingStatisticalWindowBuckets, DefaultMetricsRollingStatisticalWindowBuckets);
     MetricsIntegerBufferTimeWindowInSeconds       = HystrixPropertyFactory.AsProperty(setter.MetricsIntegerBufferTimeWindowInSeconds, DefaultMetricsIntegerBufferTimeWindowInSeconds);
     MetricsIntegerBufferBucketTimeWindowInSeconds = HystrixPropertyFactory.AsProperty(setter.MetricsIntegerBufferBucketTimeWindowInSeconds, DefaultMetricsIntegerBufferBucketTimeWindowInSeconds);
     MetricsIntegerBufferBucketSizeLimit           = HystrixPropertyFactory.AsProperty(setter.MetricsIntegerBufferBucketSizeLimit, DefaultMetricsIntegerBufferBucketSizeLimit);
     RequestLogEnabled = HystrixPropertyFactory.AsProperty(setter.RequestLogEnabled, DefaultRequestLogEnabled);
 }
        public static IHystrixCommandProperties GetCommandProperties(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
        {
            if (commandKey == null)
                throw new ArgumentNullException("commandKey");

            IHystrixPropertiesStrategy strategy = HystrixPlugins.Instance.PropertiesStrategy;
            string cacheKey = strategy.GetCommandPropertiesCacheKey(commandKey, setter);
            if (String.IsNullOrEmpty(cacheKey))
            {
                return strategy.GetCommandProperties(commandKey, setter);
            }
            else
            {
                return commandProperties.GetOrAdd(cacheKey, w =>
                {
                    if (setter == null)
                    {
                        setter = new HystrixCommandPropertiesSetter();
                    }

                    return strategy.GetCommandProperties(commandKey, setter);
                });
            }
        }
示例#14
0
 public CommandWithCustomThreadPool(TestCircuitBreaker circuitBreaker, IHystrixThreadPool threadPool, TimeSpan sleepTime, HystrixCommandPropertiesSetter properties)
     : base(new TestCommandBuilder()
 {
     ThreadPool                = threadPool,
     CircuitBreaker            = circuitBreaker,
     Metrics                   = circuitBreaker.Metrics,
     CommandPropertiesDefaults = properties,
 })
 {
     this.sleepTime = sleepTime;
 }
 public virtual string GetCommandPropertiesCacheKey(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     return commandKey.Name;
 }
        public void CircuitBreaker_MultipleTimeWindowRetriesBeforeClosingCircuit()
        {
            try
            {
                int sleepWindow = 200;
                HystrixCommandPropertiesSetter properties = UnitTestSetterFactory.GetCommandPropertiesSetter().WithCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
                HystrixCommandMetrics          metrics    = GetMetrics(properties);
                IHystrixCircuitBreaker         cb         = GetCircuitBreaker(key, CommandGroupForUnitTest.OwnerTwo, metrics, properties);

                // fail
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);
                metrics.MarkFailure(1000);

                // everything has failed in the test window so we should return false now
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsTrue(cb.IsOpen());

                // wait for sleepWindow to pass
                Thread.Sleep(sleepWindow + 50);

                // we should now allow 1 request
                Assert.IsTrue(cb.AllowRequest());
                // but the circuit should still be open
                Assert.IsTrue(cb.IsOpen());
                // and further requests are still blocked
                Assert.IsFalse(cb.AllowRequest());

                // the 'singleTest' fails so it should go back to sleep and not allow any requests again until another 'singleTest' after the sleep
                metrics.MarkFailure(1000);

                Assert.IsFalse(cb.AllowRequest());
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsFalse(cb.AllowRequest());

                // wait for sleepWindow to pass
                Thread.Sleep(sleepWindow + 50);

                // we should now allow 1 request
                Assert.IsTrue(cb.AllowRequest());
                // but the circuit should still be open
                Assert.IsTrue(cb.IsOpen());
                // and further requests are still blocked
                Assert.IsFalse(cb.AllowRequest());

                // the 'singleTest' fails again so it should go back to sleep and not allow any requests again until another 'singleTest' after the sleep
                metrics.MarkFailure(1000);

                Assert.IsFalse(cb.AllowRequest());
                Assert.IsFalse(cb.AllowRequest());
                Assert.IsFalse(cb.AllowRequest());

                // wait for sleepWindow to pass
                Thread.Sleep(sleepWindow + 50);

                // we should now allow 1 request
                Assert.IsTrue(cb.AllowRequest());
                // but the circuit should still be open
                Assert.IsTrue(cb.IsOpen());
                // and further requests are still blocked
                Assert.IsFalse(cb.AllowRequest());

                // now it finally succeeds
                metrics.MarkSuccess(200);
                cb.MarkSuccess();

                // all requests should be open again
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsTrue(cb.AllowRequest());
                Assert.IsTrue(cb.AllowRequest());
                // and the circuit should be closed again
                Assert.IsFalse(cb.IsOpen());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Assert.Fail("Error occurred: " + e.Message);
            }
        }
 public string GetCommandPropertiesCacheKey(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     return(null);
 }
 public virtual IHystrixCommandProperties GetCommandProperties(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     return new HystrixPropertiesCommandDefault(setter);
 }
 private static IHystrixCircuitBreaker GetCircuitBreaker(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandMetrics metrics, HystrixCommandPropertiesSetter properties)
 {
     return(new HystrixCircuitBreakerImpl(new MockingHystrixCommandProperties(properties), metrics));
 }
 public IHystrixCommandProperties GetCommandProperties(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     if (setter == null)
     {
         setter = UnitTestSetterFactory.GetCommandPropertiesSetter();
     }
     return(new MockingHystrixCommandProperties(setter));
 }
 /**
  * Utility method for creating {@link HystrixCommandMetrics} for unit tests.
  */
 private static HystrixCommandMetrics GetMetrics(HystrixCommandPropertiesSetter properties)
 {
     return(new HystrixCommandMetrics(CommandKeyForUnitTest.KeyOne, CommandGroupForUnitTest.OwnerOne, new MockingHystrixCommandProperties(properties), HystrixEventNotifierDefault.Instance));
 }
示例#22
0
        public void Add(Type serviceType, MethodInfo mi, Type requestType, Type responseType, bool isAsync)
        {
            this.RequestTypes.Add(requestType);

            object[] methodCustomAttributes = mi.GetCustomAttributes(true);
            object[] classCustomAttributes  = serviceType.GetCustomAttributes(true);

            var restrictTo = methodCustomAttributes.OfType <RestrictAttribute>().FirstOrDefault()
                             ?? classCustomAttributes.OfType <RestrictAttribute>().FirstOrDefault();

            var operation = new Operation
            {
                Name         = mi.Name,
                Key          = string.Format("{0}.{1}", this.RefinedFullServiceName, mi.Name.ToLower()),
                ServiceType  = serviceType,
                Method       = mi,
                RequestType  = requestType,
                ResponseType = responseType,
                RestrictTo   = restrictTo,
                Routes       = new List <RestPath>(),
                Descritpion  = mi.GetDescription(),
                IsAsync      = isAsync,
            };

            var hystrixCommandPropertiesSetter = new HystrixCommandPropertiesSetter();

            hystrixCommandPropertiesSetter.WithCircuitBreakerForceClosed(CircuitBreakerForceClosed);

            // operation timeout attribute override
            var hystrixAttribute = methodCustomAttributes.OfType <HystrixAttribute>().FirstOrDefault();

            if (hystrixAttribute != null && hystrixAttribute.Timeout > 0)
            {
                hystrixCommandPropertiesSetter.WithExecutionIsolationThreadTimeoutInMilliseconds(hystrixAttribute.Timeout);
                Log.Info(string.Format("Timeout for operation {0} is overridden by attribute to value {1}", mi.Name, hystrixAttribute.Timeout),
                         new Dictionary <string, string>()
                {
                    { "ErrorCode", "FXD300046" }
                });
            }

            // operation timeout setting override in appSettings
            if (OperationTimeoutMap != null && OperationTimeoutMap.ContainsKey(mi.Name))
            {
                var timeoutSetting = OperationTimeoutMap[mi.Name];
                try
                {
                    var timeout = int.Parse(timeoutSetting);
                    if (timeout > 0)
                    {
                        hystrixCommandPropertiesSetter.WithExecutionIsolationThreadTimeoutInMilliseconds(timeout);
                        Log.Info(string.Format("Timeout for operation {0} is overridden in appSettings to value {1}", mi.Name, timeout),
                                 new Dictionary <string, string>()
                        {
                            { "ErrorCode", "FXD300047" }
                        });
                    }
                    else
                    {
                        Log.Error(
                            string.Format("Invalid operation timeout setting {0}:{1} in appSettings", mi.Name, timeoutSetting),
                            new Dictionary <string, string>()
                        {
                            { "ErrorCode", "FXD300006" }
                        });
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(
                        string.Format("Invalid operation timeout setting {0}:{1} in appSettings", mi.Name, timeoutSetting),
                        ex,
                        new Dictionary <string, string>()
                    {
                        { "ErrorCode", "FXD300006" }
                    });
                }
            }

            var hystrixCommnad = new HystrixCommand(ServicePath, mi.Name, ServiceName, FullServiceName, ServiceMetricPrefix, hystrixCommandPropertiesSetter);

            operation.HystrixCommand = hystrixCommnad;

            operation.RequestFilters = methodCustomAttributes.OfType <IHasRequestFilter>().ToList();
            operation.RequestFilters.AddRange(classCustomAttributes.OfType <IHasRequestFilter>().ToList());
            operation.ResponseFilters = methodCustomAttributes.OfType <IHasResponseFilter>().ToList();
            operation.ResponseFilters.AddRange(classCustomAttributes.OfType <IHasResponseFilter>().ToList());

            var sensitivityAttribute = methodCustomAttributes.OfType <MessageSensitivityAttribute>().FirstOrDefault();

            operation.OperationMessageLogConfig = AttributeToConfig(sensitivityAttribute);

            this.OperationNameMap[mi.Name.ToLower()] = operation;

            if (responseType != null)
            {
                this.ResponseTypes.Add(responseType);
            }
        }
示例#23
0
 public SuccessfulTestCommand(HystrixCommandPropertiesSetter properties)
     : base(new TestCommandBuilder() { CommandPropertiesDefaults = properties })
 {
 }
 public virtual string GetCommandPropertiesCacheKey(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     return(commandKey.Name);
 }
 public virtual IHystrixCommandProperties GetCommandProperties(HystrixCommandKey commandKey, HystrixCommandPropertiesSetter setter)
 {
     return(new HystrixPropertiesCommandDefault(setter));
 }