public void CircuitBreakerForceOpenIsFalseWithFallback() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceOpen = false; string result = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty, () => null); Assert.AreEqual(string.Empty, result); }
private void TestRequestCountThresholdSetting(int?setting = null) { if (setting.HasValue) { CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold = setting.Value; } else { setting = CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; } for (int i = 0; i < setting.Value; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (HystrixException) { } }
public void CircuitBreakerForceClosedIsFalseWithoutFallback() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = false; string expected = string.Empty; for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++) { try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
private void RunToTimeoutNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout); Assert.AreEqual(Expected, actual); }
private void RunToSuccessNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => Expected); Assert.AreEqual(Expected, actual); }
public void RunCommandFailAndFallbackSuccess() { string fallback = string.Empty; string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new Exception(); }, () => fallback); Assert.AreEqual(fallback, actual); }
private void TestErrorPercentageThresholdSetting100() { CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage = 100; for (int i = 0; i < 100; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { if (i >= CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold) { Assert.Fail("Execution should throw HystrixException."); } } catch (HystrixException ex) { if (i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold) { Assert.Fail("Execution should throw ScenarioTestException."); } Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } } }
public void CircuitBreakerForceClosedIsTrueWithoutFallback() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = true; for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++) { try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } }
public void RunCommandWithDefaultConfig() { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty); Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true); Assert.AreEqual(HystrixCommandBase.DefaultGroupKey, CommandComponents.CommandInfo.GroupKey, true); Assert.AreEqual(CommandDomains.Default, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(DefaultConfigSet, CommandComponents.ConfigSet)); }
public void RunCommandSuccessAndFallbackFail() { string expected = string.Empty; string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected, () => { throw new ScenarioTestException(); }); Assert.AreEqual(expected, actual); }
public void RunCommandSuccessAndFallbackSuccess() { string expected = string.Empty; string fallback = null; string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected, () => fallback); Assert.AreEqual(expected, actual); }
public void CircuitBreakerOpenCausedByTimeout() { int timeoutInMilliseconds = 10; CommandComponents.ConfigSet.CommandTimeoutInMilliseconds = timeoutInMilliseconds; string expected = string.Empty; string actual; Func <string> execute = () => { Thread.Sleep(timeoutInMilliseconds + 2); return(expected); }; for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, execute); Assert.AreEqual(expected, actual); } for (int i = 0; i < 10; i++) { try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, execute); var snapshot = CommandComponents.Metrics.GetExecutionEventDistribution(); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } } Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1); actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, execute); Assert.AreEqual(expected, actual); try { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, execute); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1); actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => expected); Assert.AreEqual(expected, actual); }
private void RunToTimeoutShortCircuited() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
private void RunToExceptionNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } }
private void RunToExceptionShortCircuited() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
public void CircuitBreakerForceOpenIsTrueWithoutFallback() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceOpen = true; try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => string.Empty); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
public void RunWithEmptyKey() { foreach (string emptyString in EmptyStrings) { try { HystrixCommandBase.RunCommand <string>(emptyString, () => Expected); Assert.Fail("Argument exception should be thrown before."); } catch (ArgumentNullException) { } } }
private void TestErrorPercentageThresholdSetting(int?setting = null) { if (setting.HasValue) { CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage = setting.Value; } else { setting = CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage; } if (setting.Value == 100) { TestErrorPercentageThresholdSetting100(); return; } int errorCount = setting.Value; int successCount = 100 - errorCount; for (int i = 0; i < successCount; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null); } for (int i = 0; i < errorCount; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
public void CircuitBreakerForceClosedIsFalseWithFallback() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerForceClosed = false; string fallback = null; string actual; for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }, () => fallback); Assert.AreEqual(fallback, actual); } ScenarioTestHelper.SleepHealthSnapshotInverval(); actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }, () => fallback); Assert.AreEqual(fallback, actual); }
public void CircuitBreakerEnabledSettingIsFalse() { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerEnabled = false; for (int i = 0; i < CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold * 3; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); Assert.AreEqual(false, CommandComponents.CircuitBreaker.IsOpen()); Assert.AreEqual(true, CommandComponents.CircuitBreaker.AllowRequest()); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } }
public void RunCommandFailCausedByBadRequestAndFallbackFail() { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new BadRequestException(); }, () => { throw new ScenarioTestException(); }); }
public void RunCommandFailCausedByBadRequestAndFallbackSuccess() { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new BadRequestException(); }, () => string.Empty); }
private void TestSleepWindowSetting(int?setting = null, bool autoTestSuccess = true) { if (setting.HasValue) { CommandComponents.ConfigSet.ToConcrete().CircuitBreakerSleepWindowInMilliseconds = setting.Value; } else { setting = CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds; } int errorCount = CommandComponents.ConfigSet.CircuitBreakerErrorThresholdPercentage; int successCount = 100 - errorCount; for (int i = 0; i < successCount; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null); } for (int i = 0; i < errorCount; i++) { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } } ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } Thread.Sleep(CommandComponents.ConfigSet.CircuitBreakerSleepWindowInMilliseconds + 1); if (autoTestSuccess) { string result = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => null); Assert.AreEqual(null, result); return; } try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }