private void RunToTimeoutNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout); Assert.AreEqual(Expected, 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) { } }
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) { } }
private void RunToSuccessNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = HystrixCommandBase.RunCommand <string>(TestCommandKey, () => Expected); Assert.AreEqual(Expected, actual); }
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); } }
public void CustomBadRequestNotCauseCircuitBreakerOpen_CheckerThrowException() { HystrixCommandBase.RegisterCustomBadRequestExceptionChecker(WithExceptionCheckerName, IsBadRequestExceptionWithException); CommandComponents.ConfigSet.CircuitBreakerRequestCountThreshold = 2; int circuitBreakerCount = 0; for (int i = 0; i < 100; i++) { SampleIsolationCommand command = new SampleIsolationCommand( TestCommandKey, execute: () => { throw new ArgumentOutOfRangeException(); }); try { command.Run(); } catch (ArgumentOutOfRangeException) { } catch (HystrixException) { } ScenarioTestHelper.SleepHealthSnapshotInverval(); Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution(); circuitBreakerCount += counts[CommandExecutionEventEnum.ShortCircuited]; } Assert.AreNotEqual(0, circuitBreakerCount); }
public void ConfigAllCommandSettingsFromConcreteClass() { Assert.IsFalse(ScenarioTestHelper.AreEqual(DefaultConfigSet, CustomConfigSet)); new SampleIsolationCommand(TestCommandKey, config: configSet => ScenarioTestHelper.SetCommandConfigFrom(configSet, CustomConfigSet)); Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet)); }
public void ConfigAllCommandSettings() { Assert.IsFalse(ScenarioTestHelper.AreEqual(DefaultConfigSet, CustomConfigSet)); HystrixCommandBase.ConfigCommand <string>(TestCommandKey, configSet => ScenarioTestHelper.SetCommandConfigFrom(configSet, CustomConfigSet)); Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet)); }
public void Execution_Success() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); for (int i = 0; i < DefaultConfigSet.CircuitBreakerRequestCountThreshold * 2; i++) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); using (SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain)) { instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); instance.MarkSuccess(); } Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } CommandComponents.ConfigSet.InitTestHealthSnapshotInterval(); for (int i = 0; i < DefaultConfigSet.CircuitBreakerRequestCountThreshold * 2; i++) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); using (SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain)) { ScenarioTestHelper.SleepHealthSnapshotInverval(); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); instance.MarkSuccess(); } Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } }
public void ConfigCommand_Key_Domain_Config() { HystrixCommandBase.ConfigCommand <string>(TestCommandKey, TestDomain, config => ScenarioTestHelper.SetCommandConfigFrom(config, CustomConfigSet)); Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet)); }
public void ConfigCommandWithDefaultConfigFromConcreteClass() { new SampleIsolationCommand(TestCommandKey, config: c => { }); 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 Config_Key_Group_Domain_Config_Default() { SL.Config(TestCommandKey, null, null, config => {}); 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 Config_Key_Group_Domain() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true); Assert.AreEqual(TestGroupKey, CommandComponents.CommandInfo.GroupKey, true); Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(DefaultConfigSet, CommandComponents.ConfigSet)); }
public void Config_Key_Group_Config() { SL.Config(TestCommandKey, TestGroupKey, configSet => ScenarioTestHelper.SetCommandConfigFrom(configSet, CustomConfigSet)); Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true); Assert.AreEqual(TestGroupKey, CommandComponents.CommandInfo.GroupKey, true); Assert.AreEqual(CommandDomains.Default, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet)); }
public void ConfigCommandWithDefaultConfig() { HystrixCommandBase.ConfigCommand <string>(TestCommandKey, config => {}); 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 SemaphoreIsolation_Key_GroupKey_Domain() { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain); Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true); Assert.AreEqual(TestGroupKey, CommandComponents.CommandInfo.GroupKey, true); Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(DefaultConfigSet, CommandComponents.ConfigSet)); }
public void SemaphoreIsolation_Key() { SL instance = new SL(TestCommandKey); 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)); }
private void RunToSuccessNormallyFromConcreteClass() { SampleIsolationCommand command = new SampleIsolationCommand(TestCommandKey, execute: () => Expected); ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = command.Run(); Assert.AreEqual(Expected, actual); }
public void OverrideConfigInSubclass_WithoutCustomConfig() { SampleIsolationCommandWithOverriddenConfig command = new SampleIsolationCommandWithOverriddenConfig(TestCommandKey); 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)); }
private void RunToTimeoutNormallyFromConcreteClass() { SampleIsolationCommand command = new SampleIsolationCommand(TestCommandKey, expectedResult: Expected, sleepTimeInMilliseconds: TimeoutInMilliseconds + 2); ScenarioTestHelper.SleepHealthSnapshotInverval(); string actual = command.Run(); Assert.AreEqual(Expected, actual); }
public void SemaphoreIsolation_Key_GroupKey_Domain_Config() { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain, configSet => ScenarioTestHelper.SetCommandConfigFrom(configSet, CustomConfigSet)); Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true); Assert.AreEqual(TestGroupKey, CommandComponents.CommandInfo.GroupKey, true); Assert.AreEqual(TestDomain, CommandComponents.CommandInfo.Domain, true); Assert.IsTrue(ScenarioTestHelper.AreEqual(CustomConfigSet, CommandComponents.ConfigSet)); }
public void OverrideConfigInSubclass_WithCustomConfig_ConcreteFirst3() { SampleIsolationCommandWithOverriddenConfig.InitConfigSet = CustomConfigSet; SampleIsolationCommandWithOverriddenConfig command = new SampleIsolationCommandWithOverriddenConfig(TestCommandKey, config: configSet => { configSet.CommandTimeoutInMilliseconds = 111; }); 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(CustomConfigSet, CommandComponents.ConfigSet)); }
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 RunToExceptionNormally() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, () => { throw new ScenarioTestException(); }); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } }
private void RunToTimeoutShortCircuited() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { HystrixCommandBase.RunCommand <string>(TestCommandKey, ExecuteTimeout); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
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 Execution_ShortCircuited_ByFailureAndTimeout() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); CommandComponents.ConfigSet.InitTestHealthSnapshotInterval(); CommandComponents.ConfigSet.CommandTimeoutInMilliseconds = TimeoutInMilliseconds; for (int i = 0; i < DefaultConfigSet.CircuitBreakerRequestCountThreshold; i++) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); using (SL instance = new SL(TestCommandKey)) { ScenarioTestHelper.SleepHealthSnapshotInverval(); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); if (i % 2 == 0) { if (i % 4 == 0) { instance.MarkFailure(); continue; } Thread.Sleep(TimeoutInMilliseconds + 2); } instance.MarkSuccess(); } Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } try { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); using (SL instance = new SL(TestCommandKey)) { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { instance.StartExecution(); Assert.Fail("Short circuited exception should be thrown before."); } finally { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } } } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } }
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); } }
private void RunToExceptionNormallyFromConcreteClass() { ScenarioTestHelper.SleepHealthSnapshotInverval(); try { SampleIsolationCommand command = new SampleIsolationCommand(TestCommandKey, execute: () => { throw new ScenarioTestException(); }); ScenarioTestHelper.SleepHealthSnapshotInverval(); command.Run(); Assert.Fail("Execution should throw exception."); } catch (ScenarioTestException) { } }