Search in sources :

Example 1 with HystrixCommandExecutionHook

use of com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook in project Hystrix by Netflix.

the class HystrixCommandTest method testOnRunStartHookThrowsThreadIsolated.

@Test
public void testOnRunStartHookThrowsThreadIsolated() {
    final AtomicBoolean exceptionEncountered = new AtomicBoolean(false);
    final AtomicBoolean onThreadStartInvoked = new AtomicBoolean(false);
    final AtomicBoolean onThreadCompleteInvoked = new AtomicBoolean(false);
    final AtomicBoolean executionAttempted = new AtomicBoolean(false);
    class FailureInjectionHook extends HystrixCommandExecutionHook {

        @Override
        public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
            throw new HystrixRuntimeException(HystrixRuntimeException.FailureType.COMMAND_EXCEPTION, commandInstance.getClass(), "Injected Failure", null, null);
        }

        @Override
        public <T> void onThreadStart(HystrixInvokable<T> commandInstance) {
            onThreadStartInvoked.set(true);
            super.onThreadStart(commandInstance);
        }

        @Override
        public <T> void onThreadComplete(HystrixInvokable<T> commandInstance) {
            onThreadCompleteInvoked.set(true);
            super.onThreadComplete(commandInstance);
        }
    }
    final FailureInjectionHook failureInjectionHook = new FailureInjectionHook();
    class FailureInjectedCommand extends TestHystrixCommand<Integer> {

        public FailureInjectedCommand(ExecutionIsolationStrategy isolationStrategy) {
            super(testPropsBuilder(new TestCircuitBreaker()).setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolationStrategy)), failureInjectionHook);
        }

        @Override
        protected Integer run() throws Exception {
            executionAttempted.set(true);
            return 3;
        }
    }
    TestHystrixCommand<Integer> threadCmd = new FailureInjectedCommand(ExecutionIsolationStrategy.THREAD);
    try {
        int result = threadCmd.execute();
        System.out.println("RESULT : " + result);
    } catch (Throwable ex) {
        ex.printStackTrace();
        exceptionEncountered.set(true);
    }
    assertTrue(exceptionEncountered.get());
    assertTrue(onThreadStartInvoked.get());
    assertTrue(onThreadCompleteInvoked.get());
    assertFalse(executionAttempted.get());
    assertEquals(0, threadCmd.metrics.getCurrentConcurrentExecutionCount());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixCommandExecutionHook(com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) ExecutionIsolationStrategy(com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 2 with HystrixCommandExecutionHook

use of com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook in project Hystrix by Netflix.

the class HystrixCommandTest method testOnRunStartHookThrowsSemaphoreIsolated.

@Test
public void testOnRunStartHookThrowsSemaphoreIsolated() {
    final AtomicBoolean exceptionEncountered = new AtomicBoolean(false);
    final AtomicBoolean onThreadStartInvoked = new AtomicBoolean(false);
    final AtomicBoolean onThreadCompleteInvoked = new AtomicBoolean(false);
    final AtomicBoolean executionAttempted = new AtomicBoolean(false);
    class FailureInjectionHook extends HystrixCommandExecutionHook {

        @Override
        public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
            throw new HystrixRuntimeException(HystrixRuntimeException.FailureType.COMMAND_EXCEPTION, commandInstance.getClass(), "Injected Failure", null, null);
        }

        @Override
        public <T> void onThreadStart(HystrixInvokable<T> commandInstance) {
            onThreadStartInvoked.set(true);
            super.onThreadStart(commandInstance);
        }

        @Override
        public <T> void onThreadComplete(HystrixInvokable<T> commandInstance) {
            onThreadCompleteInvoked.set(true);
            super.onThreadComplete(commandInstance);
        }
    }
    final FailureInjectionHook failureInjectionHook = new FailureInjectionHook();
    class FailureInjectedCommand extends TestHystrixCommand<Integer> {

        public FailureInjectedCommand(ExecutionIsolationStrategy isolationStrategy) {
            super(testPropsBuilder().setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolationStrategy)), failureInjectionHook);
        }

        @Override
        protected Integer run() throws Exception {
            executionAttempted.set(true);
            return 3;
        }
    }
    TestHystrixCommand<Integer> semaphoreCmd = new FailureInjectedCommand(ExecutionIsolationStrategy.SEMAPHORE);
    try {
        int result = semaphoreCmd.execute();
        System.out.println("RESULT : " + result);
    } catch (Throwable ex) {
        ex.printStackTrace();
        exceptionEncountered.set(true);
    }
    assertTrue(exceptionEncountered.get());
    assertFalse(onThreadStartInvoked.get());
    assertFalse(onThreadCompleteInvoked.get());
    assertFalse(executionAttempted.get());
    assertEquals(0, semaphoreCmd.metrics.getCurrentConcurrentExecutionCount());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HystrixCommandExecutionHook(com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) ExecutionIsolationStrategy(com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy) Test(org.junit.Test)

Aggregations

ExecutionIsolationStrategy (com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy)2 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)2 HystrixCommandExecutionHook (com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)1