public void ConfigCommand_Key_Group_Domain_MaxConcurrentCount()
 {
     HystrixCommandBase.ConfigCommand <string>(TestCommandKey, TestGroupKey, TestDomain, CustomConfigSet.CommandMaxConcurrentCount);
     Assert.AreEqual(TestGroupKey, CommandComponents.CommandInfo.GroupKey, true);
     Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true);
     Assert.AreEqual(CustomConfigSet.CommandMaxConcurrentCount, CommandComponents.ConfigSet.CommandMaxConcurrentCount);
 }
Example #2
0
        public void CircuitBreakerForceClosedIsFalseWithoutFallback()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = false;

            string expected = string.Empty;

            for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++)
            {
                try
                {
                    ScenarioTestHelper.SleepHealthSnapshotInverval();
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                    Assert.Fail("Execution should throw exception.");
                }
                catch (ScenarioTestException)
                {
                }
            }

            try
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException ex)
            {
                Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
            }
        }
 public void ConfigAllCommandSettings()
 {
     Assert.IsFalse(ScenarioTestHelper.AreEqual(DefaultConfigSet, CustomConfigSet));
     HystrixCommandBase.ConfigCommand <string>(TestCommandKey,
                                               configSet => ScenarioTestHelper.SetCommandConfigFrom(configSet, CustomConfigSet));
     Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet));
 }
        private void RunToTimeoutNormally()
        {
            ScenarioTestHelper.SleepHealthSnapshotInverval();
            string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout);

            Assert.AreEqual(Expected, actual);
        }
 public void ConfigCommand_Key_Domain_Config()
 {
     HystrixCommandBase.ConfigCommand <string>(TestCommandKey, TestDomain,
                                               config => ScenarioTestHelper.SetCommandConfigFrom(config, CustomConfigSet));
     Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true);
     Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet));
 }
        public void IsBadRequestException_NonCustomBadRequestException()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker("custom", ex => ex is IndexOutOfRangeException);
            Exception ex2 = new Exception();

            Assert.IsFalse(ex2.IsBadRequestException());
        }
        public void RunCommandFailAndFallbackSuccess()
        {
            string fallback = string.Empty;
            string actual   = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new Exception(); }, () => fallback);

            Assert.AreEqual(fallback, actual);
        }
Example #8
0
        public void ExecuteAsync_ShouldExecute_MethodWithOutParametersRetuningVoid()
        {
            // Arrange

            var mockService = new MockService();
            var primaryArgs = new Type[] { };

            MethodInfo methodInfo = typeof(MockService).GetMethod(nameof(MockService.MethodReturnsTask), primaryArgs);

            var primaryFuncArgs = primaryArgs.Concat(new Type[] { methodInfo.ReturnType }).ToArray();

            var primaryDelegate = methodInfo.CreateDelegate(Expression.GetDelegateType(primaryFuncArgs), mockService);

            var fallbackArgs = new Type[] { typeof(HystrixFallback) };

            methodInfo = typeof(MockService).GetMethod(nameof(MockService.MethodRetunsVoid), fallbackArgs);

            var fallbackDelegate = methodInfo.CreateDelegate(Expression.GetActionType(fallbackArgs), mockService);

            var options = MockService.GetCommandOptions(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            //Act
            var command = new HystrixCommandBase(options, primaryDelegate, fallbackDelegate, new object[] { }, _loggerFactory);

            command.Execute();
        }
Example #9
0
        private void TestRequestCountThresholdSetting(int?setting = null)
        {
            if (setting.HasValue)
            {
                CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold = setting.Value;
            }
            else
            {
                setting = CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold;
            }

            for (int i = 0; i < setting.Value; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                try
                {
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                    Assert.Fail("Execution should throw exception.");
                }
                catch (ScenarioTestException)
                {
                }
            }

            ScenarioTestHelper.SleepHealthSnapshotInverval();
            try
            {
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException)
            {
            }
        }
Example #10
0
        public SemaphoreIsolation(string instanceKey, string commandKey, string groupKey, string domain, Action <ICommandConfigSet> config)
        {
            Func <string, CommandComponents>  valueFactory = null;
            Func <string, IsolationSemaphore> func2        = null;

            if (string.IsNullOrWhiteSpace(commandKey))
            {
                string message = "HystrixCommand Key cannot be null.";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, message, new Dictionary <string, string>().AddLogTagData("FXD303002"));
                throw new ArgumentNullException(message);
            }
            this.Key    = CommonUtils.GenerateKey(instanceKey, commandKey);
            instanceKey = string.IsNullOrWhiteSpace(instanceKey) ? null : instanceKey.Trim();
            commandKey  = commandKey.Trim();
            groupKey    = groupKey ?? "DefaultGroup";
            domain      = domain ?? "Ant";
            if (valueFactory == null)
            {
                valueFactory = key => HystrixCommandBase.CreateCommandComponents(this.Key, instanceKey, commandKey, groupKey, domain, IsolationModeEnum.SemaphoreIsolation, config, typeof(SemaphoreIsolation));
            }
            this.Components = HystrixCommandBase.CommandComponentsCollection.GetOrAdd(this.Key, valueFactory);
            if (func2 == null)
            {
                func2 = key => new IsolationSemaphore(this.Components.ConfigSet.CommandMaxConcurrentCount);
            }
            this.ExecutionSemaphore = HystrixCommandBase.ExecutionSemaphores.GetOrAdd(this.Key, func2);
            this._stopwatch         = new Stopwatch();
        }
