public void Command_CircuitBreakerAcrossMultipleCommandsButSameCircuitBreaker() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); /* fail 3 times and then it should trip the circuit and stop executing */ // failure 1 KnownFailureTestCommandWithFallback attempt1 = new KnownFailureTestCommandWithFallback(circuitBreaker); attempt1.Execute(); Assert.IsTrue(attempt1.IsResponseFromFallback); Assert.IsFalse(attempt1.IsCircuitBreakerOpen); Assert.IsFalse(attempt1.IsResponseShortCircuited); // failure 2 with a different command, same circuit breaker KnownFailureTestCommandWithoutFallback attempt2 = new KnownFailureTestCommandWithoutFallback(circuitBreaker); try { attempt2.Execute(); } catch (Exception) { // ignore ... this doesn't have a fallback so will throw an exception } Assert.IsTrue(attempt2.IsFailedExecution); Assert.IsFalse(attempt2.IsResponseFromFallback); // false because no fallback Assert.IsFalse(attempt2.IsCircuitBreakerOpen); Assert.IsFalse(attempt2.IsResponseShortCircuited); // failure 3 of the Hystrix, 2nd for this particular HystrixCommand KnownFailureTestCommandWithFallback attempt3 = new KnownFailureTestCommandWithFallback(circuitBreaker); attempt3.Execute(); Assert.IsTrue(attempt2.IsFailedExecution); Assert.IsTrue(attempt3.IsResponseFromFallback); Assert.IsFalse(attempt3.IsResponseShortCircuited); // it should now be 'open' and prevent further executions // after having 3 failures on the Hystrix that these 2 different HystrixCommand objects are for Assert.IsTrue(attempt3.IsCircuitBreakerOpen); // attempt 4 KnownFailureTestCommandWithFallback attempt4 = new KnownFailureTestCommandWithFallback(circuitBreaker); attempt4.Execute(); Assert.IsTrue(attempt4.IsResponseFromFallback); // this should now be true as the response will be short-circuited Assert.IsTrue(attempt4.IsResponseShortCircuited); // this should remain open Assert.IsTrue(attempt4.IsCircuitBreakerOpen); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown)); Assert.AreEqual(3, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure)); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection)); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure)); Assert.AreEqual(3, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess)); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected)); Assert.AreEqual(1, 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(100, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(4, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }
public void Command_QueueKnownFailureWithNoFallback() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); TestHystrixCommand<bool> command = new KnownFailureTestCommandWithoutFallback(circuitBreaker); try { IFuture<bool> future = command.Queue(); future.Get(); Assert.Fail("we shouldn't get here"); } catch (Exception e) { if (e.InnerException is HystrixRuntimeException) { HystrixRuntimeException de = (HystrixRuntimeException)e.InnerException; Assert.IsNotNull(de.FallbackException); Assert.IsNotNull(de.CommandType); } else { Assert.Fail("the cause should be HystrixRuntimeException"); } } Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1); Assert.IsTrue(command.IsFailedExecution); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown)); Assert.AreEqual(1, 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(100, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }
public void Command_ExecutionHookShortCircuitedWithFallbackViaExecute() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker().SetForceShortCircuit(true); KnownFailureTestCommandWithoutFallback command = new KnownFailureTestCommandWithoutFallback(circuitBreaker); try { // now execute one that will be short-circuited command.Execute(); Assert.Fail("we expect an error as there is no fallback"); } catch (Exception) { // expecting } Assert.IsTrue(command.IsResponseShortCircuited); // 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); // no response since we don't have a fallback Assert.IsNull(command.Builder.ExecutionHook.FallbackSuccessResponse); // not null since fallback fails and throws an exception Assert.IsNotNull(command.Builder.ExecutionHook.FallbackFailureException); // execution occurred Assert.AreEqual(1, command.Builder.ExecutionHook.StartExecute); // we should not have a response because fallback fails Assert.IsNull(command.Builder.ExecutionHook.EndExecuteSuccessResponse); // we won't have an exception because short-circuit doesn't have one Assert.IsNull(command.Builder.ExecutionHook.EndExecuteFailureException); // but we do expect to receive a onError call with FailureType.Shortcircuit Assert.AreEqual(FailureType.Shortcircuit, command.Builder.ExecutionHook.EndExecuteFailureType); // thread execution Assert.AreEqual(0, command.Builder.ExecutionHook.ThreadStart); Assert.AreEqual(0, command.Builder.ExecutionHook.ThreadComplete); Hystrix.Reset(); }
public void Command_ExecutionKnownFailureWithNoFallback() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); TestHystrixCommand<bool> command = new KnownFailureTestCommandWithoutFallback(circuitBreaker); try { command.Execute(); Assert.Fail("we shouldn't get here"); } catch (HystrixRuntimeException e) { Assert.IsNotNull(e.FallbackException); Assert.IsNotNull(e.CommandType); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited)); Assert.AreEqual(0, command.Builder.Metrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)); } catch (Exception) { Assert.Fail("We should always get an HystrixRuntimeException when an error occurs."); } Assert.IsTrue(command.ExecutionTimeInMilliseconds > -1); Assert.IsTrue(command.IsFailedExecution); Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Failure)); Assert.AreEqual(0, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.Success)); Assert.AreEqual(1, circuitBreaker.Metrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown)); 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(100, circuitBreaker.Metrics.GetHealthCounts().ErrorPercentage); Assert.AreEqual(1, HystrixRequestLog.GetCurrentRequest().ExecutedCommands.Count()); Hystrix.Reset(); }