Beispiel #1
0
        public void TestFallbackMissing()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-K");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-K");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-K");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));

            CommandStreamTest.Command cmd = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_MISSING);

            cmd.Observe();
            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public void Before()
        {
            this.context = HystrixRequestContext.InitializeContext();

            HystrixCommandMetrics.Reset();
            HystrixThreadPoolMetrics.Reset();
            HystrixCollapserMetrics.Reset();

            // clear collapsers
            RequestCollapserFactory.Reset();

            // clear circuit breakers
            HystrixCircuitBreakerFactory.Reset();
            HystrixPlugins.Reset();
            HystrixOptionsFactory.Reset();

            // clear up all streams
            CumulativeCollapserEventCounterStream.Reset();
            CumulativeCommandEventCounterStream.Reset();
            CumulativeThreadPoolEventCounterStream.Reset();
            RollingCollapserBatchSizeDistributionStream.Reset();
            RollingCollapserEventCounterStream.Reset();
            RollingCommandEventCounterStream.Reset();
            RollingCommandLatencyDistributionStream.Reset();
            RollingCommandMaxConcurrencyStream.Reset();
            RollingCommandUserLatencyDistributionStream.Reset();
            RollingThreadPoolEventCounterStream.Reset();
            RollingThreadPoolMaxConcurrencyStream.Reset();
        }
Beispiel #3
0
 public RollingThreadPoolEventCounterStreamTest(ITestOutputHelper output)
     : base()
 {
     this.output = output;
     HystrixThreadPoolCompletionStream.Reset();
     RollingThreadPoolEventCounterStream.Reset();
 }
Beispiel #4
0
        public async Task TestMultipleEventsOverTimeGetStoredAndAgeOut()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-M");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-M");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-M");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 250);
            latchSubscription = stream.Observe().Take(20 + LatchedObserver.STABLE_TICK_COUNT).Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            var cmd1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 20);
            var cmd2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 10);

            await cmd1.Observe();

            await cmd2.Observe();

            Assert.True(latch.Wait(10000), "CountdownEvent was not set!");

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());

            // all commands should have aged out
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #5
0
        public void TestThreadPoolRejected()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-I");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-I");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-I");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));

            // 10 commands will saturate threadpools when called concurrently.
            // submit 2 more requests and they should be THREADPOOL_REJECTED
            // should see 10 SUCCESSes, 2 THREADPOOL_REJECTED and 2 FALLBACK_SUCCESSes
            List <CommandStreamTest.Command> saturators = new List <CommandStreamTest.Command>();

            for (int i = 0; i < 10; i++)
            {
                saturators.Add(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 200));
            }

            CommandStreamTest.Command rejected1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0);
            CommandStreamTest.Command rejected2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0);

            foreach (CommandStreamTest.Command saturator in saturators)
            {
                saturator.Observe();
            }

            try
            {
                Time.Wait(100);
            }
            catch (Exception ie)
            {
                Assert.False(true, ie.Message);
            }

            rejected1.Observe();
            rejected2.Observe();
            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.True(rejected1.IsResponseThreadPoolRejected);
            Assert.True(rejected2.IsResponseThreadPoolRejected);

            // none of these got executed on a thread-pool, so thread pool metrics should be 0
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(10, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(2, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #6
0
        public async Task TestRequestFromCache()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-F");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-F");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-F");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            var cmd1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0);
            var cmd2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
            var cmd3 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);

            await cmd1.Observe();

            await cmd2.Observe();

            await cmd3.Observe();

            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");

            // RESPONSE_FROM_CACHE should not show up at all in thread pool counters - just the success
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #7
0
        public void TestSemaphoreRejected()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-H");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-H");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-H");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(new LatchedObserver(output, latch));

            // 10 commands will saturate semaphore when called from different threads.
            // submit 2 more requests and they should be SEMAPHORE_REJECTED
            // should see 10 SUCCESSes, 2 SEMAPHORE_REJECTED and 2 FALLBACK_SUCCESSes
            List <Command> saturators = new List <Command>();

            for (int i = 0; i < 10; i++)
            {
                saturators.Add(Command.From(groupKey, key, HystrixEventType.SUCCESS, 500, ExecutionIsolationStrategy.SEMAPHORE));
            }

            Command rejected1 = Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);
            Command rejected2 = Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);

            foreach (Command saturator in saturators)
            {
                Task t = new Task(
                    () =>
                {
                    saturator.Observe();
                }, CancellationToken.None,
                    TaskCreationOptions.LongRunning);
                t.Start();
            }

            try
            {
                Time.Wait(100);
            }
            catch (Exception ie)
            {
                Assert.False(true, ie.Message);
            }

            rejected1.Observe();
            rejected2.Observe();

            Assert.True(latch.Wait(10000), "CountdownEvent was not set!");
            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.True(rejected1.IsResponseSemaphoreRejected);
            Assert.True(rejected2.IsResponseSemaphoreRejected);

            // none of these got executed on a thread-pool, so thread pool metrics should be 0
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        internal static void Reset()
        {
            RollingThreadPoolEventCounterStream.Reset();
            CumulativeThreadPoolEventCounterStream.Reset();
            RollingThreadPoolMaxConcurrencyStream.Reset();

            Metrics.Clear();
        }
 public override void Dispose()
 {
     latchSubscription?.Dispose();
     stream?.Unsubscribe();
     latchSubscription = null;
     stream            = null;
     base.Dispose();
 }
        private HystrixThreadPoolMetrics(IHystrixThreadPoolKey threadPoolKey, IHystrixTaskScheduler threadPool, IHystrixThreadPoolOptions properties) : base(null)
        {
            this.threadPoolKey = threadPoolKey;
            this.threadPool    = threadPool;
            this.properties    = properties;

            rollingCounterStream    = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, properties);
            cumulativeCounterStream = CumulativeThreadPoolEventCounterStream.GetInstance(threadPoolKey, properties);
            rollingThreadPoolMaxConcurrencyStream = RollingThreadPoolMaxConcurrencyStream.GetInstance(threadPoolKey, properties);
        }
