Пример #1
0
        public void Command_NoRequestCacheViaExecuteSemaphore1()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SuccessfulCacheableCommandViaSemaphore command1 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "A");
            SuccessfulCacheableCommandViaSemaphore command2 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "B");
            SuccessfulCacheableCommandViaSemaphore command3 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "A");

            Assert.IsFalse(command1.IsCommandRunningInThread);

            String f1 = command1.Execute();
            String f2 = command2.Execute();
            String f3 = command3.Execute();

            Assert.AreEqual("A", f1);
            Assert.AreEqual("B", f2);
            Assert.AreEqual("A", f3);

            Assert.IsTrue(command1.Executed);
            // both should execute as they are different
            Assert.IsTrue(command2.Executed);
            // this should also execute because caching is disabled
            Assert.IsTrue(command3.Executed);

            // the execution log for command1 should show a Success
            Assert.AreEqual(1, command1.ExecutionEvents.Count());
            Assert.IsTrue(command1.ExecutionEvents.Contains(HystrixEventType.Success));

            // the execution log for command2 should show a Success
            Assert.AreEqual(1, command2.ExecutionEvents.Count());
            Assert.IsTrue(command2.ExecutionEvents.Contains(HystrixEventType.Success));

            // the execution log for command3 should show a Success
            Assert.AreEqual(1, command3.ExecutionEvents.Count());
            Assert.IsTrue(command3.ExecutionEvents.Contains(HystrixEventType.Success));

            Assert.AreEqual(3, 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(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, 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(0, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage);

            Assert.AreEqual(3, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count());
            Hystrix.Reset();
        }
Пример #2
0
        public void Command_RequestCacheViaQueueSemaphore1()
        {
            TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
            SuccessfulCacheableCommandViaSemaphore command1 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, true, "A");
            SuccessfulCacheableCommandViaSemaphore command2 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, true, "B");
            SuccessfulCacheableCommandViaSemaphore command3 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, true, "A");

            Assert.IsFalse(command1.IsCommandRunningInThread);

            IFuture<String> f1 = command1.Queue();
            IFuture<String> f2 = command2.Queue();
            IFuture<String> f3 = command3.Queue();

            try
            {
                Assert.AreEqual("A", f1.Get());
                Assert.AreEqual("B", f2.Get());
                Assert.AreEqual("A", f3.Get());
            }
            catch (Exception e)
            {
                throw new Exception("Unexpected exception.", e);
            }

            Assert.IsTrue(command1.Executed);
            // both should execute as they are different
            Assert.IsTrue(command2.Executed);
            // but the 3rd should come from cache
            Assert.IsFalse(command3.Executed);

            // the execution log for command1 should show a Success
            Assert.AreEqual(1, command1.ExecutionEvents.Count());
            Assert.IsTrue(command1.ExecutionEvents.Contains(HystrixEventType.Success));

            // the execution log for command2 should show a Success
            Assert.AreEqual(1, command2.ExecutionEvents.Count());
            Assert.IsTrue(command2.ExecutionEvents.Contains(HystrixEventType.Success));

            // the execution log for command3 should show it comes from cache
            Assert.AreEqual(2, command3.ExecutionEvents.Count()); // it will include the Success + ResponseFromCache
            Assert.IsTrue(command3.ExecutionEvents.Contains(HystrixEventType.Success));
            Assert.IsTrue(command3.ExecutionEvents.Contains(HystrixEventType.ResponseFromCache));

            Assert.IsTrue(command3.IsResponseFromCache);
            Assert.IsTrue(command3.ExecutionTimeInMilliseconds == -1);

            Assert.AreEqual(2, 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(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected));
            Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout));
            Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache));

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

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