Search in sources :

Example 41 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker in project Hystrix by Netflix.

the class HystrixCommandTest method testBadRequestExceptionViaExecuteInThread.

/**
 * Test that a BadRequestException can be thrown and not count towards errors and bypasses fallback.
 */
@Test
public void testBadRequestExceptionViaExecuteInThread() {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    BadRequestCommand command1 = null;
    try {
        command1 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command1.execute();
        fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
    } catch (HystrixBadRequestException e) {
        // success
        e.printStackTrace();
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) Test(org.junit.Test)

Example 42 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker in project Hystrix by Netflix.

the class HystrixCommandTest method testRequestCache1.

/**
 * Test Request scoped caching of commands so that a 2nd duplicate call doesn't execute but returns the previous Future
 */
@Test
public void testRequestCache1() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A");
    SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A");
    assertTrue(command1.isCommandRunningInThread());
    Future<String> f1 = command1.queue();
    Future<String> f2 = command2.queue();
    assertEquals("A", f1.get());
    assertEquals("A", f2.get());
    assertTrue(command1.executed);
    // the second one should not have executed as it should have received the cached value instead
    assertFalse(command2.executed);
    assertTrue(command1.getExecutionTimeInMilliseconds() > -1);
    assertFalse(command1.isResponseFromCache());
    assertTrue(command2.isResponseFromCache());
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 43 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker in project Hystrix by Netflix.

the class HystrixCommandTest method testNoRequestCacheViaQueueSemaphore1.

/**
 * Test Request scoped caching with a mixture of commands
 */
@Test
public void testNoRequestCacheViaQueueSemaphore1() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SuccessfulCacheableCommandViaSemaphore command1 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "A");
    SuccessfulCacheableCommandViaSemaphore command2 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "B");
    SuccessfulCacheableCommandViaSemaphore command3 = new SuccessfulCacheableCommandViaSemaphore(circuitBreaker, false, "A");
    assertFalse(command1.isCommandRunningInThread());
    Future<String> f1 = command1.queue();
    Future<String> f2 = command2.queue();
    Future<String> f3 = command3.queue();
    assertEquals("A", f1.get());
    assertEquals("B", f2.get());
    assertEquals("A", f3.get());
    assertTrue(command1.executed);
    // both should execute as they are different
    assertTrue(command2.executed);
    // this should also execute because caching is disabled
    assertTrue(command3.executed);
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.SUCCESS);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 44 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker in project Hystrix by Netflix.

the class HystrixCommandTest method testRequestCacheWithSlowExecution.

/**
 * Test Request scoped caching of commands so that a 2nd duplicate call doesn't execute but returns the previous Future
 */
@Test
public void testRequestCacheWithSlowExecution() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SlowCacheableCommand command1 = new SlowCacheableCommand(circuitBreaker, "A", 200);
    SlowCacheableCommand command2 = new SlowCacheableCommand(circuitBreaker, "A", 100);
    SlowCacheableCommand command3 = new SlowCacheableCommand(circuitBreaker, "A", 100);
    SlowCacheableCommand command4 = new SlowCacheableCommand(circuitBreaker, "A", 100);
    Future<String> f1 = command1.queue();
    Future<String> f2 = command2.queue();
    Future<String> f3 = command3.queue();
    Future<String> f4 = command4.queue();
    assertEquals("A", f2.get());
    assertEquals("A", f3.get());
    assertEquals("A", f4.get());
    assertEquals("A", f1.get());
    assertTrue(command1.executed);
    // the second one should not have executed as it should have received the cached value instead
    assertFalse(command2.executed);
    assertFalse(command3.executed);
    assertFalse(command4.executed);
    assertTrue(command1.getExecutionTimeInMilliseconds() > -1);
    assertFalse(command1.isResponseFromCache());
    assertTrue(command2.getExecutionTimeInMilliseconds() == -1);
    assertTrue(command2.isResponseFromCache());
    assertTrue(command3.isResponseFromCache());
    assertTrue(command3.getExecutionTimeInMilliseconds() == -1);
    assertTrue(command4.isResponseFromCache());
    assertTrue(command4.getExecutionTimeInMilliseconds() == -1);
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(command3, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(command4, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(4);
    System.out.println("HystrixRequestLog: " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 45 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker in project Hystrix by Netflix.

the class HystrixCommandTest method testRejectedThreadUsingQueueSize.

/**
 * Test that we can reject a thread using isQueueSpaceAvailable() instead of just when the pool rejects.
 * <p>
 * For example, we have queue size set to 100 but want to reject when we hit 10.
 * <p>
 * This allows us to use FastProperties to control our rejection point whereas we can't resize a queue after it's created.
 */
@Test
public void testRejectedThreadUsingQueueSize() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Rejection-B");
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(10, 1);
    // put 1 item in the queue
    // the thread pool won't pick it up because we're bypassing the pool and adding to the queue directly so this will keep the queue full
    pool.queue.add(new Runnable() {

        @Override
        public void run() {
            System.out.println("**** queue filler1 ****");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    TestCommandRejection command = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED);
    try {
        // this should fail as we already have 1 in the queue
        command.queue();
        fail("we shouldn't get here");
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(command.isResponseRejected());
        assertFalse(command.isResponseShortCircuited());
        assertFalse(command.isResponseTimedOut());
        assertNotNull(command.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");
        }
    }
    assertCommandExecutionEvents(command, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) 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) Test(org.junit.Test)

Aggregations

TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)82 Test (org.junit.Test)70 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)46 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 ExecutionException (java.util.concurrent.ExecutionException)40 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)37 IOException (java.io.IOException)33 TimeoutException (java.util.concurrent.TimeoutException)33 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)18 CancellationException (java.util.concurrent.CancellationException)17 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 OnSubscribe (rx.Observable.OnSubscribe)7 Action1 (rx.functions.Action1)7 TestSubscriber (rx.observers.TestSubscriber)7 TryableSemaphoreActual (com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual)6 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 TryableSemaphore (com.netflix.hystrix.AbstractCommand.TryableSemaphore)2