Beispiel #11
0
        public void TestFallbackRejection()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-L");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-L");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-L");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));

            // fallback semaphore size is 5.  So let 5 commands saturate that semaphore, then
            // let 2 more commands go to fallback.  they should get rejected by the fallback-semaphore
            List <CommandStreamTest.Command> fallbackSaturators = new List <CommandStreamTest.Command>();

            for (int i = 0; i < 5; i++)
            {
                fallbackSaturators.Add(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_SUCCESS, 400));
            }

            CommandStreamTest.Command rejection1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_SUCCESS, 0);
            CommandStreamTest.Command rejection2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_SUCCESS, 0);

            foreach (CommandStreamTest.Command saturator in fallbackSaturators)
            {
                saturator.Observe();
            }

            try
            {
                Time.Wait(70);
            }
            catch (Exception ie)
            {
                Assert.False(true, ie.Message);
            }

            rejection1.Observe();
            rejection2.Observe();
            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            // all 7 commands executed on-thread, so should be executed according to thread-pool metrics
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(7, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #12
0
        public void TestShortCircuited()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-G");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-G");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-G");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));


            //3 failures in a row will trip circuit.  let bucket roll once then submit 2 requests.
            //should see 3 FAILUREs and 2 SHORT_CIRCUITs and each should see a FALLBACK_SUCCESS

            CommandStreamTest.Command failure1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20);
            CommandStreamTest.Command failure2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20);
            CommandStreamTest.Command failure3 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20);

            CommandStreamTest.Command shortCircuit1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS);
            CommandStreamTest.Command shortCircuit2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS);

            failure1.Observe();
            failure2.Observe();
            failure3.Observe();

            Time.Wait(150);

            shortCircuit1.Observe();
            shortCircuit2.Observe();

            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.True(shortCircuit1.IsResponseShortCircuited);
            Assert.True(shortCircuit2.IsResponseShortCircuited);

            //only the FAILUREs should show up in thread pool counters
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(3, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public void TestEmptyStreamProducesZeros()
        {
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-A");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED) + stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public async void TestThreadPoolRejected()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-I");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-I");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-I");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            // 10 commands will saturate threadpools when called concurrently.
            // submit 2 more requests and they should be THREADPOOL_REJECTED
            // should see 10 SUCCESSes, 2 THREADPOOL_REJECTED and 2 FALLBACK_SUCCESSes
            var saturators = new List <Command>();

            for (int i = 0; i < 10; i++)
            {
                saturators.Add(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 500));
            }

            var rejected1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0);
            var rejected2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0);

            var tasks = new List <Task>();

            foreach (var saturator in saturators)
            {
                tasks.Add(saturator.ExecuteAsync());
            }

            await Task.Delay(50);

            await rejected1.Observe();

            await rejected2.Observe();

            Task.WaitAll(tasks.ToArray());
            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");

            Assert.True(rejected1.IsResponseThreadPoolRejected, "Command1 IsResponseThreadPoolRejected");
            Assert.True(rejected2.IsResponseThreadPoolRejected, "Command2 IsResponseThreadPoolRejected");

            // none of these got executed on a thread-pool, so thread pool metrics should be 0
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(10, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(2, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public async void TestSemaphoreRejected()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-H");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-H");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-H");
            CountdownEvent          latch         = new CountdownEvent(1);
            var observer = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            // 10 commands will saturate semaphore when called from different threads.
            // submit 2 more requests and they should be SEMAPHORE_REJECTED
            // should see 10 SUCCESSes, 2 SEMAPHORE_REJECTED and 2 FALLBACK_SUCCESSes
            List <Command> saturators = new List <Command>();

            for (int i = 0; i < 10; i++)
            {
                saturators.Add(Command.From(groupKey, key, HystrixEventType.SUCCESS, 500, ExecutionIsolationStrategy.SEMAPHORE));
            }

            Command rejected1 = Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);
            Command rejected2 = Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE);

            List <Task> tasks = new List <Task>();

            foreach (Command saturator in saturators)
            {
                tasks.Add(Task.Run(() => saturator.Execute()));
            }

            await Task.Delay(50);

            await Task.Run(() => rejected1.Execute());

            await Task.Run(() => rejected2.Execute());

            Task.WaitAll(tasks.ToArray());
            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");

            Assert.True(rejected1.IsResponseSemaphoreRejected, "rejected1 not rejected");
            Assert.True(rejected2.IsResponseSemaphoreRejected, "rejected2 not rejected");

            // none of these got executed on a thread-pool, so thread pool metrics should be 0
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
 public static void ShutdownThreads()
 {
     CumulativeCommandEventCounterStream.Reset();
     CumulativeThreadPoolEventCounterStream.Reset();
     RollingCommandEventCounterStream.Reset();
     RollingThreadPoolEventCounterStream.Reset();
     RollingCollapserEventCounterStream.Reset();
     RollingCollapserEventCounterStream.Reset();
     HealthCountsStream.Reset();
     RollingCollapserBatchSizeDistributionStream.Reset();
     RollingCommandLatencyDistributionStream.Reset();
     RollingCommandUserLatencyDistributionStream.Reset();
     RollingCommandMaxConcurrencyStream.Reset();
     RollingThreadPoolMaxConcurrencyStream.Reset();
 }
        public async void TestFallbackRejection()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-L");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-L");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-L");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            // fallback semaphore size is 5.  So let 5 commands saturate that semaphore, then
            // let 2 more commands go to fallback.  they should get rejected by the fallback-semaphore
            var fallbackSaturators = new List <Command>();

            for (int i = 0; i < 5; i++)
            {
                fallbackSaturators.Add(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0, HystrixEventType.FALLBACK_SUCCESS, 500));
            }

            var rejection1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0, HystrixEventType.FALLBACK_SUCCESS, 0);
            var rejection2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0, HystrixEventType.FALLBACK_SUCCESS, 0);

            var tasks = new List <Task>();

            foreach (var saturator in fallbackSaturators)
            {
                tasks.Add(saturator.ExecuteAsync());
            }

            await Task.Delay(50);

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());

            await Assert.ThrowsAsync <HystrixRuntimeException>(async() => await rejection1.Observe());

            await Assert.ThrowsAsync <HystrixRuntimeException>(async() => await rejection2.Observe());

            Task.WaitAll(tasks.ToArray());
            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");

            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(7, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #18
0
        public void TestEmptyStreamProducesZeros()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-A");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-A");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-A");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(new LatchedObserver(output, latch));

            // no writes
            Assert.True(latch.Wait(10000), "CountdownEvent was not set!");
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.EXECUTED) + stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public async Task TestShortCircuited()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-G");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-G");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-G");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            // 3 failures in a row will trip circuit.  let bucket roll once then submit 2 requests.
            // should see 3 FAILUREs and 2 SHORT_CIRCUITs and each should see a FALLBACK_SUCCESS
            var failure1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0);
            var failure2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0);
            var failure3 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 0);

            var shortCircuit1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS);
            var shortCircuit2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS);

            await failure1.Observe();

            await failure2.Observe();

            await failure3.Observe();

            Assert.True(WaitForHealthCountToUpdate(key.Name, 500, output), "Health count stream update took to long");

            await shortCircuit1.Observe();

            await shortCircuit2.Observe();

            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");

            Assert.True(shortCircuit1.IsResponseShortCircuited);
            Assert.True(shortCircuit2.IsResponseShortCircuited);

            // only the FAILUREs should show up in thread pool counters
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(3, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public void ShouldReturnOneExecutedTask()
        {
            // given
            var stream = RollingThreadPoolEventCounterStream.GetInstance(tpKey, 10, 100);

            stream.StartCachingStreamValuesIfUnstarted();

            var cmd = new NoOpHystrixCommand(output);

            cmd.Execute();
            Time.Wait(125);

            ICollection <HystrixThreadPoolMetrics> instances = HystrixThreadPoolMetrics.GetInstances();

            // then
            Assert.Equal(1, instances.Count);
            HystrixThreadPoolMetrics metrics = instances.First();

            Assert.Equal(1, metrics.RollingCountThreadsExecuted);
        }
        public async void TestSingleFailure()
        {
            var groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-C");
            var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-C");
            var key           = HystrixCommandKeyDefault.AsKey("RollingCounter-C");
            var latch         = new CountdownEvent(1);
            var observer      = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            var cmd = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.FAILURE, 20);

            await cmd.Observe();

            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public async void TestSingleBadRequest()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-E");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-E");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-E");
            CountdownEvent          latch         = new CountdownEvent(1);
            var observer = new LatchedObserver(output, latch);

            stream            = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            latchSubscription = stream.Observe().Subscribe(observer);
            Assert.True(Time.WaitUntil(() => observer.StreamRunning, 2000), "Stream failed to start");

            Command cmd = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.BAD_REQUEST);

            await Assert.ThrowsAsync <HystrixBadRequestException>(async() => await cmd.Observe());

            Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 2000, output), "Latch took to long to update");
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
        public async Task ShouldReturnOneExecutedTask()
        {
            // given
            var stream = RollingThreadPoolEventCounterStream.GetInstance(TpKey, 10, 100);

            stream.StartCachingStreamValuesIfUnstarted();

            var cmd = new NoOpHystrixCommand(output);
            await cmd.ExecuteAsync();

            Time.Wait(250);

            var instances = HystrixThreadPoolMetrics.GetInstances();

            // then
            output.WriteLine($"Instance count: {instances.Count}");
            Assert.Equal(1, instances.Count);
            var metrics = instances.First();

            output.WriteLine($"RollingCountThreadsExecuted: {metrics.RollingCountThreadsExecuted}");
            Assert.Equal(1, metrics.RollingCountThreadsExecuted);
        }
