public void Command_NoRequestCache3() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand command1 = new SuccessfulCacheableCommand(circuitBreaker, false, "A"); SuccessfulCacheableCommand command2 = new SuccessfulCacheableCommand(circuitBreaker, false, "B"); SuccessfulCacheableCommand command3 = new SuccessfulCacheableCommand(circuitBreaker, false, "A"); Assert.IsTrue(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); // this should also execute since we disabled the cache 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(); }
public void Command_CacheKeyExecutionRequiresRequestVariable() { try { /* force the RequestVariable to not be initialized */ HystrixRequestContext.ContextForCurrentThread = null; TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand command = new SuccessfulCacheableCommand(circuitBreaker, true, "one"); Assert.AreEqual(true, command.Execute()); SuccessfulCacheableCommand command2 = new SuccessfulCacheableCommand(circuitBreaker, true, "two"); Assert.AreEqual(true, command2.Queue().Get()); Assert.Fail("We expect an exception because cacheKey requires RequestVariable."); } catch (Exception) { } Hystrix.Reset(); }
public void Command_RequestCache1() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand command1 = new SuccessfulCacheableCommand(circuitBreaker, true, "A"); SuccessfulCacheableCommand command2 = new SuccessfulCacheableCommand(circuitBreaker, true, "A"); Assert.IsTrue(command1.IsCommandRunningInThread); IFuture<String> f1 = command1.Queue(); IFuture<String> f2 = command2.Queue(); try { Assert.AreEqual("A", f1.Get()); Assert.AreEqual("A", f2.Get()); } catch (Exception e) { throw new Exception("Unexpected exception.", e); } Assert.IsTrue(command1.Executed); // the second one should not have executed as it should have received the cached value instead Assert.IsFalse(command2.Executed); // the execution log for command1 should show a Success Assert.AreEqual(1, command1.ExecutionEvents.Count()); Assert.IsTrue(command1.ExecutionEvents.Contains(HystrixEventType.Success)); Assert.IsTrue(command1.ExecutionTimeInMilliseconds > -1); Assert.IsFalse(command1.IsResponseFromCache); // the execution log for command2 should show it came from cache Assert.AreEqual(2, command2.ExecutionEvents.Count()); // it will include the Success + ResponseFromCache Assert.IsTrue(command2.ExecutionEvents.Contains(HystrixEventType.Success)); Assert.IsTrue(command2.ExecutionEvents.Contains(HystrixEventType.ResponseFromCache)); Assert.IsTrue(command2.ExecutionTimeInMilliseconds == -1); Assert.IsTrue(command2.IsResponseFromCache); Assert.AreEqual(1, 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(2, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }