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 CreateInstance_Success()
        {
            Object instance = SL.CreateInstance(TestCommandKey);

            Assert.IsInstanceOfType(instance, typeof(SL));
            Assert.AreEqual(1, HystrixCommandBase.CommandComponentsCollection.Count);
            Assert.AreEqual(1, HystrixCommandBase.ExecutionSemaphores.Count);
            Assert.AreEqual(TestCommandKey, CommandComponents.CommandInfo.Key, true);
            Assert.AreEqual(HystrixCommandBase.DefaultGroupKey, CommandComponents.CommandInfo.GroupKey, true);
            Assert.AreEqual(CommandDomains.Default, CommandComponents.CommandInfo.Domain, true);
        }
 public void CreateInstance_Fail()
 {
     foreach (string key in EmptyStrings)
     {
         try
         {
             SL.CreateInstance(key);
         }
         catch (ArgumentNullException)
         {
         }
     }
 }
        public void Execution_SimulateReal_WithInstance()
        {
            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++)
                    {
                        object instance = SL.CreateInstance(TestCommandKey);
                        SL.StartExecution(instance);
                        Assert.AreNotEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount);
                        Thread.Sleep(1000);
                        int v = (int)j;
                        if (v % 3 == 0)
                        {
                            SL.MarkFailure(instance);
                        }
                        else if (v % 5 == 0)
                        {
                            SL.MarkBadRequest(instance);
                        }
                        else
                        {
                            SL.MarkSuccess(instance);
                        }
                        Assert.AreNotEqual(DefaultConfigSet.CommandMaxConcurrentCount, TestSemaphore.CurrentCount);
                        SL.EndExecution(instance);
                    }
                }, 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 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);
        }
        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);
            }
        }