Exemplo n.º 1
0
        public void Command_QueueUnknownFailureWithNoFallback()
        {
            TestHystrixCommand<bool> command = new UnknownFailureTestCommandWithoutFallback();
            try
            {
                command.Queue().Get();
                Assert.Fail("we shouldn't get here");
            }
            catch (Exception e)
            {

                if (e.InnerException is HystrixRuntimeException)
                {
                    HystrixRuntimeException de = (HystrixRuntimeException)e.InnerException;
                    Assert.IsNotNull(de.FallbackException);
                    Assert.IsNotNull(de.CommandType);
                }
                else
                {
                    Assert.Fail("the cause should be HystrixRuntimeException");
                }
            }

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

            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(1, 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(0, 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();
        }
Exemplo n.º 2
0
        public void Command_ExecutionHookRunFailureWithoutFallback()
        {
            /* test with Execute() */
            TestHystrixCommand<bool> command = new UnknownFailureTestCommandWithoutFallback();
            try
            {
                command.Execute();
                Assert.Fail("Expecting exception");
            }
            catch (Exception)
            {
                // ignore
            }

            // 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
            Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we should have an exception
            Assert.IsNotNull(command.Builder.ExecutionHook.RunFailureException);

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

            // the Execute() method was used
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute);
            // we should not have a response from Execute() since we do not have a fallback and run() failed
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should have an exception since run() failed
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteFailureException);
            // run() failure
            Assert.AreEqual(FailureType.CommandException, command.Builder.ExecutionHook.EndExecuteFailureType);

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

            /* test with queue() */
            command = new UnknownFailureTestCommandWithoutFallback();
            try
            {
                command.Queue().Get();
                Assert.Fail("Expecting exception");
            }
            catch (Exception)
            {
                // ignore
            }

            // 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
            Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we should have an exception
            Assert.IsNotNull(command.Builder.ExecutionHook.RunFailureException);

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

            // the queue() method was used
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute);
            // we should not have a response from queue() since we do not have a fallback and run() failed
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should have an exception since run() failed
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteFailureException);
            // run() failure
            Assert.AreEqual(FailureType.CommandException, command.Builder.ExecutionHook.EndExecuteFailureType);

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

            Hystrix.Reset();
        }