Search in sources :

Example 1 with CollapserTimer

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

the class HystrixObservableCollapserTest method testTwoRequestsWhichShouldEachEmitTwice.

@Test
public void testTwoRequestsWhichShouldEachEmitTwice() throws Exception {
    // TestCollapserTimer timer = new TestCollapserTimer();
    CollapserTimer timer = new RealCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new TestCollapserWithMultipleResponses(timer, 1, 3, false, false, prefixMapper, onMissingComplete);
    HystrixObservableCollapser<String, String, String, String> collapser2 = new TestCollapserWithMultipleResponses(timer, 2, 3, false, false, prefixMapper, onMissingComplete);
    TestSubscriber<String> testSubscriber1 = new TestSubscriber<String>();
    TestSubscriber<String> testSubscriber2 = new TestSubscriber<String>();
    System.out.println(System.currentTimeMillis() + "Starting to observe collapser1");
    collapser1.observe().subscribe(testSubscriber1);
    collapser2.observe().subscribe(testSubscriber2);
    System.out.println(System.currentTimeMillis() + "Done with collapser observe()s");
    // Note that removing these awaits breaks the unit test.  That implies that the above subscribe does not wait for a terminal event
    testSubscriber1.awaitTerminalEvent();
    testSubscriber2.awaitTerminalEvent();
    testSubscriber1.assertCompleted();
    testSubscriber1.assertNoErrors();
    testSubscriber1.assertValues("1:1", "1:2", "1:3");
    testSubscriber2.assertCompleted();
    testSubscriber2.assertNoErrors();
    testSubscriber2.assertValues("2:2", "2:4", "2:6");
}
Also used : RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) CollapserTimer(com.netflix.hystrix.collapser.CollapserTimer) TestCollapserTimer(com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer) RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 2 with CollapserTimer

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

the class HystrixObservableCollapserTest method testCollapserUnderConcurrency.

@Test
public void testCollapserUnderConcurrency() throws InterruptedException {
    final CollapserTimer timer = new RealCollapserTimer();
    final int NUM_THREADS_SUBMITTING_WORK = 8;
    final int NUM_REQUESTS_PER_THREAD = 8;
    final CountDownLatch latch = new CountDownLatch(NUM_THREADS_SUBMITTING_WORK);
    List<Runnable> runnables = new ArrayList<Runnable>();
    final ConcurrentLinkedQueue<TestSubscriber<String>> subscribers = new ConcurrentLinkedQueue<TestSubscriber<String>>();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    final AtomicInteger uniqueInt = new AtomicInteger(0);
    for (int i = 0; i < NUM_THREADS_SUBMITTING_WORK; i++) {
        runnables.add(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int j = 0; j < NUM_REQUESTS_PER_THREAD; j++) {
                        HystrixObservableCollapser<String, String, String, String> collapser = new TestCollapserWithMultipleResponses(timer, uniqueInt.getAndIncrement(), 3, false);
                        Observable<String> o = collapser.toObservable();
                        TestSubscriber<String> subscriber = new TestSubscriber<String>();
                        o.subscribe(subscriber);
                        subscribers.offer(subscriber);
                    }
                // System.out.println("Runnable done on thread : " + Thread.currentThread().getName());
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    for (Runnable r : runnables) {
        threadPool.submit(new HystrixContextRunnable(r));
    }
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    for (TestSubscriber<String> subscriber : subscribers) {
        subscriber.awaitTerminalEvent();
        if (subscriber.getOnErrorEvents().size() > 0) {
            System.out.println("ERROR : " + subscriber.getOnErrorEvents());
            for (Throwable ex : subscriber.getOnErrorEvents()) {
                ex.printStackTrace();
            }
        }
        subscriber.assertCompleted();
        subscriber.assertNoErrors();
        System.out.println("Received : " + subscriber.getOnNextEvents());
        subscriber.assertValueCount(3);
    }
    context.shutdown();
}
Also used : RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) CollapserTimer(com.netflix.hystrix.collapser.CollapserTimer) TestCollapserTimer(com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer) RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(rx.Observable) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) TestSubscriber(rx.observers.TestSubscriber) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 3 with CollapserTimer

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

the class HystrixCollapserTest method testVoidResponseTypeFireAndForgetCollapsing3.

/**
 * Test a Void response type with execute - response being set in mapResponseToRequest to null
 *
 * @throws Exception
 */
@Test
public void testVoidResponseTypeFireAndForgetCollapsing3() throws Exception {
    CollapserTimer timer = new RealCollapserTimer();
    TestCollapserWithVoidResponseType collapser1 = new TestCollapserWithVoidResponseType(timer, 1);
    assertNull(collapser1.execute());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    Iterator<HystrixInvokableInfo<?>> cmdIterator = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator();
    assertEquals(1, cmdIterator.next().getNumberCollapsed());
}
Also used : RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) CollapserTimer(com.netflix.hystrix.collapser.CollapserTimer) RealCollapserTimer(com.netflix.hystrix.collapser.RealCollapserTimer) Test(org.junit.Test)

Aggregations

CollapserTimer (com.netflix.hystrix.collapser.CollapserTimer)3 RealCollapserTimer (com.netflix.hystrix.collapser.RealCollapserTimer)3 Test (org.junit.Test)3 TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)2 TestSubscriber (rx.observers.TestSubscriber)2 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)1 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)1 ArrayList (java.util.ArrayList)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Observable (rx.Observable)1