Package com.netflix.hystrix.strategy.concurrency

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


    private static final String DIGITS_REGEX = "\\[\\d+";

    @Test
    public void testSuccess() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            new TestCommand("A", false, true).execute();
            String log = HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString();
            // strip the actual count so we can compare reliably
            log = log.replaceAll(DIGITS_REGEX, "[");
            assertEquals("TestCommand[SUCCESS][ms]", log);
        } finally {
            context.shutdown();
        }
    }
View Full Code Here


        }
    }

    @Test
    public void testSuccessFromCache() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            // 1 success
            new TestCommand("A", false, true).execute();
            // 4 success from cache
            new TestCommand("A", false, true).execute();
            new TestCommand("A", false, true).execute();
            new TestCommand("A", false, true).execute();
            new TestCommand("A", false, true).execute();
            String log = HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString();
            // strip the actual count so we can compare reliably
            log = log.replaceAll(DIGITS_REGEX, "[");
            assertEquals("TestCommand[SUCCESS][ms], TestCommand[SUCCESS, RESPONSE_FROM_CACHE][ms]x4", log);
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testFailWithFallbackSuccess() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            // 1 failure
            new TestCommand("A", true, false).execute();
            // 4 failures from cache
            new TestCommand("A", true, false).execute();
            new TestCommand("A", true, false).execute();
            new TestCommand("A", true, false).execute();
            new TestCommand("A", true, false).execute();
            String log = HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString();
            // strip the actual count so we can compare reliably
            log = log.replaceAll(DIGITS_REGEX, "[");
            assertEquals("TestCommand[FAILURE, FALLBACK_SUCCESS][ms], TestCommand[FAILURE, FALLBACK_SUCCESS, RESPONSE_FROM_CACHE][ms]x4", log);
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testFailWithFallbackFailure() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            // 1 failure
            try {
                new TestCommand("A", true, true).execute();
            } catch (Exception e) {
            }
            // 1 failure from cache
            try {
                new TestCommand("A", true, true).execute();
            } catch (Exception e) {
            }
            String log = HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString();
            // strip the actual count so we can compare reliably
            log = log.replaceAll(DIGITS_REGEX, "[");
            assertEquals("TestCommand[FAILURE, FALLBACK_FAILURE][ms], TestCommand[FAILURE, FALLBACK_FAILURE, RESPONSE_FROM_CACHE][ms]", log);
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    }

    @Test
    public void testMultipleCommands() {

        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {

            // 1 success
            new TestCommand("GetData", "A", false, false).execute();

            // 1 success
            new TestCommand("PutData", "B", false, false).execute();

            // 1 success
            new TestCommand("GetValues", "C", false, false).execute();

            // 1 success from cache
            new TestCommand("GetValues", "C", false, false).execute();

            // 1 failure
            try {
                new TestCommand("A", true, true).execute();
            } catch (Exception e) {
            }
            // 1 failure from cache
            try {
                new TestCommand("A", true, true).execute();
            } catch (Exception e) {
            }
            String log = HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString();
            // strip the actual count so we can compare reliably
            log = log.replaceAll(DIGITS_REGEX, "[");
            assertEquals("GetData[SUCCESS][ms], PutData[SUCCESS][ms], GetValues[SUCCESS][ms], GetValues[SUCCESS, RESPONSE_FROM_CACHE][ms], TestCommand[FAILURE, FALLBACK_FAILURE][ms], TestCommand[FAILURE, FALLBACK_FAILURE, RESPONSE_FROM_CACHE][ms]", log);
        } finally {
            context.shutdown();
        }

    }
