Search in sources :

Example 21 with Function

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

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorTimeoutFirst.

@Test
public void testTimeoutSelectorTimeoutFirst() throws InterruptedException {
    Observable<Integer> source = Observable.<Integer>never();
    final PublishSubject<Integer> timeout = PublishSubject.create();
    Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return timeout;
        }
    };
    Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.timeout(timeout, timeoutFunc, other).subscribe(o);
    timeout.onNext(1);
    inOrder.verify(o).onNext(100);
    inOrder.verify(o).onComplete();
    verify(o, never()).onError(any(Throwable.class));
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 22 with Function

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

the class ReplayProcessorTest method testUnsubscriptionCase.

@Test(timeout = 1000)
public void testUnsubscriptionCase() {
    ReplayProcessor<String> src = ReplayProcessor.create();
    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) {
                System.out.println(t);
                o.onNext(t);
            }

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

            @Override
            public void onComplete() {
                o.onComplete();
            }
        });
        inOrder.verify(o).onNext("0, 0");
        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 23 with Function

use of io.reactivex.functions.Function in project RxCache by VictorAlbertos.

the class ProcessorProvidersBehaviour method getDataFromLoader.

private Observable<Reply> getDataFromLoader(final io.rx_cache2.ConfigProvider configProvider, final Record record) {
    return configProvider.getLoaderObservable().map(new Function<Object, Reply>() {

        @Override
        public Reply apply(Object data) throws Exception {
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (data == null && useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            clearKeyIfNeeded(configProvider);
            if (data == null) {
                throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey());
            }
            twoLayersCache.save(configProvider.getProviderKey(), configProvider.getDynamicKey(), configProvider.getDynamicKeyGroup(), data, configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
            return new Reply(data, Source.CLOUD, configProvider.isEncrypted());
        }
    }).onErrorReturn(new Function<Object, Object>() {

        @Override
        public Object apply(Object o) throws Exception {
            clearKeyIfNeeded(configProvider);
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey(), (Throwable) o);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Reply(io.rx_cache2.Reply)

Example 24 with Function

use of io.reactivex.functions.Function in project requery by requery.

the class ReactiveTest method testQueryObservableFromEntity.

@Test
public void testQueryObservableFromEntity() throws Exception {
    final Person person = randomPerson();
    data.insert(person).map(new Function<Person, Phone>() {

        @Override
        public Phone apply(Person person) {
            Phone phone1 = randomPhone();
            phone1.setOwner(person);
            return phone1;
        }
    }).flatMap(new Function<Phone, Single<?>>() {

        @Override
        public Single<?> apply(Phone phone) {
            return data.insert(phone);
        }
    }).blockingGet();
    int count = person.getPhoneNumbers().toList().size();
    assertEquals(1, count);
}
Also used : Function(io.reactivex.functions.Function) Phone(io.requery.test.model.Phone) Person(io.requery.test.model.Person) Test(org.junit.Test)

Example 25 with Function

use of io.reactivex.functions.Function in project mosby by sockeqwe.

the class MainMenuPresenter method bindIntents.

@Override
protected void bindIntents() {
    Observable<List<String>> loadCategories = intent(MainMenuView::loadCategoriesIntent).doOnNext(categoryName -> Timber.d("intent: load category %s", categoryName)).flatMap(ignored -> backendApi.getAllCategories().subscribeOn(Schedulers.io()));
    Observable<String> selectCategory = intent(MainMenuView::selectCategoryIntent).doOnNext(categoryName -> Timber.d("intent: select category %s", categoryName)).startWith(MainMenuItem.HOME);
    List<Observable<?>> allIntents = new ArrayList<>(2);
    allIntents.add(loadCategories);
    allIntents.add(selectCategory);
    Observable<MenuViewState> menuViewStateObservable = Observable.combineLatest(allIntents, (Function<Object[], MenuViewState>) objects -> {
        List<String> categories = (List<String>) objects[0];
        String selectedCategory = (String) objects[1];
        List<MainMenuItem> categoriesItems = new ArrayList<MainMenuItem>(categories.size() + 1);
        categoriesItems.add(new MainMenuItem(MainMenuItem.HOME, selectedCategory.equals(MainMenuItem.HOME)));
        for (int i = 0; i < categories.size(); i++) {
            String category = categories.get(i);
            categoriesItems.add(new MainMenuItem(category, category.equals(selectedCategory)));
        }
        return new MenuViewState.DataState(categoriesItems);
    }).startWith(new MenuViewState.LoadingState()).onErrorReturn(MenuViewState.ErrorState::new).observeOn(AndroidSchedulers.mainThread());
    subscribeViewState(menuViewStateObservable, MainMenuView::render);
}
Also used : List(java.util.List) MviBasePresenter(com.hannesdorfmann.mosby3.mvi.MviBasePresenter) Function(io.reactivex.functions.Function) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) ProductBackendApiDecorator(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.http.ProductBackendApiDecorator) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Timber(timber.log.Timber) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) List(java.util.List) ArrayList(java.util.ArrayList) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem)

Aggregations

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