Example #11
0
        public void CircuitBreakerForceClosedIsTrueWithoutFallback()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = true;

            for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++)
            {
                try
                {
                    ScenarioTestHelper.SleepHealthSnapshotInverval();
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                    Assert.Fail("Execution should throw exception.");
                }
                catch (ScenarioTestException)
                {
                }
            }

            try
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                Assert.Fail("Execution should throw exception.");
            }
            catch (ScenarioTestException)
            {
            }
        }
Example #12
0
        public void ExecuteAsync_ShouldExecute_PrimaryWhenNoTimeoutOnIt()
        {
            // Arrange
            var executationResult = string.Empty;

            var mockService = new MockService();

            var methodInfo = default(MethodInfo);

            var primaryArgs = new Type[] { typeof(int) };

            methodInfo = typeof(MockService).GetMethod(nameof(MockService.MethodWithTimeoutTest), primaryArgs);

            var primaryFuncArgs = primaryArgs.Concat(new Type[] { methodInfo.ReturnType }).ToArray();

            var primaryDelegate = methodInfo.CreateDelegate(Expression.GetDelegateType(primaryFuncArgs), mockService);

            var fallbackArgs = new Type[] { typeof(HystrixFallback), typeof(int) };

            methodInfo = typeof(MockService).GetMethod(nameof(MockService.MethodWithTimeoutTest), fallbackArgs);

            var fallbackFuncArgs = fallbackArgs.Concat(new Type[] { methodInfo.ReturnType }).ToArray();

            var fallbackDelegate = methodInfo.CreateDelegate(Expression.GetFuncType(fallbackFuncArgs), mockService);

            var options = MockService.GetCommandOptions(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            //Act
            var command = new HystrixCommandBase <IEnumerable <Type> >(options, primaryDelegate, fallbackDelegate, new object[] { 10 }, _loggerFactory);
            var result  = command.Execute();

            // Assert
            result.Should().BeEquivalentTo(primaryArgs);
        }
Example #13
0
        public void CircuitBreakerForceOpenIsFalseWithFallback()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceOpen = false;
            string result = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty, () => null);

            Assert.AreEqual(string.Empty, result);
        }
Example #14
0
        private void TestErrorPercentageThresholdSetting100()
        {
            CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage = 100;

            for (int i = 0; i < 100; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                try
                {
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                    Assert.Fail("Execution should throw exception.");
                }
                catch (ScenarioTestException)
                {
                    if (i >= CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold)
                    {
                        Assert.Fail("Execution should throw HystrixException.");
                    }
                }
                catch (HystrixException ex)
                {
                    if (i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold)
                    {
                        Assert.Fail("Execution should throw ScenarioTestException.");
                    }
                    Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
                }
            }
        }
Example #15
0
        public void CustomBadRequestNotCauseCircuitBreakerOpen_CheckerThrowException()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(WithExceptionCheckerName, IsBadRequestExceptionWithException);

            CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold = 2;
            int circuitBreakerCount = 0;

            for (int i = 0; i < 100; i++)
            {
                SampleIsolationCommand command = new SampleIsolationCommand(
                    TestCommandKey,
                    execute: () => { throw new ArgumentOutOfRangeException(); });
                try
                {
                    command.Run();
                }
                catch (ArgumentOutOfRangeException)
                {
                }
                catch (HystrixException)
                {
                }

                ScenarioTestHelper.SleepHealthSnapshotInverval();
                Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();
                circuitBreakerCount += counts[CommandExecutionEventEnum.ShortCircuited];
            }

            Assert.AreNotEqual(0, circuitBreakerCount);
        }
        private void RunToSuccessNormally()
        {
            ScenarioTestHelper.SleepHealthSnapshotInverval();
            string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => Expected);

            Assert.AreEqual(Expected, actual);
        }
        public void RunCommandSuccessAndFallbackFail()
        {
            string expected = string.Empty;
            string actual   = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected,
                                                                     () => { throw new ScenarioTestException(); });

            Assert.AreEqual(expected, actual);
        }
 public void ConfigCommandWithDefaultConfig()
 {
     HystrixCommandBase.ConfigCommand <string>(TestCommandKey, config => {});
     Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true);
     Assert.AreEqual(HystrixCommandBase.DefaultGroupKey, CommandComponents.CommandInfo.GroupKey, true);
     Assert.AreEqual(CommandDomains.Default, CommandComponents.CommandInfo.Domain, true);
     Assert.IsTrue(ScenarioTestHelper.AreEqual(DefaultConfigSet, CommandComponents.ConfigSet));
 }
