Search in sources :

Example 61 with Action0

use of rx.functions.Action0 in project AnDevCon-RxPatterns by colintheshots.

the class Example16Test method test_anomalous_network_event.

@Test
public void test_anomalous_network_event() {
    // TestScheduler lets you advance time by hand
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();
    // Scheduler.Worker lets you schedule events in time
    Scheduler.Worker worker = scheduler.createWorker();
    // Subjects allow both input and output, so they can be swapped in for
    // Retrofit calls to unit test your code.
    final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();
    // schedule a first observable event to occur at 1000 ms
    worker.schedule(new Action0() {

        @Override
        public void call() {
            networkSubject.onError(new TimeoutException());
        }
    }, 10000, TimeUnit.MILLISECONDS);
    // subscribing so events appear
    networkSubject.subscribeOn(scheduler).subscribe(subscriber);
    scheduler.advanceTimeBy(20000, TimeUnit.MILLISECONDS);
    subscriber.awaitTerminalEvent();
    // we use the class-based assertError method, since it's easier to match
    subscriber.assertError(TimeoutException.class);
    subscriber.assertValueCount(0);
    subscriber.assertUnsubscribed();
}
Also used : Action0(rx.functions.Action0) TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Override(java.lang.Override) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 62 with Action0

use of rx.functions.Action0 in project nucleus by konmik.

the class DeliverReplay method call.

@Override
public Observable<Delivery<View, T>> call(Observable<T> observable) {
    final ReplaySubject<Notification<T>> subject = ReplaySubject.create();
    final Subscription subscription = observable.materialize().filter(new Func1<Notification<T>, Boolean>() {

        @Override
        public Boolean call(Notification<T> notification) {
            return !notification.isOnCompleted();
        }
    }).subscribe(subject);
    return view.switchMap(new Func1<View, Observable<Delivery<View, T>>>() {

        @Override
        public Observable<Delivery<View, T>> call(final View view) {
            return view == null ? Observable.<Delivery<View, T>>never() : subject.map(new Func1<Notification<T>, Delivery<View, T>>() {

                @Override
                public Delivery<View, T> call(Notification<T> notification) {
                    return new Delivery<>(view, notification);
                }
            });
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            subscription.unsubscribe();
        }
    });
}
Also used : Action0(rx.functions.Action0) Subscription(rx.Subscription) Func1(rx.functions.Func1) Notification(rx.Notification)

Example 63 with Action0

use of rx.functions.Action0 in project pinpoint by naver.

the class ConnectableObservableTestRunner method connectableObservable.

public void connectableObservable() throws Exception {
    final int numSubscribers = 2;
    final List<String> messages = Arrays.asList("Hello", "World");
    final List<String> actualMessages = Collections.synchronizedList(new ArrayList<String>());
    final CountDownLatch completeLatch = new CountDownLatch(numSubscribers);
    final Action1<String> onNext = new Action1<String>() {

        @Override
        public void call(String s) {
            actualMessages.add(s);
        }
    };
    final Action1<Throwable> onError = new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            completeLatch.countDown();
        }
    };
    final Action0 onCompleted = new Action0() {

        @Override
        public void call() {
            completeLatch.countDown();
        }
    };
    ConnectableObservable<String> echoes = echoesService.echo(messages).subscribeOn(Schedulers.computation()).publish();
    for (int i = 0; i < numSubscribers; i++) {
        echoes.subscribe(onNext, onError, onCompleted);
    }
    echoes.connect();
    completeLatch.await(500L, TimeUnit.MILLISECONDS);
    for (int i = 0; i < actualMessages.size(); i++) {
        String expectedMessage = messages.get(i / numSubscribers);
        String actualMessage = actualMessages.get(i);
        Assert.assertEquals(expectedMessage, actualMessage);
    }
    TestHelper.awaitForSpanDataFlush();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Skip rx java internal traces as they differ between versions and it's too much work to split the tests.
    // Instead, we can verify them indirectly by checking if user methods have been traced.
    verifier.ignoreServiceType("RX_JAVA_INTERNAL");
    Method publishMethod = Observable.class.getDeclaredMethod("publish");
    verifier.verifyTrace(event("RX_JAVA", publishMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) X numSubscribers
    Method subscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class, Action1.class, Action0.class);
    for (int i = 0; i < numSubscribers; i++) {
        verifier.verifyTrace(event("RX_JAVA", subscribeMethod));
    }
    Method connectMethod = ConnectableObservable.class.getDeclaredMethod("connect");
    verifier.verifyTrace(event("RX_JAVA", connectMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) for scheduling a single connectable observable
    verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"));
    // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled
    Method echoMethod = EchoRepository.class.getDeclaredMethod("echo", String.class);
    for (int i = 0; i < numSubscribers; i++) {
        verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), echoMethod));
    }
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)

