Search in sources :

Example 1 with TestCircuitBreaker

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

the class HystrixCommandTest method testDoNotInterruptObserveOnTimeoutIfPropertySaysNotTo.

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

Example 2 with TestCircuitBreaker

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

the class HystrixCommandTest method testOnRunStartHookThrowsThreadIsolated.

@Test
public void testOnRunStartHookThrowsThreadIsolated() {
    final AtomicBoolean exceptionEncountered = new AtomicBoolean(false);
    final AtomicBoolean onThreadStartInvoked = new AtomicBoolean(false);
    final AtomicBoolean onThreadCompleteInvoked = new AtomicBoolean(false);
    final AtomicBoolean executionAttempted = new AtomicBoolean(false);
    class FailureInjectionHook extends HystrixCommandExecutionHook {

        @Override
        public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
            throw new HystrixRuntimeException(HystrixRuntimeException.FailureType.COMMAND_EXCEPTION, commandInstance.getClass(), "Injected Failure", null, null);
        }

        @Override
        public <T> void onThreadStart(HystrixInvokable<T> commandInstance) {
            onThreadStartInvoked.set(true);
            super.onThreadStart(commandInstance);
        }

        @Override
        public <T> void onThreadComplete(HystrixInvokable<T> commandInstance) {
            onThreadCompleteInvoked.set(true);
            super.onThreadComplete(commandInstance);
        }
    }
    final FailureInjectionHook failureInjectionHook = new FailureInjectionHook();
    class FailureInjectedCommand extends TestHystrixCommand<Integer> {

        public FailureInjectedCommand(ExecutionIsolationStrategy isolationStrategy) {
            super(testPropsBuilder(new TestCircuitBreaker()).setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolationStrategy)), failureInjectionHook);
        }

        @Override
        protected Integer run() throws Exception {
            executionAttempted.set(true);
            return 3;
        }
    }
    TestHystrixCommand<Integer> threadCmd = new FailureInjectedCommand(ExecutionIsolationStrategy.THREAD);
    try {
        int result = threadCmd.execute();
        System.out.println("RESULT : " + result);
    } catch (Throwable ex) {
        ex.printStackTrace();
        exceptionEncountered.set(true);
    }
    assertTrue(exceptionEncountered.get());
    assertTrue(onThreadStartInvoked.get());
    assertTrue(onThreadCompleteInvoked.get());
    assertFalse(executionAttempted.get());
    assertEquals(0, threadCmd.metrics.getCurrentConcurrentExecutionCount());
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixCommandExecutionHook(com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) ExecutionIsolationStrategy(com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 3 with TestCircuitBreaker

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

the class HystrixCommandTest method testNoRequestCacheOnTimeoutThrowsException.

@Test
public void testNoRequestCacheOnTimeoutThrowsException() throws Exception {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    NoRequestCacheTimeoutWithoutFallback r1 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        System.out.println("r1 value: " + r1.execute());
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r1.isResponseTimedOut());
    // what we want
    }
    NoRequestCacheTimeoutWithoutFallback r2 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r2.execute();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r2.isResponseTimedOut());
    // what we want
    }
    NoRequestCacheTimeoutWithoutFallback r3 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    Future<Boolean> f3 = r3.queue();
    try {
        f3.get();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (ExecutionException e) {
        e.printStackTrace();
        assertTrue(r3.isResponseTimedOut());
    // what we want
    }
    // timeout on command is set to 200ms
    Thread.sleep(500);
    NoRequestCacheTimeoutWithoutFallback r4 = new NoRequestCacheTimeoutWithoutFallback(circuitBreaker);
    try {
        r4.execute();
        // we should have thrown an exception
        fail("expected a timeout");
    } catch (HystrixRuntimeException e) {
        assertTrue(r4.isResponseTimedOut());
        assertFalse(r4.isResponseFromFallback());
    // what we want
    }
    assertCommandExecutionEvents(r1, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r2, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r3, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertCommandExecutionEvents(r4, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(4);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with TestCircuitBreaker

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

the class HystrixCommandTest 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 5 with TestCircuitBreaker

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

the class HystrixCommandTest method testCancelFutureWithoutInterruption.

@Test
public void testCancelFutureWithoutInterruption() throws InterruptedException, ExecutionException, TimeoutException {
    // given
    InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true, true, 1000);
    // when
    Future<Boolean> f = cmd.queue();
    Thread.sleep(500);
    f.cancel(false);
    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)

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