Search in sources :

Example 36 with TestCircuitBreaker

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

the class HystrixCommandTest method testCancelledTasksInQueueGetRemoved.

@Test
public void testCancelledTasksInQueueGetRemoved() throws Exception {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Cancellation-A");
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(10, 1);
    TestCommandRejection command1 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED);
    TestCommandRejection command2 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_NOT_IMPLEMENTED);
    // this should go through the queue and into the thread pool
    Future<Boolean> poolFiller = command1.queue();
    // this command will stay in the queue until the thread pool is empty
    Observable<Boolean> cmdInQueue = command2.observe();
    Subscription s = cmdInQueue.subscribe();
    assertEquals(1, pool.queue.size());
    s.unsubscribe();
    assertEquals(0, pool.queue.size());
    // make sure we wait for the command to finish so the state is clean for next test
    poolFiller.get();
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.CANCELLED);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(rx.Subscription) Test(org.junit.Test)

Example 37 with TestCircuitBreaker

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

the class HystrixCommandTest method testCheckedExceptionViaExecute.

/**
 * Test a checked Exception being thrown
 */
@Test
public void testCheckedExceptionViaExecute() {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    CommandWithCheckedException command = new CommandWithCheckedException(circuitBreaker);
    try {
        command.execute();
        fail("we expect to receive a " + Exception.class.getSimpleName());
    } catch (Exception e) {
        assertEquals("simulated checked exception message", e.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) 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)

Example 38 with TestCircuitBreaker

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

the class HystrixCommandTest method testNoRequestCacheViaExecuteSemaphore1.

/**
 * Test Request scoped caching with a mixture of commands
 */
@Test
public void testNoRequestCacheViaExecuteSemaphore1() {
    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());
    String f1 = command1.execute();
    String f2 = command2.execute();
    String f3 = command3.execute();
    assertEquals("A", f1);
    assertEquals("B", f2);
    assertEquals("A", f3);
    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 39 with TestCircuitBreaker

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

the class HystrixCommandTest method testRequestCache2.

/**
 * Test Request scoped caching doesn't prevent different ones from executing
 */
@Test
public void testRequestCache2() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A");
    SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "B");
    assertTrue(command1.isCommandRunningInThread());
    Future<String> f1 = command1.queue();
    Future<String> f2 = command2.queue();
    assertEquals("A", f1.get());
    assertEquals("B", f2.get());
    assertTrue(command1.executed);
    // both should execute as they are different
    assertTrue(command2.executed);
    assertTrue(command2.getExecutionTimeInMilliseconds() > -1);
    assertFalse(command2.isResponseFromCache());
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS);
    assertNull(command1.getExecutionException());
    assertFalse(command2.isResponseFromCache());
    assertNull(command2.getExecutionException());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 40 with TestCircuitBreaker

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

the class HystrixCommandTest method testCircuitBreakerAcrossMultipleCommandsButSameCircuitBreaker.

/**
 * Test that the circuit-breaker is shared across HystrixCommand objects with the same CommandKey.
 * <p>
 * This will test HystrixCommand objects with a single circuit-breaker (as if each injected with same CommandKey)
 * <p>
 * Multiple HystrixCommand objects with the same dependency use the same circuit-breaker.
 */
@Test
public void testCircuitBreakerAcrossMultipleCommandsButSameCircuitBreaker() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("SharedCircuitBreaker");
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(key);
    /* fail 3 times and then it should trip the circuit and stop executing */
    // failure 1
    TestHystrixCommand<Integer> attempt1 = getSharedCircuitBreakerCommand(key, ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.FallbackResult.SUCCESS, circuitBreaker);
    System.out.println("COMMAND KEY (from cmd): " + attempt1.commandKey.name());
    attempt1.execute();
    Thread.sleep(100);
    assertTrue(attempt1.isResponseFromFallback());
    assertFalse(attempt1.isCircuitBreakerOpen());
    assertFalse(attempt1.isResponseShortCircuited());
    // failure 2 with a different command, same circuit breaker
    TestHystrixCommand<Integer> attempt2 = getSharedCircuitBreakerCommand(key, ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.FallbackResult.SUCCESS, circuitBreaker);
    attempt2.execute();
    Thread.sleep(100);
    assertTrue(attempt2.isFailedExecution());
    assertTrue(attempt2.isResponseFromFallback());
    assertFalse(attempt2.isCircuitBreakerOpen());
    assertFalse(attempt2.isResponseShortCircuited());
    // failure 3 of the Hystrix, 2nd for this particular HystrixCommand
    TestHystrixCommand<Integer> attempt3 = getSharedCircuitBreakerCommand(key, ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.FallbackResult.SUCCESS, circuitBreaker);
    attempt3.execute();
    Thread.sleep(100);
    assertTrue(attempt3.isFailedExecution());
    assertTrue(attempt3.isResponseFromFallback());
    assertFalse(attempt3.isResponseShortCircuited());
    // it should now be 'open' and prevent further executions
    // after having 3 failures on the Hystrix that these 2 different HystrixCommand objects are for
    assertTrue(attempt3.isCircuitBreakerOpen());
    // attempt 4
    TestHystrixCommand<Integer> attempt4 = getSharedCircuitBreakerCommand(key, ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.FallbackResult.SUCCESS, circuitBreaker);
    attempt4.execute();
    Thread.sleep(100);
    assertTrue(attempt4.isResponseFromFallback());
    // this should now be true as the response will be short-circuited
    assertTrue(attempt4.isResponseShortCircuited());
    // this should remain open
    assertTrue(attempt4.isCircuitBreakerOpen());
    assertSaneHystrixRequestLog(4);
    assertCommandExecutionEvents(attempt1, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(attempt2, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(attempt3, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(attempt4, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) 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