/*** * 3 Commands, * Command 1 gets started in Bucket A and not completed until Bucket B * Commands 2 and 3 both start and end in Bucket B, and there should be a max-concurrency of 3 */ @Test public void testOneCommandCarriesOverToNextBucket() throws InterruptedException { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Concurrency-C"); stream = RollingCommandMaxConcurrencyStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 160); Command cmd2 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10); Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 15); cmd1.observe(); Thread.sleep(100); //bucket roll cmd2.observe(); Thread.sleep(1); cmd3.observe(); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); assertEquals(3, stream.getLatestRollingMax()); }
@Test public void testMultipleEventsOverTimeGetStoredAndDoNotAgeOut() { HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Cumulative-ThreadPool-M"); HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("Cumulative-ThreadPool-M"); HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cumulative-Counter-M"); stream = CumulativeThreadPoolEventCounterStream.getInstance(threadPoolKey, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(30).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20); CommandStreamTest.Command cmd2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 10); cmd1.observe(); cmd2.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } //all commands should have aged out assertEquals(2, stream.getLatest().length); assertEquals(2, stream.getLatestCount(HystrixEventType.ThreadPool.EXECUTED)); assertEquals(0, stream.getLatestCount(HystrixEventType.ThreadPool.REJECTED)); } }
/** * Creates instance of {@link HystrixCommand.Setter}. * * @return the instance of {@link HystrixCommand.Setter} */ public HystrixCommand.Setter build() throws HystrixPropertyException { HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); if (StringUtils.isNotBlank(threadPoolKey)) { setter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadPoolKey)); } try { setter.andThreadPoolPropertiesDefaults(HystrixPropertiesManager.initializeThreadPoolProperties(threadPoolProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Thread Pool properties. " + getInfo(), e); } try { setter.andCommandPropertiesDefaults(HystrixPropertiesManager.initializeCommandProperties(commandProperties)); } catch (IllegalArgumentException e) { throw new HystrixPropertyException("Failed to set Command properties. " + getInfo(), e); } return setter; }
@Test public void testMultipleEventsOverTimeGetStoredAndAgeOut() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-M"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); //by doing a take(30), we ensure that all rolling counts go back to 0 final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(30).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20); CommandStreamTest.Command cmd2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 10); cmd1.observe(); cmd2.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); } }
@Test public void testCancelledTasksInQueueGetRemoved() throws Exception { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cancellation-A"); TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(10, 1); TestCommandRejection command1 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED); TestCommandRejection command2 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED); // this should go through the queue and into the thread pool Future<Boolean> poolFiller = command1.queue(); // this command will stay in the queue until the thread pool is empty Observable<Boolean> cmdInQueue = command2.observe(); Subscription s = cmdInQueue.subscribe(); assertEquals(1, pool.queue.size()); s.unsubscribe(); assertEquals(0, pool.queue.size()); //make sure we wait for the command to finish so the state is clean for next test poolFiller.get(); assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS); assertCommandExecutionEvents(command2, HystrixEventType.CANCELLED); assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount()); System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertSaneHystrixRequestLog(2); }
@Test public void testCollapsed() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("BatchCommand"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); for (int i = 0; i < 3; i++) { CommandStreamTest.Collapser.from(i).observe(); } try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.SUCCESS.ordinal()] = 1; expected[HystrixEventType.COLLAPSED.ordinal()] = 3; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Test public void testFallbackFailure() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-J"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_FAILURE); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.FAILURE.ordinal()] = 1; expected[HystrixEventType.FALLBACK_FAILURE.ordinal()] = 1; expected[HystrixEventType.EXCEPTION_THROWN.ordinal()] = 1; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Override public HystrixObservableCommand.Setter createObservableSetter(FilterInvoker invoker, SofaRequest request) { Method clientMethod = request.getMethod(); if (!OBSERVABLE_SETTER_CACHE.containsKey(clientMethod)) { synchronized (DefaultSetterFactory.class) { if (!OBSERVABLE_SETTER_CACHE.containsKey(clientMethod)) { String groupKey = invoker.getConfig().getInterfaceId(); String commandKey = request.getMethodName(); HystrixObservableCommand.Setter setter = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); OBSERVABLE_SETTER_CACHE.put(clientMethod, setter); } } } return OBSERVABLE_SETTER_CACHE.get(clientMethod); } }
@Test public void testCollapsed() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("BatchCommand"); stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); for (int i = 0; i < 3; i++) { CommandStreamTest.Collapser.from(i).observe(); } try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.SUCCESS.ordinal()] = 1; expected[HystrixEventType.COLLAPSED.ordinal()] = 3; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Override public HystrixObservableCommand.Setter createObservableSetter(FilterInvoker invoker, SofaRequest request) { Method clientMethod = request.getMethod(); if (!OBSERVABLE_SETTER_CACHE.containsKey(clientMethod)) { synchronized (DefaultSetterFactory.class) { if (!OBSERVABLE_SETTER_CACHE.containsKey(clientMethod)) { String groupKey = invoker.getConfig().getInterfaceId(); String commandKey = request.getMethodName(); HystrixObservableCommand.Setter setter = HystrixObservableCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)); OBSERVABLE_SETTER_CACHE.put(clientMethod, setter); } } } return OBSERVABLE_SETTER_CACHE.get(clientMethod); } }
@Test public void testFallbackFailure() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-J"); stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); Command cmd = Command.from(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_FAILURE); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.FAILURE.ordinal()] = 1; expected[HystrixEventType.FALLBACK_FAILURE.ordinal()] = 1; expected[HystrixEventType.EXCEPTION_THROWN.ordinal()] = 1; assertArrayEquals(expected, stream.getLatest()); }
@Test public void testFallbackMissing() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-K"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20, HystrixEventType.FALLBACK_MISSING); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.FAILURE.ordinal()] = 1; expected[HystrixEventType.FALLBACK_MISSING.ordinal()] = 1; expected[HystrixEventType.EXCEPTION_THROWN.ordinal()] = 1; assertArrayEquals(expected, stream.getLatest()); }
@Test public void testSingleFailure() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-C"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 20); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.FAILURE.ordinal()] = 1; expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 1; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Test public void testSingleTimeout() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-D"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.TIMEOUT); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.TIMEOUT.ordinal()] = 1; expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 1; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Test public void testRequestFromCache() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Health-F"); stream = HealthCountsStream.getInstance(key, 10, 100); 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()); assertEquals(0L, stream.getLatest().getErrorCount()); assertEquals(1L, stream.getLatest().getTotalRequests()); //responses from cache should not show up here }
@Test public void testSingleBadRequest() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-E"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.BAD_REQUEST); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.BAD_REQUEST.ordinal()] = 1; expected[HystrixEventType.EXCEPTION_THROWN.ordinal()] = 1; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }
@Test public void testMetricsPublisherReset() { // precondition: HystrixMetricsPublisherFactory class is not loaded. Calling HystrixPlugins.reset() here should be good enough to run this with other tests. // set first custom publisher HystrixCommandKey key = HystrixCommandKey.Factory.asKey("key"); HystrixMetricsPublisherCommand firstCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher firstPublisher = new CustomPublisher(firstCommand); HystrixPlugins.getInstance().registerMetricsPublisher(firstPublisher); // ensure that first custom publisher is used HystrixMetricsPublisherCommand cmd = HystrixMetricsPublisherFactory.createOrRetrievePublisherForCommand(key, null, null, null, null); assertSame(firstCommand, cmd); // reset, then change to second custom publisher HystrixPlugins.reset(); HystrixMetricsPublisherCommand secondCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher secondPublisher = new CustomPublisher(secondCommand); HystrixPlugins.getInstance().registerMetricsPublisher(secondPublisher); // ensure that second custom publisher is used cmd = HystrixMetricsPublisherFactory.createOrRetrievePublisherForCommand(key, null, null, null, null); assertNotSame(firstCommand, cmd); assertSame(secondCommand, cmd); }
@Test public void testMultipleEventsOverTimeGetStoredAndAgeOut() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Health-M"); stream = HealthCountsStream.getInstance(key, 10, 100); //by doing a take(30), we ensure that all rolling counts go back to 0 final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(30).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd1 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20); CommandStreamTest.Command cmd2 = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.FAILURE, 10); cmd1.observe(); cmd2.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertEquals(0L, stream.getLatest().getErrorCount()); assertEquals(0L, stream.getLatest().getTotalRequests()); }
@Test public void testSingleBadRequest() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-E"); stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(5).subscribe(getSubscriber(latch)); Command cmd = Command.from(groupKey, key, HystrixEventType.BAD_REQUEST); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.BAD_REQUEST.ordinal()] = 1; expected[HystrixEventType.EXCEPTION_THROWN.ordinal()] = 1; assertArrayEquals(expected, stream.getLatest()); }
@Test public void testSingleSuccess() { HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-RollingCounter-B"); stream = RollingCommandEventCounterStream.getInstance(key, 10, 100); stream.startCachingStreamValuesIfUnstarted(); final CountDownLatch latch = new CountDownLatch(1); stream.observe().take(10).subscribe(getSubscriber(latch)); CommandStreamTest.Command cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20); cmd.observe(); try { assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); } catch (InterruptedException ex) { fail("Interrupted ex"); } assertEquals(HystrixEventType.values().length, stream.getLatest().length); long[] expected = new long[HystrixEventType.values().length]; expected[HystrixEventType.SUCCESS.ordinal()] = 1; System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertArrayEquals(expected, stream.getLatest()); }