Ejemplo n.º 1
0
        public void Command_ExecutionHookRejectedWithFallback()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SingleThreadedPool pool = new SingleThreadedPool(1);

            try
            {
                // fill the queue
                new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600.0), TestCommandRejection.FallbackSuccess).Queue();
                new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600.0), TestCommandRejection.FallbackSuccess).Queue();
            }
            catch (Exception)
            {
                // ignore
            }

            TestCommandRejection command = new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600.0), TestCommandRejection.FallbackSuccess);
            try
            {
                // now execute one that will be rejected
                command.Queue().Get();
            }
            catch (Exception e)
            {
                throw new Exception("not expecting", e);
            }

            Assert.IsTrue(command.IsResponseRejected);

            // the run() method should not run as we're rejected
            Assert.AreEqual(0, command.Builder.ExecutionHook.StartRun);
            // we should not have a response because of rejection
            Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse);
            // we should not have an exception because we didn't run
            Assert.IsNull(command.Builder.ExecutionHook.RunFailureException);

            // the fallback() method should be run due to rejection
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartFallback);
            // response since we have a fallback
            Assert.IsNotNull(command.Builder.ExecutionHook.FallbackSuccessResponse);
            // null since fallback succeeds
            Assert.IsNull(command.Builder.ExecutionHook.FallbackFailureException);

            // execution occurred
            Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute);
            // we should have a response because of fallback
            Assert.IsNotNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse);
            // we should not have an exception because of fallback
            Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException);

            // thread execution
            Assert.AreEqual(0, command.Builder.ExecutionHook.ThreadStart);
            Assert.AreEqual(0, command.Builder.ExecutionHook.ThreadComplete);
            Hystrix.Reset();
        }
