Search in sources :

Example 1 with TryableSemaphore

use of com.netflix.hystrix.AbstractCommand.TryableSemaphore in project Hystrix by Netflix.

the class HystrixCommandTest method testExecutionSemaphoreWithExecution.

@Test
public void testExecutionSemaphoreWithExecution() throws Exception {
    final TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    // single thread should work
    TestSemaphoreCommand command1 = new TestSemaphoreCommand(circuitBreaker, 1, 200, TestSemaphoreCommand.RESULT_SUCCESS, TestSemaphoreCommand.FALLBACK_NOT_IMPLEMENTED);
    boolean result = command1.execute();
    assertFalse(command1.isExecutedInThread());
    assertTrue(result);
    final ArrayBlockingQueue<Boolean> results = new ArrayBlockingQueue<Boolean>(2);
    final AtomicBoolean exceptionReceived = new AtomicBoolean();
    final TryableSemaphore 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 {
                results.add(command2.execute());
            } 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 {
                results.add(command3.execute());
            } 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();
    // make sure that t2 gets a chance to run before queuing the next one
    Thread.sleep(50);
    t3.start();
    t2.join();
    t3.join();
    if (!exceptionReceived.get()) {
        fail("We expected an exception on the 2nd get");
    }
    // only 1 value is expected as the other should have thrown an exception
    assertEquals(1, results.size());
    // should contain only a true result
    assertTrue(results.contains(Boolean.TRUE));
    assertFalse(results.contains(Boolean.FALSE));
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command3, HystrixEventType.SEMAPHORE_REJECTED, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(3);
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) TryableSemaphoreActual(com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) TryableSemaphore(com.netflix.hystrix.AbstractCommand.TryableSemaphore) Test(org.junit.Test)

Example 2 with TryableSemaphore

use of com.netflix.hystrix.AbstractCommand.TryableSemaphore in project Hystrix by Netflix.

the class HystrixCommandTest method testExecutionSemaphoreWithQueue.

@Test
public void testExecutionSemaphoreWithQueue() throws Exception {
    final TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
    // single thread should work
    TestSemaphoreCommand command1 = new TestSemaphoreCommand(circuitBreaker, 1, 200, TestSemaphoreCommand.RESULT_SUCCESS, TestSemaphoreCommand.FALLBACK_NOT_IMPLEMENTED);
    boolean result = command1.queue().get();
    assertTrue(result);
    final AtomicBoolean exceptionReceived = new AtomicBoolean();
    final TryableSemaphore 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.queue().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.queue().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();
    // make sure that t2 gets a chance to run before queuing the next one
    Thread.sleep(50);
    t3.start();
    t2.join();
    t3.join();
    if (!exceptionReceived.get()) {
        fail("We expected an exception on the 2nd get");
    }
    assertCommandExecutionEvents(command1, HystrixEventType.SUCCESS);
    assertCommandExecutionEvents(command2, 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) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) TryableSemaphoreActual(com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) TryableSemaphore(com.netflix.hystrix.AbstractCommand.TryableSemaphore) 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)

Aggregations

TryableSemaphore (com.netflix.hystrix.AbstractCommand.TryableSemaphore)2 TryableSemaphoreActual (com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual)2 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)2 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)2 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)2 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)2 IOException (java.io.IOException)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1