public void IsBadRequestException_NonCustomBadRequestException()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker("custom", ex => ex is IndexOutOfRangeException);
            Exception ex2 = new Exception();

            Assert.IsFalse(ex2.IsBadRequestException());
        }
Example #2
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);
        }
Example #3
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]);
 }
Example #4
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 #5
0
        public void CustomBadRequestExecuteFallback()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);

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

            command.Run();
        }
Example #6
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 #7
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);
        }
Example #8
0
        public void RegisterCustomBadRequestExceptionChecker_NullNameParameterCausesArgumentNullException()
        {
            List <string> nullNames = new List <string>()
            {
                null, string.Empty, " ", "\t", "\n"
            };

            foreach (string name in nullNames)
            {
                try
                {
                    HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(name, IsBadRequestException);
                    Assert.Fail("ArgumentNullException should be thrown before this.");
                }
                catch (Exception ex)
                {
                    Assert.IsInstanceOfType(ex, typeof(ArgumentNullException), "Exception should be ArgumentNullException");
                }
            }
        }
Example #9
0
        public void CustomBadRequestRecordBadRequestMetrics()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);

            for (int i = 1; i <= 3; i++)
            {
                SampleIsolationCommand command = new SampleIsolationCommand(TestCommandKey, execute: () => { throw new ArgumentOutOfRangeException(); });
                try
                {
                    command.Run();
                }
                catch (Exception ex)
                {
                    Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
                }

                ScenarioTestHelper.SleepHealthSnapshotInverval();
                Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();
                Assert.AreEqual(i, counts[CommandExecutionEventEnum.BadRequest]);
            }
        }
Example #10
0
        public void CustomBadRequestNotCauseCircuitBreakerOpen()
        {
            HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, IsBadRequestException);

            CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold = 2;
            for (int i = 0; i < 100; i++)
            {
                SampleIsolationCommand command = new SampleIsolationCommand(
                    TestCommandKey,
                    execute: () => { throw new ArgumentOutOfRangeException(); });
                try
                {
                    command.Run();
                }
                catch (Exception ex)
                {
                    Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException));
                }

                ScenarioTestHelper.SleepHealthSnapshotInverval();
                Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();
                Assert.AreEqual(0, counts[CommandExecutionEventEnum.ShortCircuited]);
            }
        }
Example #11
0
 public void RegisterCustomBadRequestExceptionChecker_NullCheckerParameterCausesArgumentNullException()
 {
     HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(NormalCheckerName, null);
 }