Search in sources :

Example 21 with TestCollapserTimer

use of com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testRequestThenTwoCacheHitsAllUnsubscribed.

@Test
public void testRequestThenTwoCacheHitsAllUnsubscribed() throws Exception {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new SuccessfulCacheableCollapsedCommand(timer, "foo", true);
    Observable<String> response1 = collapser1.observe();
    HystrixObservableCollapser<String, String, String, String> collapser2 = new SuccessfulCacheableCollapsedCommand(timer, "foo", true);
    Observable<String> response2 = collapser2.observe();
    HystrixObservableCollapser<String, String, String, String> collapser3 = new SuccessfulCacheableCollapsedCommand(timer, "foo", true);
    Observable<String> response3 = collapser3.observe();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = new AtomicReference<String>(null);
    final AtomicReference<String> value3 = new AtomicReference<String>(null);
    Subscription s1 = response1.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : s1 Unsubscribed!");
            latch1.countDown();
        }
    }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : s1 OnCompleted");
            latch1.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : s1 OnError : " + e);
            latch1.countDown();
        }

        @Override
        public void onNext(String s) {
            System.out.println(System.currentTimeMillis() + " : s1 OnNext : " + s);
            value1.set(s);
        }
    });
    Subscription s2 = response2.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : s2 Unsubscribed!");
            latch2.countDown();
        }
    }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : s2 OnCompleted");
            latch2.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : s2 OnError : " + e);
            latch2.countDown();
        }

        @Override
        public void onNext(String s) {
            System.out.println(System.currentTimeMillis() + " : s2 OnNext : " + s);
            value2.set(s);
        }
    });
    Subscription s3 = response3.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : s3 Unsubscribed!");
            latch3.countDown();
        }
    }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : s3 OnCompleted");
            latch3.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : s3 OnError : " + e);
            latch3.countDown();
        }

        @Override
        public void onNext(String s) {
            System.out.println(System.currentTimeMillis() + " : s3 OnNext : " + s);
            value3.set(s);
        }
    });
    s1.unsubscribe();
    s2.unsubscribe();
    s3.unsubscribe();
    // let time pass that equals the default delay/period
    timer.incrementTime(10);
    assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS));
    assertTrue(latch2.await(1000, TimeUnit.MILLISECONDS));
    assertNull(value1.get());
    assertNull(value2.get());
    assertNull(value3.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(0, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
}
Also used : Action0(rx.functions.Action0) TestCollapserTimer(com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Test(org.junit.Test)

Example 22 with TestCollapserTimer

use of com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testTwoRequestsWithValuesForOneArgOnlyAndOnMissingIgnore.

@Test
public void testTwoRequestsWithValuesForOneArgOnlyAndOnMissingIgnore() {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new TestCollapserWithMultipleResponses(timer, 1, 0, onMissingIgnore);
    HystrixObservableCollapser<String, String, String, String> collapser2 = new TestCollapserWithMultipleResponses(timer, 2, 5, onMissingIgnore);
    System.out.println("Starting to observe collapser1");
    Observable<String> result1 = collapser1.observe();
    Observable<String> result2 = collapser2.observe();
    // let time pass that equals the default delay/period
    timer.incrementTime(10);
    TestSubscriber<String> testSubscriber1 = new TestSubscriber<String>();
    result1.subscribe(testSubscriber1);
    TestSubscriber<String> testSubscriber2 = new TestSubscriber<String>();
    result2.subscribe(testSubscriber2);
    testSubscriber1.awaitTerminalEvent();
    testSubscriber2.awaitTerminalEvent();
    testSubscriber1.assertCompleted();
    testSubscriber1.assertNoErrors();
    testSubscriber1.assertNoValues();
    testSubscriber2.assertCompleted();
    testSubscriber2.assertNoErrors();
    testSubscriber2.assertValues("2:2", "2:4", "2:6", "2:8", "2:10");
}
Also used : TestSubscriber(rx.observers.TestSubscriber) TestCollapserTimer(com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer) Test(org.junit.Test)

Example 23 with TestCollapserTimer

use of com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testTwoRequestsWithEmptyResponseAndOnMissingError.

@Test
public void testTwoRequestsWithEmptyResponseAndOnMissingError() {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new TestCollapserWithMultipleResponses(timer, 1, 0, onMissingError);
    HystrixObservableCollapser<String, String, String, String> collapser2 = new TestCollapserWithMultipleResponses(timer, 2, 0, onMissingError);
    System.out.println("Starting to observe collapser1");
    Observable<String> result1 = collapser1.observe();
    Observable<String> result2 = collapser2.observe();
    // let time pass that equals the default delay/period
    timer.incrementTime(10);
    TestSubscriber<String> testSubscriber1 = new TestSubscriber<String>();
    result1.subscribe(testSubscriber1);
    TestSubscriber<String> testSubscriber2 = new TestSubscriber<String>();
    result2.subscribe(testSubscriber2);
    testSubscriber1.awaitTerminalEvent();
    testSubscriber2.awaitTerminalEvent();
    testSubscriber1.assertError(IllegalStateException.class);
    testSubscriber1.assertNoValues();
    testSubscriber2.assertError(IllegalStateException.class);
    testSubscriber2.assertNoValues();
}
Also used : TestSubscriber(rx.observers.TestSubscriber) TestCollapserTimer(com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer) Test(org.junit.Test)

Aggregations

TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)23 Test (org.junit.Test)23 TestSubscriber (rx.observers.TestSubscriber)15 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Subscription (rx.Subscription)7 Action0 (rx.functions.Action0)7 CollapserTimer (com.netflix.hystrix.collapser.CollapserTimer)1 RealCollapserTimer (com.netflix.hystrix.collapser.RealCollapserTimer)1