Search in sources :

Example 76 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testInterruptToObservableOnTimeout.

@Test
public void testInterruptToObservableOnTimeout() throws InterruptedException {
    // given
    InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true);
    // when
    cmd.toObservable().subscribe();
    // then
    Thread.sleep(500);
    assertTrue(cmd.hasBeenInterrupted());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 77 with TestCircuitBreaker

use of com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker 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 78 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testObserveFailureWithFallback.

private void testObserveFailureWithFallback(ExecutionIsolationStrategy isolationStrategy, boolean asyncException) {
    TestHystrixObservableCommand<Boolean> command = new KnownFailureTestCommandWithFallback(new TestCircuitBreaker(), isolationStrategy, asyncException);
    try {
        assertEquals(false, command.observe().toBlocking().single());
    } catch (Exception e) {
        e.printStackTrace();
        fail("We should have received a response from the fallback.");
    }
    assertEquals("we failed with a simulated issue", command.getFailedExecutionException().getMessage());
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isFailedExecution());
    assertTrue(command.isResponseFromFallback());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS);
    assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    assertNotNull(command.getExecutionException());
    assertEquals(isolationStrategy.equals(ExecutionIsolationStrategy.THREAD), command.isExecutedInThread());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) 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)

Example 79 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testNoRequestCache3UsingThreadIsolation.

/**
     * Test Request scoped caching with a mixture of commands
     */
@Test
public void testNoRequestCache3UsingThreadIsolation() {
    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.observe().toBlocking().toFuture();
    Future<String> f2 = command2.observe().toBlocking().toFuture();
    Future<String> f3 = command3.observe().toBlocking().toFuture();
    try {
        assertEquals("A", f1.get());
        assertEquals("B", f2.get());
        assertEquals("A", f3.get());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    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.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
    // thread isolated
    assertTrue(command1.isExecutedInThread());
    assertTrue(command2.isExecutedInThread());
    assertTrue(command3.isExecutedInThread());
}
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) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 80 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testExecutionSemaphoreWithObserve.

@Test
public void testExecutionSemaphoreWithObserve() {
    final TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    TestSemaphoreCommand command1 = new TestSemaphoreCommand(circuitBreaker, 1, 200, TestSemaphoreCommand.RESULT_SUCCESS, TestSemaphoreCommand.FALLBACK_NOT_IMPLEMENTED);
    // single thread should work
    try {
        boolean result = command1.observe().toBlocking().toFuture().get();
        assertTrue(result);
    } catch (Exception e) {
        // we shouldn't fail on this one
        throw new RuntimeException(e);
    }
    final AtomicBoolean exceptionReceived = new AtomicBoolean();
    final TryableSemaphoreActual semaphore = new TryableSemaphoreActual(HystrixProperty.Factory.asProperty(1));
    final TestSemaphoreCommand command2 = new TestSemaphoreCommand(circuitBreaker, semaphore, 200, TestSemaphoreCommand.RESULT_SUCCESS, TestSemaphoreCommand.FALLBACK_NOT_IMPLEMENTED);
    Runnable r2 = new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

        @Override
        public void run() {
            try {
                command2.observe().toBlocking().toFuture().get();
            } catch (Exception e) {
                e.printStackTrace();
                exceptionReceived.set(true);
            }
        }
    });
    final TestSemaphoreCommand command3 = new TestSemaphoreCommand(circuitBreaker, semaphore, 200, TestSemaphoreCommand.RESULT_SUCCESS, TestSemaphoreCommand.FALLBACK_NOT_IMPLEMENTED);
    Runnable r3 = new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

        @Override
        public void run() {
            try {
                command3.observe().toBlocking().toFuture().get();
            } catch (Exception e) {
                e.printStackTrace();
                exceptionReceived.set(true);
            }
        }
    });
    // 2 threads, the second should be rejected by the semaphore
    Thread t2 = new Thread(r2);
    Thread t3 = new Thread(r3);
    t2.start();
    try {
        Thread.sleep(100);
    } catch (Throwable ex) {
        fail(ex.getMessage());
    }
    t3.start();
    try {
        t2.join();
        t3.join();
    } catch (Exception e) {
        e.printStackTrace();
        fail("failed waiting on threads");
    }
    if (!exceptionReceived.get()) {
        fail("We expected an exception on the 2nd get");
    }
    System.out.println("CMD1 : " + command1.getExecutionEvents());
    System.out.println("CMD2 : " + command2.getExecutionEvents());
    System.out.println("CMD3 : " + command3.getExecutionEvents());
    assertCommandExecutionEvents(command1, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.SEMAPHORE_REJECTED, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) TryableSemaphoreActual(com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) 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) 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