Ejemplo n.º 2
0
        public void Command_TimedOutCommandDoesNotExecute()
        {
            SingleThreadedPool pool = new SingleThreadedPool(5);

            TestCircuitBreaker s1 = new TestCircuitBreaker();
            TestCircuitBreaker s2 = new TestCircuitBreaker();

            // execution will take 100ms, thread pool has a 600ms timeout
            CommandWithCustomThreadPool c1 = new CommandWithCustomThreadPool(s1, pool, TimeSpan.FromMilliseconds(100), UnitTestSetterFactory.GetCommandPropertiesSetter().WithExecutionIsolationThreadTimeoutInMilliseconds(600));
            // execution will take 200ms, thread pool has a 20ms timeout
            CommandWithCustomThreadPool c2 = new CommandWithCustomThreadPool(s2, pool, TimeSpan.FromMilliseconds(200), UnitTestSetterFactory.GetCommandPropertiesSetter().WithExecutionIsolationThreadTimeoutInMilliseconds(20));
            // queue up c1 first
            IFuture<bool> c1f = c1.Queue();
            // now queue up c2 and wait on it
            bool receivedException = false;
            try
            {
                c2.Queue().Get();
            }
            catch (Exception)
            {
                // we expect to get an exception here
                receivedException = true;
            }

            if (!receivedException)
            {
                Assert.Fail("We expect to receive an exception for c2 as it's supposed to timeout.");
            }

            // c1 will complete after 100ms
            try
            {
                c1f.Get();
            }
            catch (Exception)
            {
                Assert.Fail("we should not have failed while getting c1");
            }
            Assert.IsTrue(c1.DidExecute, "c1 is expected to executed but didn't");

            // c2 will timeout after 20 ms ... we'll wait longer than the 200ms time to make sure
            // the thread doesn't keep running in the background and execute
            try
            {
                Thread.Sleep(400);
            }
            catch (Exception)
            {
                throw new Exception("Failed to sleep");
            }
            Assert.IsFalse(c2.DidExecute, "c2 is not expected to execute, but did");

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

            Assert.AreEqual(0, s1.Metrics.GetHealthCounts().ErrorPercentage);

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

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

            Assert.AreEqual(2, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }
Ejemplo n.º 3
0
        public void Command_RejectedThreadWithFallbackFailure()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SingleThreadedPool pool = new SingleThreadedPool(1);
            // fill up the queue
            pool.Queue.Add(new Runnable(() =>
            {
                try
                {
                    TestContext.WriteLine("**** queue filler1 ****");
                    Thread.Sleep(500);
                }
                catch (ThreadInterruptedException e)
                {
                    TestContext.WriteLine(e.ToString());
                }
            }));

            try
            {
                new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackFailure).Queue();
                Assert.AreEqual(false, new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackFailure).Queue().Get());
                Assert.Fail("we shouldn't get here");
            }
            catch (Exception e)
            {

                Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
                if (e is HystrixRuntimeException && e.InnerException is RejectedExecutionException)
                {
                    HystrixRuntimeException de = (HystrixRuntimeException)e;
                    Assert.IsNotNull(de.FallbackException);
                    Assert.IsFalse(de.FallbackException is NotSupportedException);
                    Assert.IsNotNull(de.CommandType);
                    Assert.IsNotNull(de.InnerException);
                    Assert.IsTrue(de.InnerException is RejectedExecutionException);
                }
                else
                {
                    Assert.Fail("the exception should be HystrixRuntimeException with cause as RejectedExecutionException");
                }
            }

            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection));
            Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
            Assert.AreEqual(1, 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(2, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }
Ejemplo n.º 4
0
        public void Command_RejectedThreadUsingQueueSize()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SingleThreadedPool pool = new SingleThreadedPool(10, 1);
            // put 1 item in the queue
            // the thread pool won't pick it up because we're bypassing the pool and adding to the queue directly so this will keep the queue full
            pool.Queue.Add(new Runnable(() =>
            {
                try
                {
                    TestContext.WriteLine("**** queue filler1 ****");
                    Thread.Sleep(500);
                }
                catch (ThreadInterruptedException e)
                {
                    TestContext.WriteLine(e.ToString());
                }
            }));

            TestCommandRejection command = null;
            try
            {
                // this should fail as we already have 1 in the queue
                command = new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackNotImplemented);
                command.Queue();
                Assert.Fail("we shouldn't get here");
            }
            catch (Exception e)
            {


                // will be -1 because it never attempted execution
                Assert.IsTrue(command.ExecutionTimeInMilliseconds == -1);
                Assert.IsTrue(command.IsResponseRejected);
                Assert.IsFalse(command.IsResponseShortCircuited);
                Assert.IsFalse(command.IsResponseTimedOut);

                Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
                if (e is HystrixRuntimeException && e.InnerException is RejectedExecutionException)
                {
                    HystrixRuntimeException de = (HystrixRuntimeException)e;
                    Assert.IsNotNull(de.FallbackException);
                    Assert.IsTrue(de.FallbackException is NotSupportedException);
                    Assert.IsNotNull(de.CommandType);
                    Assert.IsNotNull(de.InnerException);
                    Assert.IsTrue(de.InnerException is RejectedExecutionException);
                }
                else
                {
                    Assert.Fail("the exception should be HystrixRuntimeException with cause as RejectedExecutionException");
                }
            }

            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
            Assert.AreEqual(1, 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(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }
Ejemplo n.º 5
0
        public void Command_RejectedThreadWithFallback()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SingleThreadedPool pool = new SingleThreadedPool(1);
            // fill up the queue
            pool.Queue.Add(new Runnable(() =>
            {
                try
                {
                    TestContext.WriteLine("**** queue filler1 ****");
                    Thread.Sleep(500);
                }
                catch (ThreadInterruptedException e)
                {
                    TestContext.WriteLine(e.ToString());
                }
            }));

            try
            {
                TestCommandRejection command1 = new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackSuccess);
                command1.Queue();
                TestCommandRejection command2 = new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackSuccess);
                Assert.AreEqual(false, command2.Queue().Get());
                Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
                Assert.IsFalse(command1.IsResponseRejected);
                Assert.IsFalse(command1.IsResponseFromFallback);
                Assert.IsTrue(command2.IsResponseRejected);
                Assert.IsTrue(command2.IsResponseFromFallback);
            }
            catch (Exception)
            {

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

            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure));
            Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
            Assert.AreEqual(1, 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(2, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }