Search in sources :

Example 31 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableDelayTest method delayWithObservableDelayThrows.

@Test
public void delayWithObservableDelayThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> delay = PublishSubject.create();
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(delayFunc).subscribe(o);
    source.onNext(1);
    delay.onError(new TestException());
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 32 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableDelayTest method delayWithObservableAsTimed.

@Test
public void delayWithObservableAsTimed() {
    Observable<Long> source = Observable.interval(1L, TimeUnit.SECONDS, scheduler).take(3);
    final Observable<Long> delayer = Observable.timer(500L, TimeUnit.MILLISECONDS, scheduler);
    Function<Long, Observable<Long>> delayFunc = new Function<Long, Observable<Long>>() {

        @Override
        public Observable<Long> apply(Long t1) {
            return delayer;
        }
    };
    Observable<Long> delayed = source.delay(delayFunc);
    delayed.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
    verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(0L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(1L);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3400L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyLong());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext(2L);
    verify(observer, times(1)).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 33 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableDelayTest method delayWithObservableDelayFunctionThrows.

@Test
public void delayWithObservableDelayFunctionThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            throw new TestException();
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(delayFunc).subscribe(o);
    source.onNext(1);
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 34 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableFlatMapTest method flatMapMaxConcurrent.

@Test
public void flatMapMaxConcurrent() {
    final int m = 4;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
        }
    }, m);
    TestObserver<Integer> to = new TestObserver<>();
    source.subscribe(to);
    to.awaitDone(5, TimeUnit.SECONDS);
    to.assertNoErrors();
    Set<Integer> expected = new HashSet<>(Arrays.asList(10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101));
    Assert.assertEquals(expected.size(), to.values().size());
    Assert.assertTrue(expected.containsAll(to.values()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 35 with Observable

use of io.reactivex.rxjava3.core.Observable in project RxJava by ReactiveX.

the class ObservableFlatMapTest method failingFusedInnerCancelsSource.

@Test
public void failingFusedInnerCancelsSource() {
    final AtomicInteger counter = new AtomicInteger();
    Observable.range(1, 5).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer v) throws Exception {
            counter.getAndIncrement();
        }
    }).flatMap(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer v) throws Exception {
            return Observable.<Integer>fromIterable(new Iterable<Integer>() {

                @Override
                public Iterator<Integer> iterator() {
                    return new Iterator<Integer>() {

                        @Override
                        public boolean hasNext() {
                            return true;
                        }

                        @Override
                        public Integer next() {
                            throw new TestException();
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            });
        }
    }).test().assertFailure(TestException.class);
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable) IOException(java.io.IOException)

Aggregations

Test (org.junit.Test)132 TestException (io.reactivex.rxjava3.exceptions.TestException)109 Observable (io.reactivex.rxjava3.core.Observable)83 InOrder (org.mockito.InOrder)60 TestObserver (io.reactivex.rxjava3.observers.TestObserver)38 Disposable (io.reactivex.rxjava3.disposables.Disposable)35 IOException (java.io.IOException)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)22 Observer (io.reactivex.rxjava3.core.Observer)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 Function (io.reactivex.rxjava3.functions.Function)10 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)6 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)6 Collections (java.util.Collections)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Assert (org.junit.Assert)6