示例#1
0
        public void Command_ExecutionMultipleTimes()
        {
            SuccessfulTestCommand command = new SuccessfulTestCommand();
            Assert.IsFalse(command.IsExecutionComplete);
            // first should succeed
            Assert.AreEqual(true, command.Execute());
            Assert.IsTrue(command.IsExecutionComplete);
            Assert.IsTrue(command.IsExecutedInThread);
            Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1);
            Assert.IsTrue(command.IsSuccessfulExecution);
            try
            {
                // second should fail
                command.Execute();
                Assert.Fail("we should not allow this ... it breaks the state of request logs");
            }
            catch (Exception)
            {

                // we want to get here
            }

            try
            {
                // queue should also fail
                command.Queue();
                Assert.Fail("we should not allow this ... it breaks the state of request logs");
            }
            catch (Exception)
            {

                // we want to get here
            }

            Hystrix.Reset();
        }
示例#2
0
        public void Command_ExecutionHookSuccessfulCommandWithMultipleGetsOnFuture()
        {
            TestHystrixCommand<bool> command = new SuccessfulTestCommand();
            try
            {
                IFuture<bool> f = command.Queue();
                f.Get();
                f.Get();
                f.Get();
                f.Get();
            }
            catch (Exception e)
            {
                throw new Exception("Unexpected exception.", e);
            }

            /*
             * Despite multiple calls to get() we should only have 1 call to the hooks.
             */

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

            // the fallback() method should not be run as we were successful
            Assert.AreEqual(0, command.Builder.ExecutionHook.StartFallback);
            // null since it didn't run
            Assert.IsNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it didn't run
            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 run() succeeded
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception since run() succeeded
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete);
            Hystrix.Reset();
        }
示例#3
0
        public void Command_ExecutionHookSuccessfulCommandViaFireAndForget()
        {
            TestHystrixCommand<bool> command = new SuccessfulTestCommand();
            try
            {
                // do not block on "get()" ... fire this asynchronously
                command.Queue();
            }
            catch (Exception e)
            {
                throw new Exception("Unexpected exception.", e);
            }

            // wait for command to execute without calling get on the future
            while (!command.IsExecutionComplete)
            {
                try
                {
                    Thread.Sleep(10);
                }
                catch (ThreadInterruptedException)
                {
                    throw new Exception("interrupted");
                }
            }

            /*
             * All the hooks should still work even though we didn't call get() on the future
             */

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

            // the fallback() method should not be run as we were successful
            Assert.AreEqual(0, command.Builder.ExecutionHook.StartFallback);
            // null since it didn't run
            Assert.IsNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it didn't run
            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 run() succeeded
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception since run() succeeded
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete);
            Hystrix.Reset();
        }
示例#4
0
        public void Command_ExecutionHookSuccessfulCommand()
        {
            /* test with Execute() */
            TestHystrixCommand<bool> command = new SuccessfulTestCommand();
            command.Execute();

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

            // the fallback() method should not be run as we were successful
            Assert.AreEqual(0, command.Builder.ExecutionHook.StartFallback);
            // null since it didn't run
            Assert.IsNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it didn't run
            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 run() succeeded
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception since run() succeeded
            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 SuccessfulTestCommand();
            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 expect a successful response from run()
            Assert.IsNotNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we do not expect an exception
            Assert.IsNull(command.Builder.ExecutionHook.RunFailureException);

            // the fallback() method should not be run as we were successful
            Assert.AreEqual(0, command.Builder.ExecutionHook.StartFallback);
            // null since it didn't run
            Assert.IsNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since it didn't run
            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 run() succeeded
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception since run() succeeded
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete);
            Hystrix.Reset();
        }
示例#5
0
        public void Command_QueueSuccess()
        {
            TestHystrixCommand<bool> command = new SuccessfulTestCommand();
            try
            {
                IFuture<bool> future = command.Queue();
                Assert.AreEqual(true, future.Get());
            }
            catch (Exception)
            {

                Assert.Fail("We received an exception.");
            }

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

            Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
            Assert.AreEqual(0, 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(0, command.Builder.Metrics.GetHealthCounts().ErrorPercentage);

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

            Hystrix.Reset();
        }
示例#6
0
        public void Command_BasicExecutionWorksWithoutRequestVariable()
        {
            try
            {
                /* force the RequestVariable to not be initialized */
                HystrixRequestContext.ContextForCurrentThread = null;

                TestHystrixCommand<bool> command = new SuccessfulTestCommand();
                Assert.AreEqual(true, command.Execute());

                TestHystrixCommand<bool> command2 = new SuccessfulTestCommand();
                Assert.AreEqual(true, command2.Queue().Get());

                // we should be able to execute without a RequestVariable if ...
                // 1) We don't have a cacheKey
                // 2) We don't ask for the RequestLog
                // 3) We don't do collapsing

            }
            catch (Exception e)
            {

                Assert.Fail("We received an exception => " + e.Message);
            }

            Hystrix.Reset();
        }