Search in sources :

Example 1 with HystrixContextScheduler

use of com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testBadFailureRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler.

/**
 * Async Observable and semaphore isolation WITH functioning RequestContext
 *
 * Use HystrixContextScheduler to make the user provided scheduler capture context.
 */
@Test
public void testBadFailureRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler() {
    RequestContextTestResults results = testRequestContextOnBadFailure(ExecutionIsolationStrategy.THREAD, new HystrixContextScheduler(Schedulers.newThread()));
    // the user scheduler captures context
    assertTrue(results.isContextInitialized.get());
    // the user provided thread/scheduler
    assertTrue(results.originThread.get().getName().startsWith("RxNewThread"));
    // the user scheduler captures context
    assertTrue(results.isContextInitializedObserveOn.get());
    assertTrue(results.observeOnThread.get().getName().startsWith("RxNewThread"));
    // thread isolated
    assertTrue(results.command.isExecutedInThread());
}
Also used : HystrixContextScheduler(com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler) Test(org.junit.Test)

Example 2 with HystrixContextScheduler

use of com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testFailureWithFallbackRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler.

/**
 * Async Observable and semaphore isolation WITH functioning RequestContext
 *
 * Use HystrixContextScheduler to make the user provided scheduler capture context.
 */
@Test
public void testFailureWithFallbackRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler() {
    RequestContextTestResults results = testRequestContextOnFailureWithFallback(ExecutionIsolationStrategy.THREAD, new HystrixContextScheduler(Schedulers.newThread()));
    // the user scheduler captures context
    assertTrue(results.isContextInitialized.get());
    // the user provided thread/scheduler
    assertTrue(results.originThread.get().getName().startsWith("RxNewThread"));
    // the user scheduler captures context
    assertTrue(results.isContextInitializedObserveOn.get());
    assertTrue(results.observeOnThread.get().getName().startsWith("RxNewThread"));
    // thread isolated
    assertTrue(results.command.isExecutedInThread());
}
Also used : HystrixContextScheduler(com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler) Test(org.junit.Test)

Example 3 with HystrixContextScheduler

use of com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testTimeoutWithFallbackRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler.

/**
 * Async Observable and semaphore isolation WITH functioning RequestContext
 *
 * Use HystrixContextScheduler to make the user provided scheduler capture context.
 */
@Test
public void testTimeoutWithFallbackRequestContextWithThreadIsolatedAsynchronousObservableAndCapturedContextScheduler() {
    RequestContextTestResults results = testRequestContextOnTimeoutWithFallback(ExecutionIsolationStrategy.THREAD, new HystrixContextScheduler(Schedulers.newThread()));
    // the user scheduler captures context
    assertTrue(results.isContextInitialized.get());
    // the user provided thread/scheduler
    assertTrue(results.originThread.get().getName().startsWith("RxNewThread"));
    // the user scheduler captures context
    assertTrue(results.isContextInitializedObserveOn.get());
    // the user provided thread/scheduler
    assertTrue(results.observeOnThread.get().getName().startsWith("RxNewThread"));
    // thread isolated
    assertTrue(results.command.isExecutedInThread());
    try {
        Thread.sleep(100);
    } catch (InterruptedException ex) {
    }
// HystrixCircuitBreaker.Factory.reset();
}
Also used : HystrixContextScheduler(com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler) Test(org.junit.Test)

Example 4 with HystrixContextScheduler

use of com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testShortCircuitedWithFallbackRequestContextWithSemaphoreIsolatedAsynchronousObservableAndCapturedContextScheduler.

/**
 * Async Observable and semaphore isolation WITH functioning RequestContext
 *
 * Use HystrixContextScheduler to make the user provided scheduler capture context.
 */
@Test
public void testShortCircuitedWithFallbackRequestContextWithSemaphoreIsolatedAsynchronousObservableAndCapturedContextScheduler() {
    RequestContextTestResults results = testRequestContextOnShortCircuitedWithFallback(ExecutionIsolationStrategy.SEMAPHORE, new HystrixContextScheduler(Schedulers.newThread()));
    // the user scheduler captures context
    assertTrue(results.isContextInitialized.get());
    // the user provided thread/scheduler
    assertTrue(results.originThread.get().getName().startsWith("RxNewThread"));
    // the user scheduler captures context
    assertTrue(results.isContextInitializedObserveOn.get());
    // the user provided thread/scheduler
    assertTrue(results.observeOnThread.get().getName().startsWith("RxNewThread"));
    // semaphore isolated
    assertFalse(results.command.isExecutedInThread());
}
Also used : HystrixContextScheduler(com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler) Test(org.junit.Test)

