Search in sources :

Example 51 with Function

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

the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorFirstObservableThrows.

@Test
public void testTimeoutSelectorFirstObservableThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    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();
    source.timeout(Observable.<Integer>error(new TestException()), timeoutFunc, other).subscribe(o);
    verify(o).onError(any(TestException.class));
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : Function(io.reactivex.functions.Function) TestException(io.reactivex.exceptions.TestException) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 52 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 53 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 54 with Function

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

the class ReactiveTest method testInsertOneToMany.

@Test
public void testInsertOneToMany() 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();
    assertTrue(person.getPhoneNumbers().toList().size() == 1);
}
Also used : Function(io.reactivex.functions.Function) Phone(io.requery.test.model.Phone) Person(io.requery.test.model.Person) Test(org.junit.Test)

Example 55 with Function

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

the class ReactiveTest method testQuerySelfObservableMap.

@Test
public void testQuerySelfObservableMap() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    Disposable disposable = data.select(Person.class).limit(2).get().observableResult().flatMap(new Function<ReactiveResult<Person>, Observable<Person>>() {

        @Override
        public Observable<Person> apply(ReactiveResult<Person> persons) {
            return persons.observable();
        }
    }).subscribe(new Consumer<Person>() {

        @Override
        public void accept(Person persons) {
            count.incrementAndGet();
        }
    });
    data.insert(randomPerson()).blockingGet();
    data.insert(randomPerson()).blockingGet();
    assertEquals(3, count.get());
    disposable.dispose();
}
Also used : Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Person(io.requery.test.model.Person) ReactiveResult(io.requery.reactivex.ReactiveResult) Test(org.junit.Test)

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