Search in sources :

Example 16 with TestCircuitBreaker

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

the class HystrixCommandTest method testDoNotInterruptToObservableOnTimeoutIfPropertySaysNotTo.

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

Example 17 with TestCircuitBreaker

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

the class HystrixCommandTest method testFallbackSemaphore.

@Test
public void testFallbackSemaphore() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    // single thread should work
    TestSemaphoreCommandWithSlowFallback command1 = new TestSemaphoreCommandWithSlowFallback(circuitBreaker, 1, 200);
    boolean result = command1.queue().get();
    assertTrue(result);
    // 2 threads, the second should be rejected by the fallback semaphore
    boolean exceptionReceived = false;
    Future<Boolean> result2 = null;
    TestSemaphoreCommandWithSlowFallback command2 = null;
    TestSemaphoreCommandWithSlowFallback command3 = null;
    try {
        System.out.println("c2 start: " + System.currentTimeMillis());
        command2 = new TestSemaphoreCommandWithSlowFallback(circuitBreaker, 1, 800);
        result2 = command2.queue();
        System.out.println("c2 after queue: " + System.currentTimeMillis());
        // make sure that thread gets a chance to run before queuing the next one
        Thread.sleep(50);
        System.out.println("c3 start: " + System.currentTimeMillis());
        command3 = new TestSemaphoreCommandWithSlowFallback(circuitBreaker, 1, 200);
        Future<Boolean> result3 = command3.queue();
        System.out.println("c3 after queue: " + System.currentTimeMillis());
        result3.get();
    } catch (Exception e) {
        e.printStackTrace();
        exceptionReceived = true;
    }
    assertTrue(result2.get());
    if (!exceptionReceived) {
        fail("We expected an exception on the 2nd get");
    }
    assertCommandExecutionEvents(command1, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_REJECTION);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
}
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) 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 18 with TestCircuitBreaker

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

the class HystrixCommandTest method testCancelFutureWithInterruptionWhenPropertySaysNotTo.

@Test
public void testCancelFutureWithInterruptionWhenPropertySaysNotTo() throws InterruptedException, ExecutionException {
    // given
    InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true, false, 1000);
    // when
    Future<Boolean> f = cmd.queue();
    Thread.sleep(500);
    f.cancel(true);
    Thread.sleep(500);
    // then
    try {
        f.get();
        fail("Should have thrown a CancellationException");
    } catch (CancellationException e) {
        assertFalse(cmd.hasBeenInterrupted());
    }
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) CancellationException(java.util.concurrent.CancellationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 19 with TestCircuitBreaker

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

the class HystrixCommandTest method testRequestCacheOnTimeoutCausesNullPointerException.

@Test
public void testRequestCacheOnTimeoutCausesNullPointerException() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    RequestCacheNullPointerExceptionCase command1 = new RequestCacheNullPointerExceptionCase(circuitBreaker);
    RequestCacheNullPointerExceptionCase command2 = new RequestCacheNullPointerExceptionCase(circuitBreaker);
    RequestCacheNullPointerExceptionCase command3 = new RequestCacheNullPointerExceptionCase(circuitBreaker);
    // Expect it to time out - all results should be false
    assertFalse(command1.execute());
    // return from cache #1
    assertFalse(command2.execute());
    // return from cache #2
    assertFalse(command3.execute());
    // timeout on command is set to 200ms
    Thread.sleep(500);
    RequestCacheNullPointerExceptionCase command4 = new RequestCacheNullPointerExceptionCase(circuitBreaker);
    // return from cache #3
    Boolean value = command4.execute();
    assertFalse(value);
    RequestCacheNullPointerExceptionCase command5 = new RequestCacheNullPointerExceptionCase(circuitBreaker);
    // return from cache #4
    Future<Boolean> f = command5.queue();
    // the bug is that we're getting a null Future back, rather than a Future that returns false
    assertNotNull(f);
    assertFalse(f.get());
    assertTrue(command5.isResponseFromFallback());
    assertTrue(command5.isResponseTimedOut());
    assertFalse(command5.isFailedExecution());
    assertFalse(command5.isResponseShortCircuited());
    assertNotNull(command5.getExecutionException());
    assertCommandExecutionEvents(command1, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(command3, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(command4, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertCommandExecutionEvents(command5, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(5);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 20 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testErrorThrownViaObserve.

/**
 * Test a java.lang.Error being thrown
 *
 * @throws InterruptedException
 */
@Test
public void testErrorThrownViaObserve() throws InterruptedException {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    CommandWithErrorThrown command = new CommandWithErrorThrown(circuitBreaker, true);
    final AtomicReference<Throwable> t = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        command.observe().subscribe(new Observer<Boolean>() {

            @Override
            public void onCompleted() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
                t.set(e);
                latch.countDown();
            }

            @Override
            public void onNext(Boolean args) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        fail("we should not get anything thrown, it should be emitted via the Observer#onError method");
    }
    latch.await(1, TimeUnit.SECONDS);
    assertNotNull(t.get());
    t.get().printStackTrace();
    assertTrue(t.get() instanceof HystrixRuntimeException);
    // the actual error is an extra cause level deep because Hystrix needs to wrap Throwable/Error as it's public
    // methods only support Exception and it's not a strong enough reason to break backwards compatibility and jump to version 2.x
    assertEquals("simulated java.lang.Error message", t.get().getCause().getCause().getMessage());
    assertEquals("simulated java.lang.Error message", command.getFailedExecutionException().getCause().getMessage());
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isFailedExecution());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_MISSING);
    assertNotNull(command.getExecutionException());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertFalse(command.isExecutedInThread());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicReference(java.util.concurrent.atomic.AtomicReference) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) 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