Search in sources :

Example 61 with Function

use of io.reactivex.functions.Function in project RxJava by ReactiveX.

the class ObservableSwitchTest method switchMapSingleDelayErrorJustSource.

@Test
public void switchMapSingleDelayErrorJustSource() {
    final AtomicBoolean completed = new AtomicBoolean();
    Observable.just(0, 1).switchMapSingleDelayError(new Function<Integer, SingleSource<Integer>>() {

        @Override
        public SingleSource<Integer> apply(Integer v) throws Exception {
            if (v == 0) {
                return Single.error(new RuntimeException());
            } else {
                return Single.just(1).doOnSuccess(new Consumer<Integer>() {

                    @Override
                    public void accept(Integer n) throws Exception {
                        completed.set(true);
                    }
                });
            }
        }
    }).test().assertValue(1).assertError(RuntimeException.class);
    assertTrue(completed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.functions.Function)

Example 62 with Function

use of io.reactivex.functions.Function in project RxJava by ReactiveX.

the class BehaviorProcessorTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    // FIXME was plain null which is not allowed
    BehaviorProcessor<String> src = BehaviorProcessor.createDefault("null");
    for (int i = 0; i < 10; i++) {
        final Subscriber<Object> o = TestHelper.mockSubscriber();
        InOrder inOrder = inOrder(o);
        String v = "" + i;
        src.onNext(v);
        System.out.printf("Turn: %d%n", i);
        src.firstElement().toFlowable().flatMap(new Function<String, Flowable<String>>() {

            @Override
            public Flowable<String> apply(String t1) {
                return Flowable.just(t1 + ", " + t1);
            }
        }).subscribe(new DefaultSubscriber<String>() {

            @Override
            public void onNext(String t) {
                o.onNext(t);
            }

            @Override
            public void onError(Throwable e) {
                o.onError(e);
            }

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        inOrder.verify(o).onNext(v + ", " + v);
        inOrder.verify(o).onComplete();
        verify(o, never()).onError(any(Throwable.class));
    }
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 63 with Function

use of io.reactivex.functions.Function in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method startLoadingData.

/**
     * Starts loading data
     *
     * @param intentUrl Intent URL
     * @param repoSlug  Repository slug
     * @param buildId   Build ID
     */
public void startLoadingData(String intentUrl, String repoSlug, long buildId) {
    mRepoSlug = repoSlug;
    mBuildId = buildId;
    Single<BuildDetails> buildDetailsSingle;
    if (!TextUtils.isEmpty(intentUrl)) {
        buildDetailsSingle = mRawClient.singleRequest(intentUrl).doOnSuccess(response -> {
            String redirectUrl = intentUrl;
            if (response.isRedirect()) {
                redirectUrl = response.header("Location", "");
            }
            parseIntentUrl(redirectUrl);
        }).flatMap(new Function<okhttp3.Response, SingleSource<BuildDetails>>() {

            @Override
            public SingleSource<BuildDetails> apply(@NonNull okhttp3.Response response) throws Exception {
                return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
            }
        });
    } else {
        buildDetailsSingle = mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
    }
    Disposable subscription = buildDetailsSingle.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
    getView().showProgress();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull)

Example 64 with Function

use of io.reactivex.functions.Function in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method cancelBuild.

/**
     * Cancels build process
     */
public void cancelBuild() {
    RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
    Disposable subscription = mTravisRestClient.getApiService().cancelBuild(mBuildId, emptyBody).onErrorReturn(throwable -> new Object()).flatMap(new Function<Object, SingleSource<BuildDetails>>() {

        @Override
        public SingleSource<BuildDetails> apply(@NonNull Object o) throws Exception {
            return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 65 with Function

use of io.reactivex.functions.Function in project Varis-Android by dkhmelenko.

the class BuildsDetailsPresenter method startLoadingData.

/**
 * Starts loading data
 *
 * @param intentUrl Intent URL
 * @param repoSlug  Repository slug
 * @param buildId   Build ID
 */
public void startLoadingData(String intentUrl, String repoSlug, long buildId) {
    mRepoSlug = repoSlug;
    mBuildId = buildId;
    Single<BuildDetails> buildDetailsSingle;
    if (!StringUtils.isEmpty(intentUrl)) {
        buildDetailsSingle = mRawClient.singleRequest(intentUrl).doOnSuccess(response -> {
            String redirectUrl = intentUrl;
            if (response.isRedirect()) {
                redirectUrl = response.header("Location", "");
            }
            parseIntentUrl(redirectUrl);
        }).flatMap(new Function<okhttp3.Response, SingleSource<BuildDetails>>() {

            @Override
            public SingleSource<BuildDetails> apply(@NonNull okhttp3.Response response) throws Exception {
                return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
            }
        });
    } else {
        buildDetailsSingle = mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
    }
    Disposable subscription = buildDetailsSingle.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
        if (throwable == null) {
            handleBuildDetails(buildDetails);
        } else {
            handleLoadingFailed(throwable);
        }
    });
    mSubscriptions.add(subscription);
    getView().showProgress();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull)

Aggregations

Function (io.reactivex.functions.Function)65 Test (org.junit.Test)27 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 Disposable (io.reactivex.disposables.Disposable)12 Observable (io.reactivex.Observable)10 NonNull (io.reactivex.annotations.NonNull)10 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)10 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)6 List (java.util.List)4 RequestBody (okhttp3.RequestBody)4 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)3 Person (io.requery.test.model.Person)3 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 User (com.khmelenko.lab.varis.network.response.User)2 Phone (io.requery.test.model.Phone)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Headers (okhttp3.Headers)2