Search in sources :

Example 36 with Action0

use of rx.functions.Action0 in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testRequestThenCacheHitAndOriginalUnsubscribed.

@Test
public void testRequestThenCacheHitAndOriginalUnsubscribed() 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();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = 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);
        }
    });
    s1.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());
    assertEquals("foo", value2.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
    assertCommandExecutionEvents(command, HystrixEventType.EMIT, HystrixEventType.SUCCESS, HystrixEventType.COLLAPSED);
    // should only be 1 collapsed - other came from cache, then was cancelled
    assertEquals(1, command.getNumberCollapsed());
}
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 37 with Action0

use of rx.functions.Action0 in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testEarlyUnsubscribeFromAllCancelsBatch.

@Test
public void testEarlyUnsubscribeFromAllCancelsBatch() throws Exception {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new TestRequestCollapser(timer, 1);
    Observable<String> response1 = collapser1.observe();
    HystrixObservableCollapser<String, String, String, String> collapser2 = new TestRequestCollapser(timer, 2);
    Observable<String> response2 = collapser2.observe();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = 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);
        }
    });
    s1.unsubscribe();
    s2.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());
    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 38 with Action0

use of rx.functions.Action0 in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testEarlyUnsubscribeExecutedViaToObservable.

@Test
public void testEarlyUnsubscribeExecutedViaToObservable() throws Exception {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixObservableCollapser<String, String, String, String> collapser1 = new TestRequestCollapser(timer, 1);
    Observable<String> response1 = collapser1.toObservable();
    HystrixObservableCollapser<String, String, String, String> collapser2 = new TestRequestCollapser(timer, 2);
    Observable<String> response2 = collapser2.toObservable();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = 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);
        }
    });
    s1.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());
    assertEquals("2", value2.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    HystrixCollapserMetrics metrics = collapser1.getMetrics();
    assertSame(metrics, collapser2.getMetrics());
    HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
    // 1 should have been removed from batch
    assertEquals(1, command.getNumberCollapsed());
}
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 39 with Action0

use of rx.functions.Action0 in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testRequestThenCacheHitAndCacheHitUnsubscribed.

@Test
public void testRequestThenCacheHitAndCacheHitUnsubscribed() 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();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = 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);
        }
    });
    s2.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));
    assertEquals("foo", value1.get());
    assertNull(value2.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
    assertCommandExecutionEvents(command, HystrixEventType.EMIT, HystrixEventType.SUCCESS, HystrixEventType.COLLAPSED);
    // should only be 1 collapsed - other came from cache, then was cancelled
    assertEquals(1, command.getNumberCollapsed());
}
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 40 with Action0

use of rx.functions.Action0 in project Hystrix by Netflix.

the class HystrixObservableCollapserTest method testRequestThenTwoCacheHitsOriginalAndOneCacheHitUnsubscribed.

@Test
public void testRequestThenTwoCacheHitsOriginalAndOneCacheHitUnsubscribed() 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();
    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());
    assertEquals("foo", value2.get());
    assertNull(value3.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
    assertCommandExecutionEvents(command, HystrixEventType.EMIT, HystrixEventType.SUCCESS, HystrixEventType.COLLAPSED);
    // should only be 1 collapsed - other came from cache, then was cancelled
    assertEquals(1, command.getNumberCollapsed());
}
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)

Aggregations

Action0 (rx.functions.Action0)134 Subscription (rx.Subscription)58 Test (org.junit.Test)56 CountDownLatch (java.util.concurrent.CountDownLatch)50 Action1 (rx.functions.Action1)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 List (java.util.List)15 Func1 (rx.functions.Func1)13 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)12 Observable (rx.Observable)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 OnClick (butterknife.OnClick)10 IOException (java.io.IOException)9 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)8 UiThreadTest (android.support.test.annotation.UiThreadTest)7 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)7 TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)7 Method (java.lang.reflect.Method)7