View Full Code Here

    }

    @Test
    public void testMaxLimit() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            for (int i = 0; i < HystrixRequestLog.MAX_STORAGE; i++) {
                new TestCommand("A", false, true).execute();
            }
            // then execute again some more
            for (int i = 0; i < 10; i++) {
                new TestCommand("A", false, true).execute();
            }

            assertEquals(HystrixRequestLog.MAX_STORAGE, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

    }

    @Test
    public void testRequestVariableLifecycle1() throws Exception {
        // simulate request lifecycle
        HystrixRequestContext requestContext = HystrixRequestContext.initializeContext();

        // do actual work
        TestCollapserTimer timer = new TestCollapserTimer();
        Future<String> response1 = new TestRequestCollapser(timer, counter, 1).queue();
        timer.incrementTime(5);
        Future<String> response2 = new TestRequestCollapser(timer, counter, 2).queue();
        timer.incrementTime(8);
        // should execute here
        Future<String> response3 = new TestRequestCollapser(timer, counter, 3).queue();
        timer.incrementTime(6);
        Future<String> response4 = new TestRequestCollapser(timer, counter, 4).queue();
        timer.incrementTime(8);
        // should execute here
        Future<String> response5 = new TestRequestCollapser(timer, counter, 5).queue();
        timer.incrementTime(10);
        // should execute here

        // wait for all tasks to complete
        assertEquals("1", response1.get());
        assertEquals("2", response2.get());
        assertEquals("3", response3.get());
        assertEquals("4", response4.get());
        assertEquals("5", response5.get());

        // each task should have been executed 3 times
        for (TestCollapserTimer.ATask t : timer.tasks) {
            assertEquals(3, t.task.count.get());
        }

        System.out.println("timer.tasks.size() A: " + timer.tasks.size());
        System.out.println("tasks in test: " + timer.tasks);

        // simulate request lifecycle
        requestContext.shutdown();

        System.out.println("timer.tasks.size() B: " + timer.tasks.size());

        HystrixRequestVariableHolder<RequestCollapser<?, ?, ?>> rv = RequestCollapserFactory.getRequestVariable(new TestRequestCollapser(timer, counter, 1).getCollapserKey().name());
View Full Code Here

    }

    @Test
    public void testRequestVariableLifecycle2() throws Exception {
        // simulate request lifecycle
        HystrixRequestContext requestContext = HystrixRequestContext.initializeContext();

        final TestCollapserTimer timer = new TestCollapserTimer();
        final ConcurrentLinkedQueue<Future<String>> responses = new ConcurrentLinkedQueue<Future<String>>();
        ConcurrentLinkedQueue<Thread> threads = new ConcurrentLinkedQueue<Thread>();

        // kick off work (simulating a single request with multiple threads)
        for (int t = 0; t < 5; t++) {
            Thread th = new Thread(new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < 100; i++) {
                        responses.add(new TestRequestCollapser(timer, counter, 1).queue());
                    }
                }
            }));

            threads.add(th);
            th.start();
        }

        for (Thread th : threads) {
            // wait for each thread to finish
            th.join();
        }

        // we expect 5 threads * 100 responses each
        assertEquals(500, responses.size());

        for (Future<String> f : responses) {
            // they should not be done yet because the counter hasn't incremented
            assertFalse(f.isDone());
        }

        timer.incrementTime(5);
        Future<String> response2 = new TestRequestCollapser(timer, counter, 2).queue();
        timer.incrementTime(8);
        // should execute here
        Future<String> response3 = new TestRequestCollapser(timer, counter, 3).queue();
        timer.incrementTime(6);
        Future<String> response4 = new TestRequestCollapser(timer, counter, 4).queue();
        timer.incrementTime(8);
        // should execute here
        Future<String> response5 = new TestRequestCollapser(timer, counter, 5).queue();
        timer.incrementTime(10);
        // should execute here

        // wait for all tasks to complete
        for (Future<String> f : responses) {
            assertEquals("1", f.get());
        }
        assertEquals("2", response2.get());
        assertEquals("3", response3.get());
        assertEquals("4", response4.get());
        assertEquals("5", response5.get());

        // each task should have been executed 3 times
        for (TestCollapserTimer.ATask t : timer.tasks) {
            assertEquals(3, t.task.count.get());
        }

        // simulate request lifecycle
        requestContext.shutdown();

        HystrixRequestVariableHolder<RequestCollapser<?, ?, ?>> rv = RequestCollapserFactory.getRequestVariable(new TestRequestCollapser(timer, counter, 1).getCollapserKey().name());

        assertNotNull(rv);
        // they should have all been removed as part of ThreadContext.remove()
View Full Code Here

    @Autowired
    private UserService userService;

    @Test
    public void testGetUser() throws NoSuchFieldException, IllegalAccessException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUser("1", "name: ");
            assertEquals("name: 1", u1.getName());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();
            assertEquals("GetUserCommand", command.getCommandKey().name());
            assertEquals("UserGroupKey", command.getCommandGroup().name());
            assertEquals("Test", command.getThreadPoolKey().name());
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
            // assert properties
            assertEquals(110, command.getProperties().executionIsolationThreadTimeoutInMilliseconds().get().intValue());
            assertEquals(false, command.getProperties().executionIsolationThreadInterruptOnTimeout().get());

            Field field = command.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("threadPool");
            field.setAccessible(true);
            HystrixThreadPool threadPool = (HystrixThreadPool) field.get(command);

            Field field2 = HystrixThreadPool.HystrixThreadPoolDefault.class.getDeclaredField("properties");
            field2.setAccessible(true);
            HystrixThreadPoolProperties properties = (HystrixThreadPoolProperties) field2.get(threadPool);

            assertEquals(30, (int) properties.coreSize().get());
            assertEquals(101, (int) properties.maxQueueSize().get());
            assertEquals(2, (int) properties.keepAliveTimeMinutes().get());
            assertEquals(15, (int) properties.queueSizeRejectionThreshold().get());
            assertEquals(1440, (int) properties.metricsRollingStatisticalWindowInMilliseconds().get());
            assertEquals(12, (int) properties.metricsRollingStatisticalWindowBuckets().get());
        } finally {
            context.shutdown();
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testGetUserDefaultPropertiesValues() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            User u1 = userService.getUserDefProperties("1", "name: ");
            assertEquals("name: 1", u1.getName());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
            com.netflix.hystrix.HystrixExecutableInfo<?> command = HystrixRequestLog.getCurrentRequest()
                    .getAllExecutedCommands().iterator().next();
            assertEquals("getUserDefProperties", command.getCommandKey().name());
            assertEquals("UserService", command.getCommandGroup().name());
            assertEquals("UserService", command.getThreadPoolKey().name());
            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.