Search in sources :

Example 21 with HystrixRuntimeException

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

the class HystrixCommandTest method testRejectedThreadWithNoFallback.

/**
     * Test when a command fails to get queued up in the threadpool where the command didn't implement getFallback.
     * <p>
     * We specifically want to protect against developers getting random thread exceptions and instead just correctly receiving HystrixRuntimeException when no fallback exists.
     */
@Test
public void testRejectedThreadWithNoFallback() throws Exception {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Rejection-NoFallback");
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(1);
    // fill up the queue
    pool.queue.add(new Runnable() {

        @Override
        public void run() {
            System.out.println("**** queue filler1 ****");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    Future<Boolean> f = null;
    TestCommandRejection command1 = null;
    TestCommandRejection command2 = null;
    try {
        command1 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED);
        f = command1.queue();
        command2 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED);
        command2.queue();
        fail("we shouldn't get here");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("command.getExecutionTimeInMilliseconds(): " + command2.getExecutionTimeInMilliseconds());
        // will be -1 because it never attempted execution
        assertTrue(command2.isResponseRejected());
        assertFalse(command2.isResponseShortCircuited());
        assertFalse(command2.isResponseTimedOut());
        assertNotNull(command2.getExecutionException());
        if (e instanceof HystrixRuntimeException && e.getCause() instanceof RejectedExecutionException) {
            HystrixRuntimeException de = (HystrixRuntimeException) e;
            assertNotNull(de.getFallbackException());
            assertTrue(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");
        }
    }
    f.get();
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
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) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 22 with HystrixRuntimeException

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

the class HystrixObservableCommandTest method testRequestCacheOnTimeoutThrowsException.

@Test
public void testRequestCacheOnTimeoutThrowsException() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    RequestCacheTimeoutWithoutFallback r1 = new RequestCacheTimeoutWithoutFallback(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());
        assertNotNull(r1.getExecutionException());
    // what we want
    }
    RequestCacheTimeoutWithoutFallback r2 = new RequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r2.observe().toBlocking().single();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r2.isResponseTimedOut());
        assertNotNull(r2.getExecutionException());
    // what we want
    }
    RequestCacheTimeoutWithoutFallback r3 = new RequestCacheTimeoutWithoutFallback(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());
        assertNotNull(r3.getExecutionException());
    // what we want
    }
    // timeout on command is set to 200ms
    Thread.sleep(500);
    RequestCacheTimeoutWithoutFallback r4 = new RequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r4.observe().toBlocking().single();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r4.isResponseTimedOut());
        assertFalse(r4.isResponseFromFallback());
        assertNotNull(r4.getExecutionException());
    }
    assertCommandExecutionEvents(r1, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r2, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(r3, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(r4, HystrixEventType.TIMEOUT, 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 23 with HystrixRuntimeException

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

the class HystrixObservableCommandTest 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.observe().toBlocking().single());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        e.printStackTrace();
        assertTrue(r1.isResponseRejected());
        assertNotNull(r1.getExecutionException());
    // what we want
    }
    RequestCacheThreadRejectionWithoutFallback r2 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("r2: " + r2.observe().toBlocking().single());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        //                e.printStackTrace();
        assertTrue(r2.isResponseRejected());
        assertNotNull(r2.getExecutionException());
    // what we want
    }
    RequestCacheThreadRejectionWithoutFallback r3 = new RequestCacheThreadRejectionWithoutFallback(circuitBreaker, completionLatch);
    try {
        System.out.println("f3: " + r3.observe().toBlocking().toFuture().get());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (ExecutionException e) {
        assertTrue(r3.isResponseRejected());
        assertTrue(e.getCause() instanceof HystrixRuntimeException);
        assertNotNull(r3.getExecutionException());
    }
    // 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.observe().toBlocking().single());
        // we should have thrown an exception
        fail("expected a rejection");
    } catch (HystrixRuntimeException e) {
        //                e.printStackTrace();
        assertTrue(r4.isResponseRejected());
        assertFalse(r4.isResponseFromFallback());
        assertNotNull(r4.getExecutionException());
    // 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) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 24 with HystrixRuntimeException

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

the class HystrixObservableCommandTest method testObserveKnownFailureWithNoFallback.

private void testObserveKnownFailureWithNoFallback(ExecutionIsolationStrategy isolationStrategy, boolean asyncException) {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    TestHystrixObservableCommand<Boolean> command = new KnownFailureTestCommandWithoutFallback(circuitBreaker, isolationStrategy, asyncException);
    try {
        command.observe().toBlocking().single();
        fail("we shouldn't get here");
    } catch (HystrixRuntimeException e) {
        e.printStackTrace();
        assertNotNull(e.getFallbackException());
        assertNotNull(e.getImplementingClass());
    } catch (Exception e) {
        e.printStackTrace();
        fail("We should always get an HystrixRuntimeException when an error occurs.");
    }
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isFailedExecution());
    assertFalse(command.isResponseFromFallback());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_MISSING);
    assertNotNull(command.getExecutionException());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    assertEquals(isolationStrategy.equals(ExecutionIsolationStrategy.THREAD), command.isExecutedInThread());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

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