Package com.netflix.hystrix.strategy.concurrency

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


        userService.setFailoverService(failoverService);
    }

    @Test(expected = BadRequestException.class)
    public void testGetUserByBadId() throws NotFoundException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            String badId = "";
            userService.getUserById(badId);
        } finally {
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey(COMMAND_KEY);
            // will not affect metrics
            assertFalse(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and will not trigger fallback logic
            verify(failoverService, never()).getDefUser();
            context.shutdown();
        }
    }
View Full Code Here


        }
    }

    @Test(expected = NotFoundException.class)
    public void testGetNonExistentUser() throws NotFoundException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            userService.getUserById("4"); // user with id 4 doesn't exist
        } finally {
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey(COMMAND_KEY);
            // will not affect metrics
            assertFalse(getUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            // and will not trigger fallback logic
            verify(failoverService, never()).getDefUser();
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test // don't expect any exceptions because fallback must be triggered
    public void testActivateUser() throws NotFoundException, ActivationException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            userService.activateUser("1"); // this method always throws ActivationException
        } finally {
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand activateUserCommand = getHystrixCommandByKey("activateUser");
            // will not affect metrics
            assertTrue(activateUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            assertTrue(activateUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_SUCCESS));
            // and will not trigger fallback logic
            verify(failoverService, atLeastOnce()).activate();
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test(expected = HystrixRuntimeException.class)
    public void testBlockUser() throws NotFoundException, ActivationException, OperationException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            userService.blockUser("1"); // this method always throws ActivationException
        } finally {
            assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand activateUserCommand = getHystrixCommandByKey("blockUser");
            // will not affect metrics
            assertTrue(activateUserCommand.getExecutionEvents().contains(HystrixEventType.FAILURE));
            assertTrue(activateUserCommand.getExecutionEvents().contains(HystrixEventType.FALLBACK_FAILURE));
            context.shutdown();
        }
    }
View Full Code Here

* }
* </pre>
*/
public class HystrixRequestContextServletFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            chain.doFilter(request, response);
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    @Autowired
    private UserService userService;

    @Test
    public void testCollapserAsync() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsync("1", "name: ");
            Future<User> f2 = userService.getUserAsync("2", "name: ");
            Future<User> f3 = userService.getUserAsync("3", "name: ");
            Future<User> f4 = userService.getUserAsync("4", "name: ");

            assertEquals("name: 1", f1.get().getName());
            assertEquals("name: 2", f2.get().getName());
            assertEquals("name: 3", f3.get().getName());
            assertEquals("name: 4", f4.get().getName());

            // assert that the batch command 'GetUserCommand' was in fact
            // executed and that it executed only once
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();
            // assert the command is the one we're expecting
            assertEquals("GetUserCommand", command.getCommandKey().name());
            // 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

    /**
     * This test covers situation when fallback isn't enabled in collapser.
     */
    @Test(expected = ExecutionException.class)
    public void testCollapserAsyncNotFound() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsync("1", "name: ");
            Future<User> f2 = userService.getUserAsync("2", "name: ");
            Future<User> f3 = userService.getUserAsync("not found", "name"); // not found, exception here
            Future<User> f4 = userService.getUserAsync("4", "name: "); // will not be processed
            Future<User> f5 = userService.getUserAsync("5", "name: "); // will not be processed
            System.out.println(f1.get().getName()); // this line will be executed
            System.out.println(f2.get().getName()); // this line will be executed
            System.out.println(f3.get().getName()); // this line will not be executed
            System.out.println(f4.get().getName()); // this line will not be executed
            System.out.println(f5.get().getName()); // this line will not be executed

        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    /**
     * This test covers situation when fallback is enabled in collapser.
     */
    @Test
    public void testCollapserAsyncNotFoundWithFallbackEnabled() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsyncWithFallback("1", "name: ");
            Future<User> f2 = userService.getUserAsyncWithFallback("2", "name: ");
            Future<User> f3 = userService.getUserAsyncWithFallback("not found", "name"); // not found, exception here
            Future<User> f4 = userService.getUserAsyncWithFallback("4", "name: ");
            Future<User> f5 = userService.getUserAsyncWithFallback("5", "name: ");


            assertEquals("name: 1", f1.get().getName());
            assertEquals("name: 2", f2.get().getName());
            assertEquals(UserService.DEFAULT_USER, f3.get()); // default value from fallback
            assertEquals("name: 4", f4.get().getName());
            assertEquals("name: 5", f5.get().getName());

            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey("getUserAsyncWithFallback");

            // confirm that it was a COLLAPSED command execution
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));

        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    /**
     * This test covers situation when fallback is enabled in collapser.
     */
    @Test
    public void testCollapserAsyncNotFoundWithFallbackCommandEnabled() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<User> f1 = userService.getUserAsyncWithFallbackCommand("1", "name: ");
            Future<User> f2 = userService.getUserAsyncWithFallbackCommand("2", "name: ");
            Future<User> f3 = userService.getUserAsyncWithFallbackCommand("not found", "name"); // not found, exception here
            Future<User> f4 = userService.getUserAsyncWithFallbackCommand("4", "name: ");
            Future<User> f5 = userService.getUserAsyncWithFallbackCommand("5", "name: ");


            assertEquals("name: 1", f1.get().getName());
            assertEquals("name: 2", f2.get().getName());
            assertEquals(UserService.DEFAULT_USER, f3.get()); // default value from fallback
            assertEquals("name: 4", f4.get().getName());
            assertEquals("name: 5", f5.get().getName());

            assertEquals(2, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixCommand getUserCommand = getHystrixCommandByKey("getUserAsyncWithFallbackCommand");

            // confirm that it was a COLLAPSED command execution
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
            assertTrue(getUserCommand.getExecutionEvents().contains(HystrixEventType.SUCCESS));

        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    }


    @Test
    public void testCollapserSync() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUserSync("1", "name: ");
            User u2 = userService.getUserSync("2", "name: ");
            User u3 = userService.getUserSync("3", "name: ");
            User u4 = userService.getUserSync("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("GetUserCommand", command.getCommandKey().name());
            // 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

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.