Search in sources :

Example 46 with Flowable

use of io.reactivex.Flowable in project brave by openzipkin.

the class CurrentTraceContextAssemblyTrackingMatrixTest method connectableFlowable_assembleInScope_subscribeNoScope.

@Test
public void connectableFlowable_assembleInScope_subscribeNoScope() {
    ConnectableFlowable<Integer> source, errorSource;
    try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
        source = Flowable.range(1, 3).doOnNext(e -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
        errorSource = Flowable.<Integer>error(new IllegalStateException()).doOnError(t -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
    }
    subscribeInNoContext(source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()).assertResult(1, 2, 3);
}
Also used : MaybeObserver(io.reactivex.MaybeObserver) ScalarCallable(io.reactivex.internal.fuseable.ScalarCallable) ObservableRange(io.reactivex.internal.operators.observable.ObservableRange) ParallelFilter(io.reactivex.internal.operators.parallel.ParallelFilter) FlowableFromCallable(io.reactivex.internal.operators.flowable.FlowableFromCallable) Completable(io.reactivex.Completable) FlowableFilter(io.reactivex.internal.operators.flowable.FlowableFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Maybe(io.reactivex.Maybe) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) Callable(java.util.concurrent.Callable) CurrentTraceContext(brave.propagation.CurrentTraceContext) SingleFromCallable(io.reactivex.internal.operators.single.SingleFromCallable) Single(io.reactivex.Single) SingleJust(io.reactivex.internal.operators.single.SingleJust) MaybeFromCallable(io.reactivex.internal.operators.maybe.MaybeFromCallable) ParallelFlowable(io.reactivex.parallel.ParallelFlowable) Flowable(io.reactivex.Flowable) CompletableFromCallable(io.reactivex.internal.operators.completable.CompletableFromCallable) After(org.junit.After) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) MaybeFilterSingle(io.reactivex.internal.operators.maybe.MaybeFilterSingle) Before(org.junit.Before) ObservablePublish(io.reactivex.internal.operators.observable.ObservablePublish) ConnectableObservable(io.reactivex.observables.ConnectableObservable) CompletableObserver(io.reactivex.CompletableObserver) ParallelFromPublisher(io.reactivex.internal.operators.parallel.ParallelFromPublisher) CompletableEmpty(io.reactivex.internal.operators.completable.CompletableEmpty) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Predicate(io.reactivex.functions.Predicate) TraceContext(brave.propagation.TraceContext) MaybeFilter(io.reactivex.internal.operators.maybe.MaybeFilter) FlowablePublish(io.reactivex.internal.operators.flowable.FlowablePublish) ObservableFromCallable(io.reactivex.internal.operators.observable.ObservableFromCallable) SingleObserver(io.reactivex.SingleObserver) FlowableRange(io.reactivex.internal.operators.flowable.FlowableRange) MaybeJust(io.reactivex.internal.operators.maybe.MaybeJust) Observer(io.reactivex.Observer) Scope(brave.propagation.CurrentTraceContext.Scope) ObservableFilter(io.reactivex.internal.operators.observable.ObservableFilter) Scope(brave.propagation.CurrentTraceContext.Scope) Test(org.junit.Test)

Example 47 with Flowable

use of io.reactivex.Flowable in project brave by openzipkin.

the class CurrentTraceContextAssemblyTrackingMatrixTest method connectableFlowable_assembleInScope_subscribeInScope.

