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); }
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)); }
public async void TestFallbackMissing() { var groupKey = HystrixCommandGroupKeyDefault.AsKey("ThreadPool-K"); var threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("ThreadPool-K"); var key = HystrixCommandKeyDefault.AsKey("RollingCounter-K"); 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, 0, HystrixEventType.FALLBACK_MISSING); await Assert.ThrowsAsync <HystrixRuntimeException>(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 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)); }
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(GetSubscriber(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(CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 500, ExecutionIsolationStrategy.SEMAPHORE)); } CommandStreamTest.Command rejected1 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE); CommandStreamTest.Command rejected2 = CommandStreamTest.Command.From(groupKey, key, HystrixEventType.SUCCESS, 0, ExecutionIsolationStrategy.SEMAPHORE); foreach (CommandStreamTest.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(); try { Assert.True(latch.Wait(10000)); } catch (Exception) { Assert.False(true, "Interrupted ex"); } 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)); }