@Test public void testConcurrencyStreamProperlyFiltersOutSemaphoreRejections() throws InterruptedException { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-I"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-I"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-I");
@Test public void testShortCircuited() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-G"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-G"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-G");
@Test public void testAsyncExecutionTimeoutValueViaObserve() { HystrixObservableCommand.Setter properties = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(50));
@Test public void testThreadPoolRejected() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-I"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-I"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-I");
@Test public void testThreadPoolRejected() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-I"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-I"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingCounter-I");
@Test public void testSynchronousExecutionUsingThreadIsolationTimeoutValueViaObserve() { HystrixObservableCommand.Setter properties = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
@Test public void testFallbackRejection() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-L"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-L"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingCounter-L");
@Test public void testShortCircuited() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-G"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-G"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingCounter-G");
@Test public void testFallbackRejection() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-L"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-L"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-L");
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("OuterSemaphoreCommand")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE))) {
HystrixCommandGroupKey groupKeyX = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-X"); HystrixCommandGroupKey groupKeyY = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-Y"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-X"); HystrixCommandKey keyX = HystrixCommandKey.Factory.asKey("RollingConcurrency-X");
@Test public void testConcurrencyStreamProperlyFiltersOutThreadPoolRejections() throws InterruptedException { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-J"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-J"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-J");
@Test public void testConcurrencyStreamProperlyFiltersOutShortCircuits() throws InterruptedException { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-H"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-H"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-H");
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("OuterCommand"))) {
HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-F"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-F"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-F");
HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-Concurrency-E"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-Concurrency-E"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingConcurrency-E");
@Test public void testRequestFromCache() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-F"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-F"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-F"); stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(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 { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); //RESPONSE_FROM_CACHE should not show up at all in thread pool counters - just the success assertEquals(2, stream.getLatest().length); assertEquals(1, stream.getLatestCount(HystrixEventType.ThreadPool.EXECUTED)); assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.REJECTED)); }
@Override protected Boolean run() { assertEquals("OuterSemaphoreCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Outer Semaphore Thread : " + Thread.currentThread().getName()); //should be a single execution on the caller thread assertEquals(1, Hystrix.getCommandCount()); if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("BEFORE expected it to run inside a semaphore"); } HystrixCommand<Boolean> command2 = new HystrixCommand<Boolean>(Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestUtil")) .andCommandKey(HystrixCommandKey.Factory.asKey("InnerCommand"))) { @Override protected Boolean run() { assertEquals("InnerCommand", Hystrix.getCurrentThreadExecutingCommand().name()); System.out.println("Inner Thread : " + Thread.currentThread().getName()); //should be a single execution on the thread isolating the second command assertEquals(1, Hystrix.getCommandCount()); return Hystrix.getCurrentThreadExecutingCommand() != null; } }; if (Hystrix.getCurrentThreadExecutingCommand() == null) { throw new RuntimeException("AFTER expected it to run inside a semaphore"); } return command2.execute(); }
@Test public void testExecutionTimeoutValue() { HystrixCommand.Setter properties = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestKey")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(50)); HystrixCommand<String> command = new HystrixCommand<String>(properties) { @Override protected String run() throws Exception { Thread.sleep(3000); // should never reach here return "hello"; } @Override protected String getFallback() { if (isResponseTimedOut()) { return "timed-out"; } else { return "abc"; } } }; String value = command.execute(); assertTrue(command.isResponseTimedOut()); assertEquals("expected fallback value", "timed-out", value); }
@Test public void testRequestFromCache() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ThreadPool-F"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("ThreadPool-F"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("RollingCounter-F"); stream = RollingThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(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 { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); //RESPONSE_FROM_CACHE should not show up at all in thread pool counters - just the success assertEquals(2, stream.getLatest().length); assertEquals(1, stream.getLatestCount(HystrixEventType.ThreadPool.EXECUTED)); assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.REJECTED)); }