@Test
public void connectableFlowable_assembleInScope_subscribeInScope() {
    ConnectableFlowable<Integer> source, errorSource;
    try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
        source = Flowable.range(1, 3).doOnNext(e -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
        errorSource = Flowable.<Integer>error(new IllegalStateException()).doOnError(t -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
    }
    subscribeInDifferentContext(source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()).assertResult(1, 2, 3);
}
Also used : MaybeObserver(io.reactivex.MaybeObserver) ScalarCallable(io.reactivex.internal.fuseable.ScalarCallable) ObservableRange(io.reactivex.internal.operators.observable.ObservableRange) ParallelFilter(io.reactivex.internal.operators.parallel.ParallelFilter) FlowableFromCallable(io.reactivex.internal.operators.flowable.FlowableFromCallable) Completable(io.reactivex.Completable) FlowableFilter(io.reactivex.internal.operators.flowable.FlowableFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Maybe(io.reactivex.Maybe) ConnectableFlowable(io.reactivex.flowables.ConnectableFlowable) StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) Callable(java.util.concurrent.Callable) CurrentTraceContext(brave.propagation.CurrentTraceContext) SingleFromCallable(io.reactivex.internal.operators.single.SingleFromCallable) Single(io.reactivex.Single) SingleJust(io.reactivex.internal.operators.single.SingleJust) MaybeFromCallable(io.reactivex.internal.operators.maybe.MaybeFromCallable) ParallelFlowable(io.reactivex.parallel.ParallelFlowable) Flowable(io.reactivex.Flowable) CompletableFromCallable(io.reactivex.internal.operators.completable.CompletableFromCallable) After(org.junit.After) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) MaybeFilterSingle(io.reactivex.internal.operators.maybe.MaybeFilterSingle) Before(org.junit.Before) ObservablePublish(io.reactivex.internal.operators.observable.ObservablePublish) ConnectableObservable(io.reactivex.observables.ConnectableObservable) CompletableObserver(io.reactivex.CompletableObserver) ParallelFromPublisher(io.reactivex.internal.operators.parallel.ParallelFromPublisher) CompletableEmpty(io.reactivex.internal.operators.completable.CompletableEmpty) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Predicate(io.reactivex.functions.Predicate) TraceContext(brave.propagation.TraceContext) MaybeFilter(io.reactivex.internal.operators.maybe.MaybeFilter) FlowablePublish(io.reactivex.internal.operators.flowable.FlowablePublish) ObservableFromCallable(io.reactivex.internal.operators.observable.ObservableFromCallable) SingleObserver(io.reactivex.SingleObserver) FlowableRange(io.reactivex.internal.operators.flowable.FlowableRange) MaybeJust(io.reactivex.internal.operators.maybe.MaybeJust) Observer(io.reactivex.Observer) Scope(brave.propagation.CurrentTraceContext.Scope) ObservableFilter(io.reactivex.internal.operators.observable.ObservableFilter) Scope(brave.propagation.CurrentTraceContext.Scope) Test(org.junit.Test)

Example 48 with Flowable

use of io.reactivex.Flowable in project BaseProject by wareine.

the class Transformers method transformers.

// public static <T> FlowableTransformer<ResponseBody,ApiResponse<T>> transformers(){
// return new FlowableTransformer<ResponseBody, ApiResponse<T>>() {
// @Override
// public Publisher<ApiResponse<T>> apply(Flowable<ResponseBody> upstream) {
// return upstream.map(new Function<ResponseBody, ApiResponse<T>>() {
// @Override
// public ApiResponse<T> apply(ResponseBody responseBody) throws Exception {
// String response = responseBody.string();
// Gson gson = new Gson();
// Type objectType = new TypeToken<ApiResponse<T>>() {}.getType();
// return gson.fromJson(response, objectType);
// }
// }).onErrorResumeNext(new Function<Throwable, Publisher<? extends ApiResponse<T>>>() {
// @Override
// public Publisher<? extends ApiResponse<T>> apply(Throwable throwable) throws Exception {
// return Flowable.error(ExceptionHandle.handleException(throwable));
// }
// });
// }
// };
// }
public static <T> FlowableTransformer<ResponseBody, T> transformers(final Class<T> cls) {
    return new FlowableTransformer<ResponseBody, T>() {

        @Override
        public Publisher<T> apply(Flowable<ResponseBody> upstream) {
            return upstream.map(new Function<ResponseBody, T>() {

                @Override
                public T apply(ResponseBody responseBody) throws Exception {
                    String response = responseBody.string();
                    Gson gson = new Gson();
                    return gson.fromJson(response, cls);
                }
            }).onErrorResumeNext(new Function<Throwable, Publisher<? extends T>>() {

                @Override
                public Publisher<? extends T> apply(Throwable throwable) throws Exception {
                    return Flowable.error(ExceptionHandle.handleException(throwable));
                }
            });
        }
    };
}
Also used : Function(io.reactivex.functions.Function) FlowableTransformer(io.reactivex.FlowableTransformer) Gson(com.google.gson.Gson) Publisher(org.reactivestreams.Publisher) Flowable(io.reactivex.Flowable) ResponseBody(okhttp3.ResponseBody)

Example 49 with Flowable

use of io.reactivex.Flowable in project NewPipe by TeamNewPipe.

the class SearchFragment method initSuggestionObserver.

