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 MarkSuccess_RunTwice()
        {
            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.MarkSuccess();
                    ScenarioTestHelper.SleepHealthSnapshotInverval();
                    Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();
                    Assert.AreEqual(i, counts[CommandExecutionEventEnum.Success]);

                    instance.MarkSuccess();
                    ScenarioTestHelper.SleepHealthSnapshotInverval();
                    counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();
                    Assert.AreEqual(i, counts[CommandExecutionEventEnum.Success]);
                }
            }
        }
        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 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 MarkSuccess_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.MarkSuccess(instance);
            Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount - 1, TestSemaphore.CurrentCount);

            ScenarioTestHelper.SleepHealthSnapshotInverval();
            Dictionary <CommandExecutionEventEnum, int> counts = CommandComponents.Metrics.ToConcrete().GetExecutionEventDistribution();

            Assert.AreEqual(1, counts[CommandExecutionEventEnum.Success]);

            SL.EndExecution(instance);
            Assert.AreEqual(CommandComponents.ConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount);
        }
예제 #6
0
        public static void MarkSuccess(object instance)
        {
            SemaphoreIsolation isolationInstance = ConvertInstance(instance);

            isolationInstance.MarkSuccess();
        }
        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 MarkSuccess_WithEmptyInstance()
 {
     SL.MarkSuccess(null);
 }