Пример #1
0
        public void Command_QueueFailureWithFallback()
        {
            TestHystrixCommand<bool> command = new KnownFailureTestCommandWithFallback(new TestCircuitBreaker());
            try
            {
                IFuture<bool> future = command.Queue();
                Assert.AreEqual(false, future.Get());
            }
            catch (Exception)
            {

                Assert.Fail("We should have received a response from the fallback.");
            }

            Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1);
            Assert.IsTrue(command.IsFailedExecution);

            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
            Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure));
            Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache));

            Assert.AreEqual(100, command.Builder.Metrics.GetHealthCounts().ErrorPercentage);

            Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());

            Hystrix.Reset();
        }
Пример #2
0
        public void Command_CircuitBreakerTripsAfterFailuresViaQueue()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            try
            {
                /* fail 3 times and then it should trip the circuit and stop executing */
                // failure 1
                KnownFailureTestCommandWithFallback attempt1 = new KnownFailureTestCommandWithFallback(circuitBreaker);
                attempt1.Queue().Get();
                Assert.IsTrue(attempt1.IsResponseFromFallback);
                Assert.IsFalse(attempt1.IsCircuitBreakerOpen);
                Assert.IsFalse(attempt1.IsResponseShortCircuited);

                // failure 2
                KnownFailureTestCommandWithFallback attempt2 = new KnownFailureTestCommandWithFallback(circuitBreaker);
                attempt2.Queue().Get();
                Assert.IsTrue(attempt2.IsResponseFromFallback);
                Assert.IsFalse(attempt2.IsCircuitBreakerOpen);
                Assert.IsFalse(attempt2.IsResponseShortCircuited);

                // failure 3
                KnownFailureTestCommandWithFallback attempt3 = new KnownFailureTestCommandWithFallback(circuitBreaker);
                attempt3.Queue().Get();
                Assert.IsTrue(attempt3.IsResponseFromFallback);
                Assert.IsFalse(attempt3.IsResponseShortCircuited);
                // it should now be 'open' and prevent further executions
                Assert.IsTrue(attempt3.IsCircuitBreakerOpen);

                // attempt 4
                KnownFailureTestCommandWithFallback attempt4 = new KnownFailureTestCommandWithFallback(circuitBreaker);
                attempt4.Queue().Get();
                Assert.IsTrue(attempt4.IsResponseFromFallback);
                // this should now be true as the response will be short-circuited
                Assert.IsTrue(attempt4.IsResponseShortCircuited);
                // this should remain open
                Assert.IsTrue(attempt4.IsCircuitBreakerOpen);

                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
                Assert.AreEqual(3, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure));
                Assert.AreEqual(4, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
                Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache));

                Assert.AreEqual(100, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage);

                Assert.AreEqual(4, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            }
            catch (Exception)
            {

                Assert.Fail("We should have received fallbacks.");
            }

            Hystrix.Reset();
        }
Пример #3
0
        public void Command_ExecutionHookRunFailureWithFallback()
        {
            /* test with Execute() */
            TestHystrixCommand<bool> command = new KnownFailureTestCommandWithFallback(new TestCircuitBreaker());
            command.Execute();

            // the run() method should run as we're not short-circuited or rejected
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartRun);
            // we should not have a response from run since run() failed
            Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we should have an exception since run() failed
            Assert.IsNotNull(command.Builder.ExecutionHook.RunFailureException);

            // the fallback() method should be run since run() failed
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartFallback);
            // a response since fallback is implemented
            Assert.IsNotNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it's implemented and succeeds
            Assert.IsNull(command.Builder.ExecutionHook.FallbackFailureException);

            // the Execute() method was used
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute);
            // we should have a response from Execute() since we expect a fallback despite failure of run()
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception because we expect a fallback
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete);

            /* test with queue() */
            command = new KnownFailureTestCommandWithFallback(new TestCircuitBreaker());
            try
            {
                command.Queue().Get();
            }
            catch (Exception e)
            {
                throw new Exception("Unexpected exception.", e);
            }

            // the run() method should run as we're not short-circuited or rejected
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartRun);
            // we should not have a response from run since run() failed
            Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we should have an exception since run() failed
            Assert.IsNotNull(command.Builder.ExecutionHook.RunFailureException);

            // the fallback() method should be run since run() failed
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartFallback);
            // a response since fallback is implemented
            Assert.IsNotNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it's implemented and succeeds
            Assert.IsNull(command.Builder.ExecutionHook.FallbackFailureException);

            // the queue() method was used
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute);
            // we should have a response from queue() since we expect a fallback despite failure of run()
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception because we expect a fallback
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete);
            Hystrix.Reset();
        }