Search in sources :

Example 6 with HystrixConcurrencyStrategy

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

the class HystrixCommandTestWithCustomConcurrencyStrategy method testCommandRequiresContextConcurrencyStrategyProvidesItContextSetUpCorrectly.

/**
     * HystrixConcurrencyStrategy
     ** useDefaultRequestContext : true
     * HystrixCommand
     ** useRequestCache   : true
     ** useRequestLog     : true
     *
     * OUTCOME: RequestLog set up properly in command
     */
@Test
public void testCommandRequiresContextConcurrencyStrategyProvidesItContextSetUpCorrectly() {
    HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true);
    HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy);
    //context is set up properly
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    HystrixCommand<Boolean> cmd = new TestCommand(true, true);
    assertTrue(cmd.execute());
    printRequestLog();
    assertNotNull(HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertNotNull(cmd.currentRequestLog);
    context.shutdown();
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Test(org.junit.Test)

Example 7 with HystrixConcurrencyStrategy

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

the class HystrixCommandTestWithCustomConcurrencyStrategy method testCommandDoesNotRequireContextConcurrencyStrategyProvidesItContextSetUpCorrectly.

/**
     * HystrixConcurrencyStrategy
     ** useDefaultRequestContext : true
     * HystrixCommand
     ** useRequestCache   : false
     ** useRequestLog     : false
     *
     * OUTCOME: RequestLog not set up in command, static access works properly
     */
@Test
public void testCommandDoesNotRequireContextConcurrencyStrategyProvidesItContextSetUpCorrectly() {
    HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(true);
    HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy);
    //context is set up properly
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    HystrixCommand<Boolean> cmd = new TestCommand(false, false);
    assertTrue(cmd.execute());
    printRequestLog();
    assertNotNull(HystrixRequestLog.getCurrentRequest());
    assertNotNull(HystrixRequestLog.getCurrentRequest(strategy));
    assertNull(cmd.currentRequestLog);
    context.shutdown();
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Test(org.junit.Test)

Example 8 with HystrixConcurrencyStrategy

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

the class HystrixCommandTestWithCustomConcurrencyStrategy method testCommandDoesNotRequireContextConcurrencyStrategyDoesNotProvideItContextSetUpCorrectly.

/**
     * HystrixConcurrencyStrategy
     ** useDefaultRequestContext : false
     * HystrixCommand
     ** useRequestCache   : false
     ** useRequestLog     : false
     *
     * OUTCOME: RequestLog not set up in command, not available statically
     */
@Test
public void testCommandDoesNotRequireContextConcurrencyStrategyDoesNotProvideItContextSetUpCorrectly() {
    HystrixConcurrencyStrategy strategy = new CustomConcurrencyStrategy(false);
    HystrixPlugins.getInstance().registerConcurrencyStrategy(strategy);
    //context is set up properly
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    HystrixCommand<Boolean> cmd = new TestCommand(true, true);
    assertTrue(cmd.execute());
    printRequestLog();
    assertNull(HystrixRequestLog.getCurrentRequest());
    assertNull(HystrixRequestLog.getCurrentRequest(strategy));
    assertNull(cmd.currentRequestLog);
    context.shutdown();
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Test(org.junit.Test)

Example 9 with HystrixConcurrencyStrategy

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

the class HystrixRequestCacheTest method testCacheWithoutRequestContext.

@Test
public void testCacheWithoutRequestContext() {
    HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
    //HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        HystrixRequestCache cache1 = HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("command1"), strategy);
        //this should fail, as there's no HystrixRequestContext instance to place the cache into
        cache1.putIfAbsent("valueA", new TestObservable("a1"));
        fail("should throw an exception on cache put");
    } catch (Exception e) {
        //expected
        e.printStackTrace();
    }
}
Also used : HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy) Test(org.junit.Test)

Example 10 with HystrixConcurrencyStrategy

use of com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy 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)

Aggregations

HystrixConcurrencyStrategy (com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy)11 Test (org.junit.Test)11 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)6