Search in sources :

Example 51 with TestCircuitBreaker

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

the class HystrixCommandTest method testRequestCache3.

/**
     * Test Request scoped caching with a mixture of commands
     */
@Test
public void testRequestCache3() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A");
    SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "B");
    SuccessfulCacheableCommand<String> command3 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A");
    assertTrue(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);
    // but the 3rd should come from cache
    assertFalse(command3.executed);
    assertTrue(command3.isResponseFromCache());
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertSaneHystrixRequestLog(3);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 52 with TestCircuitBreaker

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

the class HystrixCommandTest method testBadRequestExceptionViaQueueInThreadOnResponseFromCache.

/**
     * Test that BadRequestException behavior works the same on a cached response.
     */
@Test
public void testBadRequestExceptionViaQueueInThreadOnResponseFromCache() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    // execute once to cache the value
    BadRequestCommand command1 = null;
    try {
        command1 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command1.execute();
    } catch (Throwable e) {
    // ignore
    }
    BadRequestCommand command2 = null;
    try {
        command2 = new BadRequestCommand(circuitBreaker, ExecutionIsolationStrategy.THREAD);
        command2.queue().get();
        fail("we expect to receive a " + HystrixBadRequestException.class.getSimpleName());
    } catch (ExecutionException e) {
        e.printStackTrace();
        if (e.getCause() instanceof HystrixBadRequestException) {
        // success
        } else {
            fail("We expect a " + HystrixBadRequestException.class.getSimpleName() + " but got a " + e.getClass().getSimpleName());
        }
    }
    assertCommandExecutionEvents(command1, HystrixEventType.BAD_REQUEST);
    assertCommandExecutionEvents(command2, HystrixEventType.BAD_REQUEST, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 53 with TestCircuitBreaker

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

the class HystrixCommandTest method testSemaphoreExecutionWithTimeout.

@Test
public void testSemaphoreExecutionWithTimeout() {
    TestHystrixCommand<Boolean> cmd = new InterruptibleCommand(new TestCircuitBreaker(), false);
    System.out.println("Starting command");
    long timeMillis = System.currentTimeMillis();
    try {
        cmd.execute();
        fail("Should throw");
    } catch (Throwable t) {
        assertNotNull(cmd.getExecutionException());
        System.out.println("Unsuccessful Execution took : " + (System.currentTimeMillis() - timeMillis));
        assertCommandExecutionEvents(cmd, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
        assertEquals(0, cmd.metrics.getCurrentConcurrentExecutionCount());
        assertSaneHystrixRequestLog(1);
    }
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 54 with TestCircuitBreaker

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

the class HystrixCommandTest method testRejectedThreadWithFallback.

/**
     * Test when a command fails to get queued up in the threadpool where the command implemented getFallback.
     * <p>
     * We specifically want to protect against developers getting random thread exceptions and instead just correctly receives a fallback.
     */
@Test
public void testRejectedThreadWithFallback() throws InterruptedException {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("Rejection-Fallback");
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SingleThreadedPoolWithQueue pool = new SingleThreadedPoolWithQueue(1);
    //command 1 will execute in threadpool (passing through the queue)
    //command 2 will execute after spending time in the queue (after command1 completes)
    //command 3 will get rejected, since it finds pool and queue both full
    TestCommandRejection command1 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_SUCCESS);
    TestCommandRejection command2 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_SUCCESS);
    TestCommandRejection command3 = new TestCommandRejection(key, circuitBreaker, pool, 500, 600, TestCommandRejection.FALLBACK_SUCCESS);
    Observable<Boolean> result1 = command1.observe();
    Observable<Boolean> result2 = command2.observe();
    Thread.sleep(100);
    //command3 should find queue filled, and get rejected
    assertFalse(command3.execute());
    assertTrue(command3.isResponseRejected());
    assertFalse(command1.isResponseRejected());
    assertFalse(command2.isResponseRejected());
    assertTrue(command3.isResponseFromFallback());
    assertNotNull(command3.getExecutionException());
    assertCommandExecutionEvents(command3, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_SUCCESS);
    //await the 2 latent commands
    Observable.merge(result1, result2).toList().toBlocking().single();
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertSaneHystrixRequestLog(3);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 55 with TestCircuitBreaker

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

the class HystrixCommandTest method testNoRequestCache3.

/**
     * Test Request scoped caching with a mixture of commands
     */
@Test
public void testNoRequestCache3() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "A");
    SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "B");
    SuccessfulCacheableCommand<String> command3 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "A");
    assertTrue(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 since we disabled the cache
    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)

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 Notification (rx.Notification)7 OnSubscribe (rx.Observable.OnSubscribe)7 Subscriber (rx.Subscriber)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