Search in sources :

Example 56 with Action1

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

the class Example3 method onResume.

@Override
protected void onResume() {
    super.onResume();
    Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            for (int i = 0; i <= 10; i++) {
                subscriber.onNext(Integer.toString(i));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    subscriber.onError(e);
                }
            }
            subscriber.onCompleted();
        }
    }).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnSubscribe()");
        }
    }).doOnNext(new Action1<String>() {

        @Override
        public void call(String s) {
            Log.d(TAG, "doOnNext() " + s);
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnCompleted()");
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnUnSubscribe()");
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.d(TAG, "doOnError() " + throwable.getLocalizedMessage());
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "doOnTerminate()");
        }
    }).finallyDo(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "finallyDo()");
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            mTextView.setText(s);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) Observable(rx.Observable)

Example 57 with Action1

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

the class Example6 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example6);
    mOutputTextView1 = (TextView) findViewById(R.id.example6_textView1);
    mOutputTextView2 = (TextView) findViewById(R.id.example6_textView2);
    mOutputTextView3 = (TextView) findViewById(R.id.example6_textView3);
    createSub = exampleObservable.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            Log.d(TAG, "Called unsubscribe OnDestroy()");
        }
    }).subscribe(new Action1<Integer>() {

        @Override
        public void call(Integer i) {
            mOutputTextView1.setText(Integer.toString(i) + " OnCreate()");
        }
    }, errorHandler);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1)

Example 58 with Action1

use of rx.functions.Action1 in project scissors by lyft.

the class MainActivity method onCropClicked.

@OnClick(R.id.crop_fab)
public void onCropClicked() {
    final File croppedFile = new File(getCacheDir(), "cropped.jpg");
    Observable<Void> onSave = Observable.from(cropView.extensions().crop().quality(100).format(JPEG).into(croppedFile)).subscribeOn(io()).observeOn(mainThread());
    subscriptions.add(onSave.subscribe(new Action1<Void>() {

        @Override
        public void call(Void nothing) {
            CropResultActivity.startUsing(croppedFile, MainActivity.this);
        }
    }));
}
Also used : Action1(rx.functions.Action1) File(java.io.File) OnClick(butterknife.OnClick)

Example 59 with Action1

use of rx.functions.Action1 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 60 with Action1

use of rx.functions.Action1 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)

Aggregations

Action1 (rx.functions.Action1)106 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 OnSubscribe (rx.Observable.OnSubscribe)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5