private void initSuggestionObserver() {
    if (DEBUG)
        Log.d(TAG, "initSuggestionObserver() called");
    if (suggestionDisposable != null)
        suggestionDisposable.dispose();
    final Observable<String> observable = suggestionPublisher.debounce(SUGGESTIONS_DEBOUNCE, TimeUnit.MILLISECONDS).startWith(searchQuery != null ? searchQuery : "").filter(query -> isSuggestionsEnabled);
    suggestionDisposable = observable.switchMap(query -> {
        final Flowable<List<SearchHistoryEntry>> flowable = historyRecordManager.getRelatedSearches(query, 3, 25);
        final Observable<List<SuggestionItem>> local = flowable.toObservable().map(searchHistoryEntries -> {
            List<SuggestionItem> result = new ArrayList<>();
            for (SearchHistoryEntry entry : searchHistoryEntries) result.add(new SuggestionItem(true, entry.getSearch()));
            return result;
        });
        if (query.length() < THRESHOLD_NETWORK_SUGGESTION) {
            // Only pass through if the query length is equal or greater than THRESHOLD_NETWORK_SUGGESTION
            return local.materialize();
        }
        final Observable<List<SuggestionItem>> network = ExtractorHelper.suggestionsFor(serviceId, query, contentCountry).toObservable().map(strings -> {
            List<SuggestionItem> result = new ArrayList<>();
            for (String entry : strings) {
                result.add(new SuggestionItem(false, entry));
            }
            return result;
        });
        return Observable.zip(local, network, (localResult, networkResult) -> {
            List<SuggestionItem> result = new ArrayList<>();
            if (localResult.size() > 0)
                result.addAll(localResult);
            // Remove duplicates
            final Iterator<SuggestionItem> iterator = networkResult.iterator();
            while (iterator.hasNext() && localResult.size() > 0) {
                final SuggestionItem next = iterator.next();
                for (SuggestionItem item : localResult) {
                    if (item.query.equals(next.query)) {
                        iterator.remove();
                        break;
                    }
                }
            }
            if (networkResult.size() > 0)
                result.addAll(networkResult);
            return result;
        }).materialize();
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(listNotification -> {
        if (listNotification.isOnNext()) {
            handleSuggestions(listNotification.getValue());
        } else if (listNotification.isOnError()) {
            Throwable error = listNotification.getError();
            if (!ExtractorHelper.hasAssignableCauseThrowable(error, IOException.class, SocketException.class, InterruptedException.class, InterruptedIOException.class)) {
                onSuggestionError(error);
            }
        }
    });
}
Also used : Bundle(android.os.Bundle) ReCaptchaActivity(org.schabi.newpipe.ReCaptchaActivity) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) ExtractorHelper(org.schabi.newpipe.util.ExtractorHelper) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) SearchResult(org.schabi.newpipe.extractor.search.SearchResult) PreferenceManager(android.preference.PreferenceManager) AnimationUtils.animateView(org.schabi.newpipe.util.AnimationUtils.animateView) R(org.schabi.newpipe.R) ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Log(android.util.Log) BaseListFragment(org.schabi.newpipe.fragments.list.BaseListFragment) UserAction(org.schabi.newpipe.report.UserAction) ViewGroup(android.view.ViewGroup) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) Constants(org.schabi.newpipe.util.Constants) PublishSubject(io.reactivex.subjects.PublishSubject) BackPressable(org.schabi.newpipe.fragments.BackPressable) Queue(java.util.Queue) Nullable(android.support.annotation.Nullable) EditorInfo(android.view.inputmethod.EditorInfo) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) ListExtractor(org.schabi.newpipe.extractor.ListExtractor) StreamingService(org.schabi.newpipe.extractor.StreamingService) NewPipe(org.schabi.newpipe.extractor.NewPipe) NavigationHelper(org.schabi.newpipe.util.NavigationHelper) Intent(android.content.Intent) AnimationUtils(org.schabi.newpipe.util.AnimationUtils) Callable(java.util.concurrent.Callable) NonNull(android.support.annotation.NonNull) InterruptedIOException(java.io.InterruptedIOException) Editable(android.text.Editable) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) ArrayList(java.util.ArrayList) SearchEngine(org.schabi.newpipe.extractor.search.SearchEngine) SocketException(java.net.SocketException) InfoItem(org.schabi.newpipe.extractor.InfoItem) Flowable(io.reactivex.Flowable) HistoryRecordManager(org.schabi.newpipe.history.HistoryRecordManager) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) LayoutManagerSmoothScroller(org.schabi.newpipe.util.LayoutManagerSmoothScroller) Observable(io.reactivex.Observable) State(icepick.State) ActionBar(android.support.v7.app.ActionBar) Iterator(java.util.Iterator) LayoutInflater(android.view.LayoutInflater) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Consumer(io.reactivex.functions.Consumer) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) AlertDialog(android.support.v7.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) TooltipCompat(android.support.v7.widget.TooltipCompat) Activity(android.app.Activity) EditText(android.widget.EditText) SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) ArrayList(java.util.ArrayList) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SearchHistoryEntry(org.schabi.newpipe.database.history.model.SearchHistoryEntry) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 50 with Flowable