Beispiel #24
0
        public void TestSingleSuccess()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-B");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-B");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-B");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(new LatchedObserver(output, latch));

            Command cmd = Command.From(groupKey, key, HystrixEventType.SUCCESS, 20);

            cmd.Observe();

            Assert.True(latch.Wait(10000), "CountdownEvent was not set!");
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }
Beispiel #25
0
        public void TestRequestFromCache()
        {
            IHystrixCommandGroupKey groupKey      = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-F");
            IHystrixThreadPoolKey   threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-F");
            IHystrixCommandKey      key           = HystrixCommandKeyDefault.AsKey("RollingCounter-F");

            stream = RollingThreadPoolEventCounterStream.GetInstance(threadPoolKey, 10, 500);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(5).Subscribe(GetSubscriber(output, latch));


            CommandStreamTest.Command cmd1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 20);
            CommandStreamTest.Command cmd2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);
            CommandStreamTest.Command cmd3 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.RESPONSE_FROM_CACHE);

            cmd1.Observe();
            cmd2.Observe();
            cmd3.Observe();

            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());

            //RESPONSE_FROM_CACHE should not show up at all in thread pool counters - just the success
            Assert.Equal(2, stream.Latest.Length);
            Assert.Equal(1, stream.GetLatestCount(ThreadPoolEventType.EXECUTED));
            Assert.Equal(0, stream.GetLatestCount(ThreadPoolEventType.REJECTED));
        }