public void StartExecution_RestartFailedSilently() { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); }
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 EndExecution_ForNotStartedExecution() { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); instance.EndExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); instance.EndExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); instance.EndExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); instance.EndExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); using (instance = new SL(TestCommandKey)) { instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); } Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); instance.EndExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); }
public void StartExecution_WithInstance() { object instance = SL.CreateInstance(TestCommandKey); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); SL.StartExecution(instance); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); }
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); } }
public void MarkBadRequest_RunOnce() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); CommandComponents.ConfigSet.InitTestHealthSnapshotInterval(); for (int i = 1; i <= DefaultConfigSet.CircuitBreakerRequestCountThreshold; i++) { using (SL instance = new SL(TestCommandKey)) { instance.StartExecution(); instance.MarkBadRequest(); ScenarioTestHelper.SleepHealthSnapshotInverval(); Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution(); Assert.AreEqual(i, counts[CommandExecutionEventEnum.BadRequest]); } } }
public void Execution_SimulateReal() { CountdownEvent waitHandle = new CountdownEvent(DefaultConfigSet.CommandMaxConcurrentCount); for (int i = 0; i < DefaultConfigSet.CommandMaxConcurrentCount; i++) { Task.Factory.StartNew( j => { for (int k = 0; k < 10; k++) { using (SL instance = new SL(TestCommandKey)) { instance.StartExecution(); Assert.AreNotEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); Thread.Sleep(1000); int v = (int)j; if (v % 3 == 0) { instance.MarkFailure(); } else if (v % 5 == 0) { instance.MarkBadRequest(); } else { instance.MarkSuccess(); } Assert.AreNotEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } } }, i) .ContinueWith( t => { var ex = t.Exception; waitHandle.Signal(); }); } bool success = waitHandle.Wait(180 * 1000); Assert.IsTrue(success); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); }
public void StartExecution_Rejected() { for (int i = 1; i <= DefaultConfigSet.CommandMaxConcurrentCount; i++) { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain); instance.StartExecution(); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - i, TestSemaphore.CurrentCount); } try { SL instance = new SL(TestCommandKey, TestGroupKey, TestDomain); instance.StartExecution(); Assert.Fail("Execution should be rejected here."); } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.SemaphoreIsolationRejected, ex.FailureType); } }
public void MarkBadRequest_WithInstance() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); CommandComponents.ConfigSet.InitTestHealthSnapshotInterval(); object instance = SL.CreateInstance(TestCommandKey); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); SL.StartExecution(instance); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); SL.MarkBadRequest(instance); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); ScenarioTestHelper.SleepHealthSnapshotInverval(); Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution(); Assert.AreEqual(1, counts[CommandExecutionEventEnum.BadRequest]); SL.EndExecution(instance); Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); }
/// <summary> /// HystrixException will be thrown when execution is short-circuited or rejected. /// Once an instance is started, a semaphore is occupied. /// It's required to call EndExecution(instance) to release the semaphore after execution. /// </summary> /// <exception cref="HystrixException"></exception> public static void StartExecution(object instance) { SemaphoreIsolation isolationInstance = ConvertInstance(instance); isolationInstance.StartExecution(); }
public void Execution_ShortCircuited_AutoRecover_WithInstance() { SL.Config(TestCommandKey, TestGroupKey, TestDomain); CommandComponents.ConfigSet.InitTestHealthSnapshotInterval(); for (int i = 0; i < DefaultConfigSet.CircuitBreakerRequestCountThreshold; i++) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); object instance = SL.CreateInstance(TestCommandKey); ScenarioTestHelper.SleepHealthSnapshotInverval(); SL.StartExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); SL.MarkFailure(instance); SL.EndExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } for (int i = 0; i < 10; i++) { try { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); object instance = SL.CreateInstance(TestCommandKey); ScenarioTestHelper.SleepHealthSnapshotInverval(); try { SL.StartExecution(instance); Assert.Fail("Short circuited exception should be thrown before."); } catch (HystrixException) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); throw; } } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } } Thread.Sleep(DefaultConfigSet.CircuitBreakerSleepWindowInMilliseconds + 10); { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); object instance = SL.CreateInstance(TestCommandKey); ScenarioTestHelper.SleepHealthSnapshotInverval(); SL.StartExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); SL.MarkFailure(instance); SL.EndExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } for (int i = 0; i < 10; i++) { try { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); object instance = SL.CreateInstance(TestCommandKey); ScenarioTestHelper.SleepHealthSnapshotInverval(); try { SL.StartExecution(instance); Assert.Fail("Short circuited exception should be thrown before."); } catch (HystrixException) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); throw; } } catch (HystrixException ex) { Assert.AreEqual(FailureTypeEnum.ShortCircuited, ex.FailureType); } } Thread.Sleep(DefaultConfigSet.CircuitBreakerSleepWindowInMilliseconds + 10); for (int i = 0; i < 10; i++) { Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); object instance = SL.CreateInstance(TestCommandKey); SL.StartExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount); SL.MarkSuccess(instance); SL.EndExecution(instance); Assert.AreEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount); } }
public void StartExecution_WithEmptyInstance() { SL.StartExecution(null); }