Example 64 with Action0

use of rx.functions.Action0 in project pinpoint by naver.

the class ConnectableObservableTestRunner method connectableObservableError.

public void connectableObservableError() throws Exception {
    final int numSubscribers = 3;
    final List<String> messages = Arrays.asList("Hello", "World");
    final Exception expected = new RuntimeException("expected");
    final CountDownLatch completeLatch = new CountDownLatch(numSubscribers);
    final List<Exception> actualExceptions = Collections.synchronizedList(new ArrayList<Exception>());
    final Action1<String> onNext = new Action1<String>() {

        @Override
        public void call(String s) {
        // ignore
        }
    };
    final Action1<Throwable> onError = new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            actualExceptions.add((Exception) throwable);
            completeLatch.countDown();
        }
    };
    final Action0 onCompleted = new Action0() {

        @Override
        public void call() {
            completeLatch.countDown();
        }
    };
    ConnectableObservable<String> echoes = echoesService.echo(messages, expected).subscribeOn(Schedulers.computation()).publish();
    for (int i = 0; i < numSubscribers; i++) {
        echoes.subscribe(onNext, onError, onCompleted);
    }
    echoes.connect();
    completeLatch.await(500L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(numSubscribers, actualExceptions.size());
    for (Exception actualException : actualExceptions) {
        Assert.assertSame(expected, actualException);
    }
    TestHelper.awaitForSpanDataFlush();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Skip rx java internal traces as they differ between versions and it's too much work to split the tests.
    // Instead, we can verify them indirectly by checking if user methods have been traced.
    verifier.ignoreServiceType("RX_JAVA_INTERNAL");
    Method publishMethod = Observable.class.getDeclaredMethod("publish");
    verifier.verifyTrace(event("RX_JAVA", publishMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) X numSubscribers
    Method subscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class, Action1.class, Action0.class);
    for (int i = 0; i < numSubscribers; i++) {
        verifier.verifyTrace(event("RX_JAVA", subscribeMethod));
    }
    Method connectMethod = ConnectableObservable.class.getDeclaredMethod("connect");
    verifier.verifyTrace(event("RX_JAVA", connectMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) for scheduling a single connectable observable
    verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"));
    // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled
    Method echoMethod = EchoRepository.class.getDeclaredMethod("echo", String.class, Exception.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), echoMethod, expected));
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)

Example 65 with Action0

use of rx.functions.Action0 in project pinpoint by naver.

the class CompletableTestRunner method completableError.

public void completableError() throws Exception {
    final String message = "Hello World";
    final Exception expected = new RuntimeException("expected");
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Exception> actual = new AtomicReference<>();
    Subscription subscription = shoutService.shout(message, expected).subscribeOn(Schedulers.computation()).doOnCompleted(new Action0() {

        @Override
        public void call() {
            latch.countDown();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            actual.set((Exception) throwable);
            latch.countDown();
        }
    }).subscribe();
    latch.await(500L, TimeUnit.MILLISECONDS);
    subscription.unsubscribe();
    Assert.assertSame(expected, actual.get());
    TestHelper.awaitForSpanDataFlush();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Skip rx java internal traces as they differ between versions and it's too much work to split the tests.
    // Instead, we can verify them indirectly by checking if user methods have been traced.
    verifier.ignoreServiceType("RX_JAVA_INTERNAL");
    verifier.awaitTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"), 20, 500);
    Method subscribeMethod = Completable.class.getDeclaredMethod("subscribe");
    verifier.verifyTrace(event("RX_JAVA", subscribeMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0)
    verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"));
    // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled
    Method shoutMethod = EchoRepository.class.getDeclaredMethod("shout", String.class, Exception.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), shoutMethod, expected));
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Subscription(rx.Subscription)

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