@Override public Integer getValue() { return metrics.getCurrentConcurrentExecutionCount(); } });
public static HystrixCommandUtilization sample(HystrixCommandMetrics commandMetrics) { return new HystrixCommandUtilization(commandMetrics.getCurrentConcurrentExecutionCount()); }
@Test public void testExceptionConvertedToBadRequestExceptionInExecutionHookBypassesCircuitBreaker() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); ExceptionToBadRequestByExecutionHookCommand command = new ExceptionToBadRequestByExecutionHookCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD); try { command.execute(); fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName()); } catch (HystrixBadRequestException e) { // success e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName()); } assertCommandExecutionEvents(command, HystrixEventType.BAD_REQUEST); assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
@Test public void testSemaphoreExecutionWithTimeout() { TestHystrixCommand<Boolean> cmd = new InterruptibleCommand(new TestCircuitBreaker(), false); System.out.println("Starting command"); long timeMillis = System.currentTimeMillis(); try { cmd.execute(); fail("Should throw"); } catch (Throwable t) { assertNotNull(cmd.getExecutionException()); System.out.println("Unsuccessful Execution took : " + (System.currentTimeMillis() - timeMillis)); assertCommandExecutionEvents(cmd, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING); assertEquals(0, cmd.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); } }
@Test public void testNonBlockingCommandQueueFiresTimeout() throws Exception { //see https://github.com/Netflix/Hystrix/issues/514 final TestHystrixCommand<Integer> cmd = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.SUCCESS, 50); new Thread() { @Override public void run() { cmd.queue(); } }.start(); Thread.sleep(200); //timeout should occur in 50ms, and underlying thread should run for 500ms //therefore, after 200ms, the command should have finished with a fallback on timeout assertTrue(cmd.isExecutionComplete()); assertTrue(cmd.isResponseTimedOut()); assertEquals(0, cmd.metrics.getCurrentConcurrentExecutionCount()); }
/** * Test that the circuit-breaker being disabled doesn't wreak havoc. */ @Test public void testExecutionSuccessWithCircuitBreakerDisabled() { TestHystrixObservableCommand<Boolean> command = new TestCommandWithoutCircuitBreaker(); try { assertEquals(true, command.observe().toBlocking().single()); } catch (Exception e) { e.printStackTrace(); fail("We received an exception."); } assertCommandExecutionEvents(command, HystrixEventType.EMIT, HystrixEventType.SUCCESS); assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); assertNull(command.getExecutionException()); }
/** * Test a queued command execution timeout where the command implemented getFallback. * <p> * We specifically want to protect against developers queuing commands and using queue().get() without a timeout (such as queue().get(3000, TimeUnit.Milliseconds)) and ending up blocking * indefinitely by skipping the timeout protection of the execute() command. */ @Test public void testQueuedExecutionTimeoutWithFallback() throws Exception { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.SUCCESS, 50); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.queue().get()); assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
/** * Test that the circuit-breaker being disabled doesn't wreak havoc. */ @Test public void testExecutionSuccessWithCircuitBreakerDisabled() { TestHystrixCommand<Integer> command = getCircuitBreakerDisabledCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.EXECUTE_VALUE, command.execute()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); // we'll still get metrics ... just not the circuit breaker opening/closing assertCommandExecutionEvents(command, HystrixEventType.SUCCESS); }
/** * Test a queued command execution timeout where the command implemented getFallback. * <p> * We specifically want to protect against developers queuing commands and using queue().get() without a timeout (such as queue().get(3000, TimeUnit.Milliseconds)) and ending up blocking * indefinitely by skipping the timeout protection of the execute() command. */ @Test public void testObservedExecutionTimeoutWithFallback() throws Exception { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.SUCCESS, 50); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.observe().toBlocking().single()); assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
/** * Test a successful command execution (asynchronously). */ @Test public void testQueueSuccess() throws Exception { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS); Future<Integer> future = command.queue(); assertEquals(FlexibleTestHystrixCommand.EXECUTE_VALUE, future.get()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isSuccessfulExecution()); assertCommandExecutionEvents(command, HystrixEventType.SUCCESS); assertNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
private void testObserveFailureWithFallback(ExecutionIsolationStrategy isolationStrategy, boolean asyncException) { TestHystrixObservableCommand<Boolean> command = new KnownFailureTestCommandWithFallback(new TestCircuitBreaker(), isolationStrategy, asyncException); try { assertEquals(false, command.observe().toBlocking().single()); } catch (Exception e) { e.printStackTrace(); fail("We should have received a response from the fallback."); } assertEquals("we failed with a simulated issue", command.getFailedExecutionException().getMessage()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isFailedExecution()); assertTrue(command.isResponseFromFallback()); assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS); assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); assertNotNull(command.getExecutionException()); assertEquals(isolationStrategy.equals(ExecutionIsolationStrategy.THREAD), command.isExecutedInThread()); }
/** * Test a command execution that fails but has a fallback. */ @Test public void testExecutionFailureWithFallback() { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.FAILURE, AbstractTestHystrixCommand.FallbackResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.execute()); assertEquals("Execution Failure for TestHystrixCommand", command.getFailedExecutionException().getMessage()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isFailedExecution()); assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
/** * Test a command execution that throws exception that should not be wrapped but has a fallback. */ @Test public void testNotWrappedExceptionWithFallback() { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.NOT_WRAPPED_FAILURE, AbstractTestHystrixCommand.FallbackResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.execute()); assertEquals("Raw exception for TestHystrixCommand", command.getFailedExecutionException().getMessage()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isFailedExecution()); assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
private void testExecutionTimeoutWithSuccessfulFallback(ExecutionIsolationStrategy isolationStrategy) { TestHystrixObservableCommand<Integer> command = getCommand(isolationStrategy, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.SUCCESS, 100); try { assertEquals(FlexibleTestHystrixObservableCommand.FALLBACK_VALUE, command.observe().toBlocking().single()); // the time should be 50+ since we timeout at 50ms assertTrue("Execution Time is: " + command.getExecutionTimeInMilliseconds(), command.getExecutionTimeInMilliseconds() >= 50); assertTrue(command.isResponseTimedOut()); assertTrue(command.isResponseFromFallback()); assertNotNull(command.getExecutionException()); } catch (Exception e) { e.printStackTrace(); fail("We should have received a response from the fallback."); } assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS); assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); assertEquals(isolationStrategy.equals(ExecutionIsolationStrategy.THREAD), command.isExecutedInThread()); }
@Test public void testRecoverableErrorMaskedByFallbackButLogged() { TestHystrixCommand<Integer> command = getRecoverableErrorCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.FallbackResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.execute()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isFailedExecution()); assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
/** * Test a successful command execution. */ @Test public void testObserveSuccess() { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.EXECUTE_VALUE, command.observe().toBlocking().single()); assertEquals(null, command.getFailedExecutionException()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isSuccessfulExecution()); assertCommandExecutionEvents(command, HystrixEventType.SUCCESS); assertNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }
private void testObserveSuccess(ExecutionIsolationStrategy isolationStrategy) { try { TestHystrixObservableCommand<Boolean> command = new SuccessfulTestCommand(isolationStrategy); assertEquals(true, command.observe().toBlocking().single()); assertEquals(null, command.getFailedExecutionException()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isSuccessfulExecution()); assertFalse(command.isResponseFromFallback()); assertCommandExecutionEvents(command, HystrixEventType.EMIT, HystrixEventType.SUCCESS); assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); assertNull(command.getExecutionException()); assertEquals(isolationStrategy.equals(ExecutionIsolationStrategy.THREAD), command.isExecutedInThread()); } catch (Exception e) { e.printStackTrace(); fail("We received an exception."); } }
/** * Test a successful command execution. */ @Test public void testExecutionSuccess() { TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS); assertEquals(FlexibleTestHystrixCommand.EXECUTE_VALUE, command.execute()); assertEquals(null, command.getFailedExecutionException()); assertNull(command.getExecutionException()); assertTrue(command.getExecutionTimeInMilliseconds() > -1); assertTrue(command.isSuccessfulExecution()); assertCommandExecutionEvents(command, HystrixEventType.SUCCESS); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); assertSaneHystrixRequestLog(1); }
@Test public void testShortCircuitFallbackCounter() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker().setForceShortCircuit(true); KnownFailureTestCommandWithFallback command1 = new KnownFailureTestCommandWithFallback(circuitBreaker); command1.execute(); KnownFailureTestCommandWithFallback command2 = new KnownFailureTestCommandWithFallback(circuitBreaker); command2.execute(); // will be -1 because it never attempted execution assertTrue(command1.getExecutionTimeInMilliseconds() == -1); assertTrue(command1.isResponseShortCircuited()); assertFalse(command1.isResponseTimedOut()); assertNotNull(command1.getExecutionException()); assertCommandExecutionEvents(command1, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS); assertCommandExecutionEvents(command2, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS); assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(2); }
/** * Test a command execution timeout where the command implemented getFallback. */ @Test public void testExecutionTimeoutWithFallback() { TestHystrixCommand<Integer> command = getLatentCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.SUCCESS, 50); assertEquals(FlexibleTestHystrixCommand.FALLBACK_VALUE, command.execute()); // the time should be 50+ since we timeout at 50ms assertTrue("Execution Time is: " + command.getExecutionTimeInMilliseconds(), command.getExecutionTimeInMilliseconds() >= 50); assertFalse(command.isCircuitBreakerOpen()); assertFalse(command.isResponseShortCircuited()); assertTrue(command.isResponseTimedOut()); assertTrue(command.isResponseFromFallback()); assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS); assertNotNull(command.getExecutionException()); assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); }