use of io.reactivex.Flowable in project retrofit by square.

the class RxJava2CallAdapterFactoryTest method responseTypes.

@Test
public void responseTypes() {
    Type oBodyClass = new TypeToken<Observable<String>>() {
    }.getType();
    assertThat(factory.get(oBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sBodyClass = new TypeToken<Single<String>>() {
    }.getType();
    assertThat(factory.get(sBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mBodyClass = new TypeToken<Maybe<String>>() {
    }.getType();
    assertThat(factory.get(mBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fBodyClass = new TypeToken<Flowable<String>>() {
    }.getType();
    assertThat(factory.get(fBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oBodyWildcard = new TypeToken<Observable<? extends String>>() {
    }.getType();
    assertThat(factory.get(oBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sBodyWildcard = new TypeToken<Single<? extends String>>() {
    }.getType();
    assertThat(factory.get(sBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mBodyWildcard = new TypeToken<Maybe<? extends String>>() {
    }.getType();
    assertThat(factory.get(mBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fBodyWildcard = new TypeToken<Flowable<? extends String>>() {
    }.getType();
    assertThat(factory.get(fBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oBodyGeneric = new TypeToken<Observable<List<String>>>() {
    }.getType();
    assertThat(factory.get(oBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type sBodyGeneric = new TypeToken<Single<List<String>>>() {
    }.getType();
    assertThat(factory.get(sBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type mBodyGeneric = new TypeToken<Maybe<List<String>>>() {
    }.getType();
    assertThat(factory.get(mBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type fBodyGeneric = new TypeToken<Flowable<List<String>>>() {
    }.getType();
    assertThat(factory.get(fBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type oResponseClass = new TypeToken<Observable<Response<String>>>() {
    }.getType();
    assertThat(factory.get(oResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResponseClass = new TypeToken<Single<Response<String>>>() {
    }.getType();
    assertThat(factory.get(sResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResponseClass = new TypeToken<Maybe<Response<String>>>() {
    }.getType();
    assertThat(factory.get(mResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResponseClass = new TypeToken<Flowable<Response<String>>>() {
    }.getType();
    assertThat(factory.get(fResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResponseWildcard = new TypeToken<Observable<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(oResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResponseWildcard = new TypeToken<Single<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResponseWildcard = new TypeToken<Maybe<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(mResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResponseWildcard = new TypeToken<Flowable<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(fResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResultClass = new TypeToken<Observable<Result<String>>>() {
    }.getType();
    assertThat(factory.get(oResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResultClass = new TypeToken<Single<Result<String>>>() {
    }.getType();
    assertThat(factory.get(sResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResultClass = new TypeToken<Maybe<Result<String>>>() {
    }.getType();
    assertThat(factory.get(mResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResultClass = new TypeToken<Flowable<Result<String>>>() {
    }.getType();
    assertThat(factory.get(fResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResultWildcard = new TypeToken<Observable<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(oResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResultWildcard = new TypeToken<Single<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResultWildcard = new TypeToken<Maybe<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(mResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResultWildcard = new TypeToken<Flowable<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(fResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
}
Also used : Maybe(io.reactivex.Maybe) Observable(io.reactivex.Observable) Type(java.lang.reflect.Type) Single(io.reactivex.Single) TypeToken(com.google.common.reflect.TypeToken) Flowable(io.reactivex.Flowable) Test(org.junit.Test)

Aggregations

Flowable (io.reactivex.Flowable)65 Test (org.junit.Test)29 List (java.util.List)21 TimeUnit (java.util.concurrent.TimeUnit)19 Observable (io.reactivex.Observable)14 TestSubscriber (io.reactivex.subscribers.TestSubscriber)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)13 Single (io.reactivex.Single)13 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 FlowableRxInvoker (org.apache.cxf.jaxrs.rx2.client.FlowableRxInvoker)13 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx2.client.FlowableRxInvokerProvider)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 BeforeClass (org.junit.BeforeClass)13 Collections (java.util.Collections)9 Response (javax.ws.rs.core.Response)9 Assert.assertEquals (org.junit.Assert.assertEquals)9