Example 5 with HystrixContextScheduler

use of com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testRequestContextOnRejectionWithFallback.

private RequestContextTestResults testRequestContextOnRejectionWithFallback(ExecutionIsolationStrategy isolation, final Scheduler userScheduler) {
    final RequestContextTestResults results = new RequestContextTestResults();
    TestHystrixObservableCommand<Boolean> command = new TestHystrixObservableCommand<Boolean>(TestHystrixObservableCommand.testPropsBuilder(new TestCircuitBreaker()).setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolation).withExecutionIsolationSemaphoreMaxConcurrentRequests(0)).setThreadPool(new HystrixThreadPool() {

        @Override
        public ThreadPoolExecutor getExecutor() {
            return null;
        }

        @Override
        public void markThreadExecution() {
        }

        @Override
        public void markThreadCompletion() {
        }

        @Override
        public void markThreadRejection() {
        }

        @Override
        public boolean isQueueSpaceAvailable() {
            // always return false so we reject everything
            return false;
        }

        @Override
        public Scheduler getScheduler() {
            return new HystrixContextScheduler(HystrixPlugins.getInstance().getConcurrencyStrategy(), this);
        }

        @Override
        public Scheduler getScheduler(Func0<Boolean> shouldInterruptThread) {
            return new HystrixContextScheduler(HystrixPlugins.getInstance().getConcurrencyStrategy(), this, shouldInterruptThread);
        }
    })) {

        @Override
        protected Observable<Boolean> construct() {
            return Observable.create(new OnSubscribe<Boolean>() {

                @Override
                public void call(Subscriber<? super Boolean> s) {
                    s.onError(new RuntimeException("onError"));
                }
            }).subscribeOn(userScheduler);
        }

        @Override
        protected Observable<Boolean> resumeWithFallback() {
            return Observable.create(new OnSubscribe<Boolean>() {

                @Override
                public void call(Subscriber<? super Boolean> s) {
                    results.isContextInitialized.set(HystrixRequestContext.isCurrentThreadInitialized());
                    results.originThread.set(Thread.currentThread());
                    s.onNext(false);
                    s.onCompleted();
                }
            }).subscribeOn(userScheduler);
        }
    };
    results.command = command;
    command.toObservable().doOnEach(new Action1<Notification<? super Boolean>>() {

        @Override
        public void call(Notification<? super Boolean> n) {
            results.isContextInitializedObserveOn.set(HystrixRequestContext.isCurrentThreadInitialized());
            results.observeOnThread.set(Thread.currentThread());
        }
    }).subscribe(results.ts);
    results.ts.awaitTerminalEvent();
    System.out.println("Run => Initialized: " + results.isContextInitialized.get() + "  Thread: " + results.originThread.get());
    System.out.println("Observed => Initialized: " + results.isContextInitializedObserveOn.get() + "  Thread: " + results.observeOnThread.get());
    assertEquals(0, results.ts.getOnErrorEvents().size());
    assertEquals(1, results.ts.getOnNextEvents().size());
    assertEquals(false, results.ts.getOnNextEvents().get(0));
    assertFalse(command.isSuccessfulExecution());
    assertTrue(command.isResponseRejected());
    if (isolation == ExecutionIsolationStrategy.SEMAPHORE) {
        assertCommandExecutionEvents(command, HystrixEventType.SEMAPHORE_REJECTED, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS);
    } else {
        assertCommandExecutionEvents(command, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS);
    }
    assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    return results;
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Action1(rx.functions.Action1) HystrixContextScheduler(com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler) OnSubscribe(rx.Observable.OnSubscribe) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) TestSubscriber(rx.observers.TestSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Func0(rx.functions.Func0)

Aggregations

HystrixContextScheduler (com.netflix.hystrix.strategy.concurrency.HystrixContextScheduler)16 Test (org.junit.Test)15 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 OnSubscribe (rx.Observable.OnSubscribe)1 Action1 (rx.functions.Action1)1 Func0 (rx.functions.Func0)1 TestSubscriber (rx.observers.TestSubscriber)1