Search in sources :

Example 1 with RequestCollapser

use of com.netflix.hystrix.collapser.RequestCollapser in project Hystrix by Netflix.

the class HystrixCollapserTest method testRequestVariableLifecycle2.

@Test
public void testRequestVariableLifecycle2() throws Exception {
    final HystrixRequestContext reqContext = 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++) {
        final int outerLoop = t;
        Thread th = new Thread(new HystrixContextRunnable(HystrixPlugins.getInstance().getConcurrencyStrategy(), new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    int uniqueInt = (outerLoop * 100) + i;
                    responses.add(new TestRequestCollapser(timer, uniqueInt).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);
    HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 2);
    Future<String> response2 = collapser1.queue();
    timer.incrementTime(8);
    // should execute here
    Future<String> response3 = new TestRequestCollapser(timer, 3).queue();
    timer.incrementTime(6);
    Future<String> response4 = new TestRequestCollapser(timer, 4).queue();
    timer.incrementTime(8);
    // should execute here
    Future<String> response5 = new TestRequestCollapser(timer, 5).queue();
    timer.incrementTime(10);
    // wait for all tasks to complete
    for (Future<String> f : responses) {
        f.get(1000, TimeUnit.MILLISECONDS);
    }
    assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("4", response4.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("5", response5.get(1000, TimeUnit.MILLISECONDS));
    // each task should have been executed 3 times
    for (TestCollapserTimer.ATask t : timer.tasks) {
        assertEquals(3, t.task.count.get());
    }
    Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator();
    assertEquals(500, cmdIterator.next().getNumberCollapsed());
    assertEquals(2, cmdIterator.next().getNumberCollapsed());
    assertEquals(1, cmdIterator.next().getNumberCollapsed());
    HystrixRequestVariableHolder<RequestCollapser<?, ?, ?>> rv = RequestCollapserFactory.getRequestVariable(new TestRequestCollapser(timer, 1).getCollapserKey().name());
    reqContext.close();
    assertNotNull(rv);
    // they should have all been removed as part of ThreadContext.remove()
    assertEquals(0, timer.tasks.size());
}
Also used : HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) Future(java.util.concurrent.Future) RequestCollapser(com.netflix.hystrix.collapser.RequestCollapser) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 2 with RequestCollapser

use of com.netflix.hystrix.collapser.RequestCollapser in project Hystrix by Netflix.

the class HystrixCollapserTest method testRequestVariableLifecycle1.

@Test
public void testRequestVariableLifecycle1() throws Exception {
    HystrixRequestContext reqContext = HystrixRequestContext.initializeContext();
    // do actual work
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1);
    Future<String> response1 = collapser1.queue();
    timer.incrementTime(5);
    Future<String> response2 = new TestRequestCollapser(timer, 2).queue();
    timer.incrementTime(8);
    // should execute here
    Future<String> response3 = new TestRequestCollapser(timer, 3).queue();
    timer.incrementTime(6);
    Future<String> response4 = new TestRequestCollapser(timer, 4).queue();
    timer.incrementTime(8);
    // should execute here
    Future<String> response5 = new TestRequestCollapser(timer, 5).queue();
    timer.incrementTime(10);
    // should execute here
    // wait for all tasks to complete
    assertEquals("1", response1.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("2", response2.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("3", response3.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("4", response4.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("5", response5.get(1000, TimeUnit.MILLISECONDS));
    // 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);
    Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator();
    assertEquals(2, cmdIterator.next().getNumberCollapsed());
    assertEquals(2, cmdIterator.next().getNumberCollapsed());
    assertEquals(1, cmdIterator.next().getNumberCollapsed());
    System.out.println("timer.tasks.size() B: " + timer.tasks.size());
    HystrixRequestVariableHolder<RequestCollapser<?, ?, ?>> rv = RequestCollapserFactory.getRequestVariable(new TestRequestCollapser(timer, 1).getCollapserKey().name());
    reqContext.close();
    assertNotNull(rv);
    // they should have all been removed as part of ThreadContext.remove()
    assertEquals(0, timer.tasks.size());
}
Also used : RequestCollapser(com.netflix.hystrix.collapser.RequestCollapser) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

RequestCollapser (com.netflix.hystrix.collapser.RequestCollapser)2 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Future (java.util.concurrent.Future)1