Search in sources :

Example 16 with HystrixRuntimeException

use of com.netflix.hystrix.exception.HystrixRuntimeException in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testNoRequestCacheOnTimeoutThrowsException.

@Test
public void testNoRequestCacheOnTimeoutThrowsException() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    NoRequestCacheTimeoutWithoutFallback r1 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        System.out.println("r1 value: " + r1.observe().toBlocking().single());
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r1.isResponseTimedOut());
    // what we want
    }
    NoRequestCacheTimeoutWithoutFallback r2 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r2.observe().toBlocking().single();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r2.isResponseTimedOut());
    // what we want
    }
    NoRequestCacheTimeoutWithoutFallback r3 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    Future<Boolean> f3 = r3.observe().toBlocking().toFuture();
    try {
        f3.get();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (ExecutionException e) {
        e.printStackTrace();
        assertTrue(r3.isResponseTimedOut());
    // what we want
    }
    // timeout on command is set to 200ms
    Thread.sleep(500);
    NoRequestCacheTimeoutWithoutFallback r4 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r4.observe().toBlocking().single();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        e.printStackTrace();
        assertTrue(r4.isResponseTimedOut());
        assertFalse(r4.isResponseFromFallback());
    // what we want
    }
    assertCommandExecutionEvents(r1, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r2, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r3, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r4, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(4);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 17 with HystrixRuntimeException

use of com.netflix.hystrix.exception.HystrixRuntimeException in project Hystrix by Netflix.

the class HystrixCommandTest method testRequestCacheOnThreadRejectionThrowsException.

@Test
public void testRequestCacheOnThreadRejectionThrowsException() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    CountDownLatch completionLatch = new CountDownLatch(1);
    RequestCacheThreadRejectionWithoutFallback r1 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("r1: " + r1.execute());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        assertTrue(r1.isResponseRejected());
    // what we want
    }
    RequestCacheThreadRejectionWithoutFallback r2 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("r2: " + r2.execute());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        //                e.printStackTrace();
        assertTrue(r2.isResponseRejected());
    // what we want
    }
    RequestCacheThreadRejectionWithoutFallback r3 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("f3: " + r3.queue().get());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        //                e.printStackTrace();
        assertTrue(r3.isResponseRejected());
    // what we want
    }
    // let the command finish (only 1 should actually be blocked on this due to the response cache)
    completionLatch.countDown();
    // then another after the command has completed
    RequestCacheThreadRejectionWithoutFallback r4 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("r4: " + r4.execute());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        //                e.printStackTrace();
        assertTrue(r4.isResponseRejected());
        assertFalse(r4.isResponseFromFallback());
    // what we want
    }
    assertCommandExecutionEvents(r1, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r2, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(r3, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(r4, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(4);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 18 with HystrixRuntimeException

use of com.netflix.hystrix.exception.HystrixRuntimeException 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)

Example 19 with HystrixRuntimeException

use of com.netflix.hystrix.exception.HystrixRuntimeException in project Hystrix by Netflix.

the class HystrixCommandTest method testRejectedThreadWithFallbackFailure.

/**
     * Test when a command fails to get queued up in the threadpool where the command implemented getFallback but it fails.
     * <p>
     * We specifically want to protect against developers getting random thread exceptions and instead just correctly receives an HystrixRuntimeException.
     */
@Test
public void testRejectedThreadWithFallbackFailure() throws ExecutionException, InterruptedException {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(1);
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Rejection-A");
    //this should pass through the queue and sit in the pool
    TestCommandRejection command1 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE);
    //this should sit in the queue
    TestCommandRejection command2 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_SUCCESS);
    //this should observe full queue and get rejected
    TestCommandRejection command3 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_FAILURE);
    Future<Boolean> f1 = null;
    Future<Boolean> f2 = null;
    try {
        f1 = command1.queue();
        f2 = command2.queue();
        //should get thread-pool rejected
        assertEquals(false, command3.queue().get());
        fail("we shouldn't get here");
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof HystrixRuntimeException && e.getCause() instanceof RejectedExecutionException) {
            HystrixRuntimeException de = (HystrixRuntimeException) e;
            assertNotNull(de.getFallbackException());
            assertFalse(de.getFallbackException() instanceof UnsupportedOperationException);
            assertNotNull(de.getImplementingClass());
            assertNotNull(de.getCause());
            assertTrue(de.getCause() instanceof RejectedExecutionException);
        } else {
            fail("the exception should be HystrixRuntimeException with cause as RejectedExecutionException");
        }
    }
    //still in-flight, no events yet
    assertCommandExecutionEvents(command1);
    //still in-flight, no events yet
    assertCommandExecutionEvents(command2);
    assertCommandExecutionEvents(command3, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_FAILURE);
    int numInFlight = circuitBreaker.metrics.getCurrentConcurrentExecutionCount();
    //pool-filler still going
    assertTrue("Expected at most 1 in flight but got : " + numInFlight, numInFlight <= 1);
    //This is a case where we knowingly walk away from executing Hystrix threads. They should have an in-flight status ("Executed").  You should avoid this in a production environment
    HystrixRequestLog requestLog = HystrixRequestLog.getCurrentRequest();
    assertEquals(3, requestLog.getAllExecutedCommands().size());
    assertTrue(requestLog.getExecutedCommandsAsString().contains("Executed"));
    //block on the outstanding work, so we don't inadvertently affect any other tests
    long startTime = System.currentTimeMillis();
    f1.get();
    f2.get();
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    System.out.println("Time blocked : " + (System.currentTimeMillis() - startTime));
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 20 with HystrixRuntimeException

use of com.netflix.hystrix.exception.HystrixRuntimeException in project Hystrix by Netflix.

the class HystrixCommandTest method testCheckedExceptionViaObserve.

/**
     * Test a java.lang.Error being thrown
     *
     * @throws InterruptedException
     */
@Test
public void testCheckedExceptionViaObserve() throws InterruptedException {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    CommandWithCheckedException command = new CommandWithCheckedException(circuitBreaker);
    final AtomicReference<Throwable> t = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        command.observe().subscribe(new Observer<Boolean>() {

            @Override
            public void onCompleted() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
                t.set(e);
                latch.countDown();
            }

            @Override
            public void onNext(Boolean args) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        fail("we should not get anything thrown, it should be emitted via the Observer#onError method");
    }
    latch.await(1, TimeUnit.SECONDS);
    assertNotNull(t.get());
    t.get().printStackTrace();
    assertTrue(t.get() instanceof HystrixRuntimeException);
    assertEquals("simulated checked exception message", t.get().getCause().getMessage());
    assertEquals("simulated checked exception message", command.getFailedExecutionException().getMessage());
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isFailedExecution());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_MISSING);
    assertNotNull(command.getExecutionException());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicReference(java.util.concurrent.atomic.AtomicReference) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Test (org.junit.Test)16 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)15 ExecutionException (java.util.concurrent.ExecutionException)13 TimeoutException (java.util.concurrent.TimeoutException)12 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)11 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 CancellationException (java.util.concurrent.CancellationException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Action1 (rx.functions.Action1)3 ExecutionIsolationStrategy (com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy)2 HystrixTimeoutException (com.netflix.hystrix.exception.HystrixTimeoutException)2 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)2 HystrixCommandExecutionHook (com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook)2 Observable (rx.Observable)2 Action0 (rx.functions.Action0)2