Search in sources :

Example 46 with TestCircuitBreaker

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

the class HystrixObservableCommandTest method testRequestCache3UsingThreadIsolation.

/**
     * Test Request scoped caching with a mixture of commands
     */
@Test
public void testRequestCache3UsingThreadIsolation() {
    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.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);
    // but the 3rd should come from cache
    assertFalse(command3.executed);
    assertCommandExecutionEvents(command1, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.EMIT, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.EMIT, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE);
    assertTrue(command3.getExecutionTimeInMilliseconds() == -1);
    assertTrue(command3.isResponseFromCache());
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
}
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 47 with TestCircuitBreaker

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

the class HystrixCommandTest method testNotWrappedExceptionViaObserve.

/**
     * Test an Exception implementing NotWrappedByHystrix being thrown
     *
     * @throws InterruptedException
     */
@Test
public void testNotWrappedExceptionViaObserve() throws InterruptedException {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    CommandWithNotWrappedByHystrixException command = new CommandWithNotWrappedByHystrixException(circuitBreaker);
    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 NotWrappedByHystrixTestException);
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isFailedExecution());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE);
    assertNotNull(command.getExecutionException());
    assertTrue(command.getExecutionException() instanceof NotWrappedByHystrixTestException);
    assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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 48 with TestCircuitBreaker

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

the class HystrixCommandTest method testShortCircuitFallbackCounter.

@Test
public void testShortCircuitFallbackCounter() {
    TestCircuitBreaker circuitBreaker = new TestCircuitBreaker().setForceShortCircuit(true);
    KnownFailureTestCommandWithFallback command1 = new KnownFailureTestCommandWithFallback(circuitBreaker);
    command1.execute();
    KnownFailureTestCommandWithFallback command2 = new KnownFailureTestCommandWithFallback(circuitBreaker);
    command2.execute();
    // will be -1 because it never attempted execution
    assertTrue(command1.getExecutionTimeInMilliseconds() == -1);
    assertTrue(command1.isResponseShortCircuited());
    assertFalse(command1.isResponseTimedOut());
    assertNotNull(command1.getExecutionException());
    assertCommandExecutionEvents(command1, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Test(org.junit.Test)

Example 49 with TestCircuitBreaker

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

the class HystrixCommandTest method testRejectedExecutionSemaphoreWithFallbackViaExecute.

@Test
public void testRejectedExecutionSemaphoreWithFallbackViaExecute() throws Exception {
    final TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    final ArrayBlockingQueue<Boolean> results = new ArrayBlockingQueue<Boolean>(2);
    final AtomicBoolean exceptionReceived = new AtomicBoolean();
    final TestSemaphoreCommandWithFallback command1 = new TestSemaphoreCommandWithFallback(circuitBreaker, 1, 200, false);
    Runnable r1 = new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

        @Override
        public void run() {
            try {
                results.add(command1.execute());
            } catch (Exception e) {
                e.printStackTrace();
                exceptionReceived.set(true);
            }
        }
    });
    final TestSemaphoreCommandWithFallback command2 = new TestSemaphoreCommandWithFallback(circuitBreaker, 1, 200, false);
    Runnable r2 = new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

        @Override
        public void run() {
            try {
                results.add(command2.execute());
            } catch (Exception e) {
                e.printStackTrace();
                exceptionReceived.set(true);
            }
        }
    });
    // 2 threads, the second should be rejected by the semaphore and return fallback
    Thread t1 = new Thread(r1);
    Thread t2 = new Thread(r2);
    t1.start();
    // make sure that t2 gets a chance to run before queuing the next one
    Thread.sleep(50);
    t2.start();
    t1.join();
    t2.join();
    if (exceptionReceived.get()) {
        fail("We should have received a fallback response");
    }
    // both threads should have returned values
    assertEquals(2, results.size());
    // should contain both a true and false result
    assertTrue(results.contains(Boolean.TRUE));
    assertTrue(results.contains(Boolean.FALSE));
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SEMAPHORE_REJECTED, HystrixEventType.FALLBACK_SUCCESS);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(2);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) 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 50 with TestCircuitBreaker

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

the class HystrixCommandTest method testInterruptObserveOnTimeout.

@Test
public void testInterruptObserveOnTimeout() throws InterruptedException {
    // given
    InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true);
    // when
    cmd.observe().subscribe();
    // then
    Thread.sleep(500);
    assertTrue(cmd.hasBeenInterrupted());
}
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