Example #19
0
 public void RegisterCustomBadRequestExceptionChecker_MultiAddWithSameNameOnlyUseTheFirstAddAndIngoreTheLatters()
 {
     HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);
     HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestExceptionWithException);
     Assert.AreNotEqual(IsBadRequestException, IsBadRequestExceptionWithException);
     Assert.AreEqual(IsBadRequestException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName]);
     Assert.AreNotEqual(IsBadRequestExceptionWithException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName]);
 }
        public void RunCommandSuccessAndFallbackSuccess()
        {
            string expected = string.Empty;
            string fallback = null;
            string actual   = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected,
                                                                     () => fallback);

            Assert.AreEqual(expected, actual);
        }
Example #21
0
 public void RegisterCustomBadRequestExceptionChecker_IgnoreNameCase()
 {
     HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);
     Assert.AreEqual(1, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers.Count);
     Assert.AreEqual(IsBadRequestException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName]);
     Assert.AreEqual(CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName],
                     CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName.ToUpper()]);
     Assert.AreEqual(CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName],
                     CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName.ToLower()]);
 }
Example #22
0
        public void CircuitBreakerOpenCausedByTimeout()
        {
            int timeoutInMilliseconds = 10;

            CommandComponents.ConfigSet.CommandTimeoutInMilliseconds = timeoutInMilliseconds;

            string        expected = string.Empty;
            string        actual;
            Func <string> execute = () =>
            {
                Thread.Sleep(timeoutInMilliseconds + 2);
                return(expected);
            };

            for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, execute);
                Assert.AreEqual(expected, actual);
            }

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    ScenarioTestHelper.SleepHealthSnapshotInverval();
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, execute);
                    var snapshot = CommandComponents.Metrics.GetExecutionEventDistribution();
                    Assert.Fail("Execution should throw exception.");
                }
                catch (HystrixException ex)
                {
                    Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
                }
            }

            Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1);
            actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, execute);
            Assert.AreEqual(expected, actual);
            try
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                HystrixCommandBase.RunCommand <string>(TestCommandKey, execute);
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException ex)
            {
                Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
            }

            Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1);
            actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected);
            Assert.AreEqual(expected, actual);
        }
Example #23
0
        public void CustomBadRequestExecuteFallback()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);

            SampleHasFallbackIsolationCommand command = new SampleHasFallbackIsolationCommand(
                TestCommandKey,
                execute: () => { throw new ArgumentOutOfRangeException(); },
                fallback: () => string.Empty);

            command.Run();
        }
Example #24
0
        public void RegisterCustomBadRequestExceptionChecker_AddCustomCheckerSuccess()
        {
            Assert.AreEqual(0, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers.Count);
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);
            Assert.AreEqual(1, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers.Count);
            Assert.AreEqual(IsBadRequestException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName]);

            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(WithExceptionCheckerName, IsBadRequestExceptionWithException);
            Assert.AreEqual(2, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers.Count);
            Assert.AreEqual(IsBadRequestException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[NormalCheckerName]);
            Assert.AreEqual(IsBadRequestExceptionWithException, CustomBadRequestExceptionChecker.BadRequestExceptionCheckers[WithExceptionCheckerName]);
        }
Example #25
0
        public void CustomBadRequestNotExecuteFallback_CheckerThrowException()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(WithExceptionCheckerName, IsBadRequestExceptionWithException);

            SampleHasFallbackIsolationCommand command = new SampleHasFallbackIsolationCommand(
                TestCommandKey,
                execute: () => { throw new ArgumentOutOfRangeException(); },
                fallback: () => string.Empty);
            string result = command.Run();

            Assert.AreEqual(string.Empty, result);
        }
 private void RunToExceptionNormally()
 {
     ScenarioTestHelper.SleepHealthSnapshotInverval();
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
         Assert.Fail("Execution should throw exception.");
     }
     catch (ScenarioTestException)
     {
     }
 }
 private void RunToTimeoutShortCircuited()
 {
     ScenarioTestHelper.SleepHealthSnapshotInverval();
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout);
     }
     catch (HystrixException ex)
     {
         Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
     }
 }
Example #28
0
 public void CircuitBreakerForceOpenIsTrueWithoutFallback()
 {
     CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceOpen = true;
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty);
         Assert.Fail("Execution should throw exception.");
     }
     catch (HystrixException ex)
     {
         Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
     }
 }
        public void TestAsyncCommandSuccessful()
        {
            Console.WriteLine(13 % 2);
            var result = HystrixCommandBase.RunCommandAsync <bool>("testasync", () =>
            {
                return(true);
            }, () =>
            {
                return(false);
            });

            Assert.AreEqual(true, result.Result);
        }
 private void RunToExceptionShortCircuited()
 {
     ScenarioTestHelper.SleepHealthSnapshotInverval();
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
         Assert.Fail("Execution should throw exception.");
     }
     catch (HystrixException ex)
     {
         Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
     }
 }