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_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();
        }
Ejemplo n.º 3
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.º 4
0
        public void Command_RejectedThreadWithNoFallback()
        {
            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());
                }
            }));

            IFuture<bool> f = null;
            TestCommandRejection command = null;
            try
            {
                f = new TestCommandRejection(circuitBreaker, pool, TimeSpan.FromMilliseconds(500.0), TimeSpan.FromMilliseconds(600), TestCommandRejection.FallbackNotImplemented).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));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure));
                Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success));
                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");
                }
            }

            try
            {
                f.Get();
            }
            catch (Exception)
            {

                Assert.Fail("The first one should succeed.");
            }

            Assert.AreEqual(1, 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(50, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage);

            Assert.AreEqual(2, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }