Package com.netflix.hystrix.strategy.concurrency

Examples of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext


    @Autowired
    private UserService userService;

    @Test
    public void testCollapser() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUser("1", "name: ");
            User u2 = userService.getUser("2", "name: ");
            User u3 = userService.getUser("3", "name: ");
            User u4 = userService.getUser("4", "name: ");

            assertEquals("name: 1", u1.getName());
            assertEquals("name: 2", u2.getName());
            assertEquals("name: 3", u3.getName());
            assertEquals("name: 4", u4.getName());

            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();
            assertEquals("getUser", command.getCommandKey().name());
            //When a command is fronted by an HystrixCollapser then this marks how many requests are collapsed into the single command execution.
            assertEquals(4, command.getMetrics().getCumulativeCount(HystrixRollingNumberEvent.COLLAPSED));
            // confirm that it was a COLLAPSED command execution
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
            // and that it was successful
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here


    @Autowired
    private UserService userService;

    @Test
    public void testGetUserByIdObservable() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {

            // blocking
            assertEquals("name: 1", userService.getUser("1", "name: ").toBlockingObservable().single().getName());

            // non-blocking
            // - this is a verbose anonymous inner-class approach and doesn't do assertions
            Observable<User> fUser = userService.getUser("1", "name: ");
            fUser.subscribe(new Observer<User>() {

                @Override
                public void onCompleted() {
                    // nothing needed here
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(User v) {
                    System.out.println("onNext: " + v);
                }

            });

            Observable<User> fs = userService.getUser("1", "name: ");
            fs.subscribe(new Action1<User>() {

                @Override
                public void call(User user) {
                    assertEquals("name: 1", user.getName());
                }
            });
            assertEquals(3, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey("getUser");
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testGetUserWithFallback() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            final User exUser = new User("def", "def");

            // blocking
            assertEquals(exUser, userService.getUser(" ", "").toBlockingObservable().single());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey("getUser");
            // confirm that command has failed
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and that fallback was successful
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    @Autowired
    private UserService userService;

    @Test
    public void testGetUserAsyncWithFallback() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsync(" ", "name: ");

            assertEquals("def", f1.get().getName());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();
            assertEquals("getUserAsync", command.getCommandKey().name());

            // confirm that 'getUserAsync' command has failed
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and that fallback waw successful
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testGetUserSyncWithFallback() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUserSync(" ", "name: ");

            assertEquals("def", u1.getName());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();

            assertEquals("getUserSync", command.getCommandKey().name());
            // confirm that command has failed
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and that fallback was successful
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

     */


    @Test
    public void testGetUserAsyncWithFallbackCommand() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsyncFallbackCommand(" ", "name: ");

            assertEquals("def", f1.get().getName());

            assertEquals(3, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> getUserAsyncFallbackCommand = getHystrixCommandByKey(
                    "getUserAsyncFallbackCommand");
            com.netflix.hystrix.HystrixCommand firstFallbackCommand = getHystrixCommandByKey("firstFallbackCommand");
            com.netflix.hystrix.HystrixCommand secondFallbackCommand = getHystrixCommandByKey("secondFallbackCommand");

            assertEquals("getUserAsyncFallbackCommand", getUserAsyncFallbackCommand.getCommandKey().name());
            // confirm that command has failed
            assertTrue(getUserAsyncFallbackCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // confirm that first fallback has failed
            assertTrue(firstFallbackCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and that second fallback was successful
            assertTrue(secondFallbackCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testGetUserSyncWithFallbackCommand() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUserSyncFallbackCommand(" ", "name: ");

            assertEquals("def", u1.getName());
            assertEquals(3, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> getUserSyncFallbackCommand = getHystrixCommandByKey(
                    "getUserSyncFallbackCommand");
            com.netflix.hystrix.HystrixCommand firstFallbackCommand = getHystrixCommandByKey("firstFallbackCommand");
            com.netflix.hystrix.HystrixCommand secondFallbackCommand = getHystrixCommandByKey("secondFallbackCommand");

            assertEquals("getUserSyncFallbackCommand", getUserSyncFallbackCommand.getCommandKey().name());
            // confirm that command has failed
            assertTrue(getUserSyncFallbackCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // confirm that first fallback has failed
            assertTrue(firstFallbackCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and that second fallback was successful
            assertTrue(secondFallbackCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

            public <T> Callable<T> wrapCallable(final Callable<T> callable) {
                return new RequestIdCallable<T>(callable);
            }
        });

        HystrixRequestContext context = HystrixRequestContext.initializeContext();

        testRequestIdThreadLocal.set("foobar");
        final AtomicReference<String> valueInTimeout = new AtomicReference<String>();

        new DummyCommand().toObservable()
                .doOnError(new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        System.out.println("initialized = " + HystrixRequestContext.isCurrentThreadInitialized());
                        System.out.println("requestId (timeout) = " + testRequestIdThreadLocal.get());
                        valueInTimeout.set(testRequestIdThreadLocal.get());
                    }
                })
                .materialize()
                .toBlocking().single();

        context.shutdown();
        Hystrix.reset();
       
        assertEquals("foobar", valueInTimeout.get());
    }
View Full Code Here

public class HystrixRequestCacheTest {

    @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").toBlocking().last());
            assertEquals("b1", cache1.get("valueB").toBlocking().last());

            assertEquals("a3", cache2.get("valueA").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();
        }
    }
View Full Code Here

    }

    @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").toBlocking().last());
            cache1.clear("valueA");
            assertNull(cache1.get("valueA"));
        } catch (Exception e) {
            fail("Exception: " + e.getMessage());
            e.printStackTrace();
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

TOP

Related Classes of com.netflix.hystrix.strategy.concurrency.HystrixRequestContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.