Exemplo n.º 1
0
        public void CircuitBreakerForceOpenIsFalseWithFallback()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceOpen = false;
            string result = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty, () => null);

            Assert.AreEqual(string.Empty, result);
        }
Exemplo n.º 2
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)
            {
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        private void RunToTimeoutNormally()
        {
            ScenarioTestHelper.SleepHealthSnapshotInverval();
            string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout);

            Assert.AreEqual(Expected, actual);
        }
Exemplo n.º 5
0
        private void RunToSuccessNormally()
        {
            ScenarioTestHelper.SleepHealthSnapshotInverval();
            string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => Expected);

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

            Assert.AreEqual(fallback, actual);
        }
Exemplo n.º 7
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);
                }
            }
        }
Exemplo n.º 8
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)
            {
            }
        }
Exemplo n.º 9
0
 public void RunCommandWithDefaultConfig()
 {
     HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty);
     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));
 }
        public void RunCommandSuccessAndFallbackFail()
        {
            string expected = string.Empty;
            string actual   = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected,
                                                                     () => { throw new ScenarioTestException(); });

            Assert.AreEqual(expected, actual);
        }
        public void RunCommandSuccessAndFallbackSuccess()
        {
            string expected = string.Empty;
            string fallback = null;
            string actual   = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected,
                                                                     () => fallback);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
 private void RunToTimeoutShortCircuited()
 {
     ScenarioTestHelper.SleepHealthSnapshotInverval();
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout);
     }
     catch (HystrixException ex)
     {
         Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
     }
 }
Exemplo n.º 14
0
 private void RunToExceptionNormally()
 {
     ScenarioTestHelper.SleepHealthSnapshotInverval();
     try
     {
         HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
         Assert.Fail("Execution should throw exception.");
     }
     catch (ScenarioTestException)
     {
     }
 }
Exemplo n.º 15
0
 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);
     }
 }
Exemplo n.º 16
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);
     }
 }
Exemplo n.º 17
0
 public void RunWithEmptyKey()
 {
     foreach (string emptyString in EmptyStrings)
     {
         try
         {
             HystrixCommandBase.RunCommand <string>(emptyString, () => Expected);
             Assert.Fail("Argument exception should be thrown before.");
         }
         catch (ArgumentNullException)
         {
         }
     }
 }
Exemplo n.º 18
0
        private void TestErrorPercentageThresholdSetting(int?setting = null)
        {
            if (setting.HasValue)
            {
                CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage = setting.Value;
            }
            else
            {
                setting = CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage;
            }

            if (setting.Value == 100)
            {
                TestErrorPercentageThresholdSetting100();
                return;
            }

            int errorCount   = setting.Value;
            int successCount = 100 - errorCount;

            for (int i = 0; i < successCount; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null);
            }

            for (int i = 0; i < errorCount; 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, () => null);
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException ex)
            {
                Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
            }
        }
Exemplo n.º 19
0
        public void CircuitBreakerForceClosedIsFalseWithFallback()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = false;

            string fallback = null;
            string actual;

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

            ScenarioTestHelper.SleepHealthSnapshotInverval();
            actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }, () => fallback);
            Assert.AreEqual(fallback, actual);
        }
Exemplo n.º 20
0
        public void CircuitBreakerEnabledSettingIsFalse()
        {
            CommandComponents.ConfigSet.ToConcrete().CircuitBreakerEnabled = false;

            for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold * 3; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                Assert.AreEqual(false, CommandComponents.CircuitBreaker.IsOpen());
                Assert.AreEqual(true, CommandComponents.CircuitBreaker.AllowRequest());

                try
                {
                    HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                    Assert.Fail("Execution should throw exception.");
                }
                catch (ScenarioTestException)
                {
                }
            }
        }
 public void RunCommandFailCausedByBadRequestAndFallbackFail()
 {
     HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new BadRequestException(); },
                                            () => { throw new ScenarioTestException(); });
 }
 public void RunCommandFailCausedByBadRequestAndFallbackSuccess()
 {
     HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new BadRequestException(); }, () => string.Empty);
 }
Exemplo n.º 23
0
        private void TestSleepWindowSetting(int?setting = null, bool autoTestSuccess = true)
        {
            if (setting.HasValue)
            {
                CommandComponents.ConfigSet.ToConcrete().CircuitBreakerSleepWindowInMilliseconds = setting.Value;
            }
            else
            {
                setting = CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds;
            }

            int errorCount   = CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage;
            int successCount = 100 - errorCount;

            for (int i = 0; i < successCount; i++)
            {
                ScenarioTestHelper.SleepHealthSnapshotInverval();
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null);
            }

            for (int i = 0; i < errorCount; 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, () => null);
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException ex)
            {
                Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
            }

            Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1);

            if (autoTestSuccess)
            {
                string result = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null);
                Assert.AreEqual(null, result);
                return;
            }

            try
            {
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                Assert.Fail("Execution should throw exception.");
            }
            catch (ScenarioTestException)
            {
            }

            try
            {
                HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); });
                Assert.Fail("Execution should throw exception.");
            }
            catch (HystrixException ex)
            {
                Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType);
            }
        }