Search in sources :

Example 11 with HystrixRequestContext

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

the class HystrixRequestCacheTest method testCache.

@Test
public void testCache() {
    HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
        cache1.putIfAbsent("valueA", new TestObservable("a1"));
        cache1.putIfAbsent("valueA", new TestObservable("a2"));
        cache1.putIfAbsent("valueB", new TestObservable("b1"));
        HystrixRequestCache cache2 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command2"), strategy);
        cache2.putIfAbsent("valueA", new TestObservable("a3"));
        assertEquals("a1", cache1.get("valueA").toObservable().toBlocking().last());
        assertEquals("b1", cache1.get("valueB").toObservable().toBlocking().last());
        assertEquals("a3", cache2.get("valueA").toObservable().toBlocking().last());
        assertNull(cache2.get("valueB"));
    } catch (Exception e) {
        fail("Exception: " + e.getMessage());
        e.printStackTrace();
    } finally {
        context.shutdown();
    }
    context = HystrixRequestContext.initializeContext();
    try {
        // with a new context  the instance should have nothing in it
        HystrixRequestCache cache = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
        assertNull(cache.get("valueA"));
        assertNull(cache.get("valueB"));
    } finally {
        context.shutdown();
    }
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Test(org.junit.Test)

Example 12 with HystrixRequestContext

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

the class HystrixRequestCacheTest method testClearCache.

@Test
public void testClearCache() {
    HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
        cache1.putIfAbsent("valueA", new TestObservable("a1"));
        assertEquals("a1", cache1.get("valueA").toObservable().toBlocking().last());
        cache1.clear("valueA");
        assertNull(cache1.get("valueA"));
    } catch (Exception e) {
        fail("Exception: " + e.getMessage());
        e.printStackTrace();
    } finally {
        context.shutdown();
    }
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Test(org.junit.Test)

Example 13 with HystrixRequestContext

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

the class HystrixCommandAsyncDemo method startDemo.

public void startDemo(final boolean shouldLog) {
    startMetricsMonitor(shouldLog);
    while (true) {
        final HystrixRequestContext context = HystrixRequestContext.initializeContext();
        Observable<CreditCardAuthorizationResult> o = observeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment();
        final CountDownLatch latch = new CountDownLatch(1);
        o.subscribe(new Subscriber<CreditCardAuthorizationResult>() {

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

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                latch.countDown();
                context.shutdown();
            }

            @Override
            public void onNext(CreditCardAuthorizationResult creditCardAuthorizationResult) {
                if (shouldLog) {
                    System.out.println("Request => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
                }
            }
        });
        try {
            latch.await(5000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ex) {
            System.out.println("INTERRUPTED!");
        }
    }
}
Also used : HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with HystrixRequestContext

use of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext in project ribbon by Netflix.

the class RibbonTest method testHystrixCache.

@Test
public void testHystrixCache() throws IOException {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
    server.enqueue(response);
    server.enqueue(response);
    server.play();
    HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("myclient").build();
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("http://localhost:" + server.getPort()).withMethod("GET").withRequestCacheKey("xyz").build();
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        RibbonResponse<ByteBuf> ribbonResponse = request.withMetadata().execute();
        assertFalse(ribbonResponse.getHystrixInfo().isResponseFromCache());
        ribbonResponse = request.withMetadata().execute();
        assertTrue(ribbonResponse.getHystrixInfo().isResponseFromCache());
    } finally {
        context.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)14 Test (org.junit.Test)11 HystrixConcurrencyStrategy (com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy)6 ArrayList (java.util.ArrayList)4 Observable (rx.Observable)4 RequestCollapser (com.netflix.hystrix.collapser.RequestCollapser)2 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)2 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)2 HystrixTimeoutException (com.netflix.hystrix.exception.HystrixTimeoutException)2 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)2 List (java.util.List)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Notification (rx.Notification)2 Action0 (rx.functions.Action0)2 Action1 (rx.functions.Action1)2 Func1 (rx.functions.Func1)2 MockResponse (com.google.mockwebserver.MockResponse)1