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());
}
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));
}
}
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();
}
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);
}
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();
}
Aggregations