public void Command_CircuitBreakerOnExecutionTimeout() { TestHystrixCommand<bool> command = new TestCommandWithTimeout(TimeSpan.FromMilliseconds(50.0), TestCommandWithTimeout.FallbackSuccess); try { Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); command.Execute(); Assert.IsTrue(command.IsResponseFromFallback); Assert.IsFalse(command.IsCircuitBreakerOpen); Assert.IsFalse(command.IsResponseShortCircuited); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure)); Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited)); } catch (Exception) { Assert.Fail("We should have received a response from the fallback."); } Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1); Assert.IsTrue(command.IsResponseTimedOut); Assert.AreEqual(0, 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(1, 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(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache)); Assert.AreEqual(100, command.Builder.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }
public void Command_ExecutionTimeoutFallbackFailure() { TestHystrixCommand<bool> command = new TestCommandWithTimeout(TimeSpan.FromMilliseconds(50.0), TestCommandWithTimeout.FallbackFailure); try { command.Execute(); Assert.Fail("we shouldn't get here"); } catch (Exception e) { if (e is HystrixRuntimeException) { 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 TimeoutException); } else { Assert.Fail("the exception should be HystrixRuntimeException"); } } // the time should be 50+ since we timeout at 50ms Assert.IsTrue(command.ExecutionTimeInMilliseconds >= 50, "Execution Time is: " + command.ExecutionTimeInMilliseconds); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, 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(1, 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(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache)); Assert.AreEqual(100, command.Builder.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }
public void Command_QueuedExecutionTimeoutWithNoFallback() { TestHystrixCommand<bool> command = new TestCommandWithTimeout(TimeSpan.FromMilliseconds(50), TestCommandWithTimeout.FallbackNotImplemented); try { command.Queue().Get(); Assert.Fail("we shouldn't get here"); } catch (Exception e) { if (e is ExecutionException && e.InnerException is HystrixRuntimeException) { HystrixRuntimeException de = (HystrixRuntimeException)e.InnerException; Assert.IsNotNull(de.FallbackException); Assert.IsTrue(de.FallbackException is NotSupportedException); Assert.IsNotNull(de.CommandType); Assert.IsNotNull(de.InnerException); Assert.IsTrue(de.InnerException is TimeoutException); } else { Assert.Fail("the exception should be ExecutionException with cause as HystrixRuntimeException"); } } Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1); Assert.IsTrue(command.IsResponseTimedOut); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, 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(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache)); Assert.AreEqual(100, command.Builder.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }
public void Command_ExecutionHookTimeoutWithFallback() { TestHystrixCommand<bool> command = new TestCommandWithTimeout(TimeSpan.FromMilliseconds(50.0), TestCommandWithTimeout.FallbackSuccess); try { command.Queue().Get(); } catch (Exception e) { throw new Exception("not expecting", e); } // the run() method should run as we're not short-circuited or rejected Assert.AreEqual(1, command.Builder.ExecutionHook.StartRun); // we should not have a response because of timeout Assert.IsNull(command.Builder.ExecutionHook.RunSuccessResponse); // we should not have an exception because run() didn't fail, it timed out Assert.IsNull(command.Builder.ExecutionHook.RunFailureException); // the fallback() method should be run due to timeout 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(1, command.Builder.ExecutionHook.ThreadStart); // we need to wait for the thread to complete before the onThreadComplete hook will be called try { Thread.Sleep(400); } catch (ThreadInterruptedException) { // ignore } Assert.AreEqual(1, command.Builder.ExecutionHook.ThreadComplete); Hystrix.Reset(); }
public void Command_CountersOnExecutionTimeout() { TestHystrixCommand<bool> command = new TestCommandWithTimeout(TimeSpan.FromMilliseconds(50.0), TestCommandWithTimeout.FallbackSuccess); try { command.Execute(); /* wait long enough for the command to have finished */ Thread.Sleep(200); /* response should still be the same as 'testCircuitBreakerOnExecutionTimeout' */ Assert.IsTrue(command.IsResponseFromFallback); Assert.IsFalse(command.IsCircuitBreakerOpen); Assert.IsFalse(command.IsResponseShortCircuited); Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1); Assert.IsTrue(command.IsResponseTimedOut); Assert.IsFalse(command.IsSuccessfulExecution); /* failure and timeout count should be the same as 'testCircuitBreakerOnExecutionTimeout' */ Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure)); Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); /* we should NOT have a 'success' counter */ Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); } catch (Exception) { Assert.Fail("We should have received a response from the fallback."); } Assert.AreEqual(0, 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(1, 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(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache)); Assert.AreEqual(100, command.Builder.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }