Search in sources :

Example 36 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableDelaySubscriptionOtherTest method noPrematureSubscriptionToError.

@Test
public void noPrematureSubscriptionToError() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> to = new TestObserver<>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(to);
    to.assertNotComplete();
    to.assertNoErrors();
    to.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onComplete();
    Assert.assertEquals("No subscription", 1, subscribed.get());
    to.assertNoValues();
    to.assertNotComplete();
    to.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 37 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableConcatMapSchedulerTest method concatArray.

@SuppressWarnings("unchecked")
@Test
public void concatArray() throws Exception {
    for (int i = 2; i < 10; i++) {
        Observable<Integer>[] obs = new Observable[i];
        Arrays.fill(obs, Observable.just(1));
        Integer[] expected = new Integer[i];
        Arrays.fill(expected, 1);
        Method m = Observable.class.getMethod("concatArray", ObservableSource[].class);
        TestObserver<Integer> to = TestObserver.create();
        ((Observable<Integer>) m.invoke(null, new Object[] { obs })).subscribe(to);
        to.assertValues(expected);
        to.assertNoErrors();
        to.assertComplete();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Method(java.lang.reflect.Method) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 38 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableFlatMapTest method flatMapSelectorMaxConcurrent.

@Test
public void flatMapSelectorMaxConcurrent() {
    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());
        }
    }, new BiFunction<Integer, Integer, Integer>() {

        @Override
        public Integer apply(Integer t1, Integer t2) {
            return t1 * 1000 + t2;
        }
    }, m);
    TestObserver<Integer> to = new TestObserver<>();
    source.subscribe(to);
    to.awaitDone(5, TimeUnit.SECONDS);
    to.assertNoErrors();
    Set<Integer> expected = new HashSet<>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
    Assert.assertEquals(expected.size(), to.values().size());
    System.out.println("--> testFlatMapSelectorMaxConcurrent: " + to.values());
    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 39 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class ObservableDelayTest method delaySupplierErrors.

@Test
public void delaySupplierErrors() {
    final PublishSubject<Integer> ps = PublishSubject.create();
    Observable<Integer> source = Observable.range(1, 5);
    TestObserver<Integer> to = new TestObserver<>();
    source.delaySubscription(ps).subscribe(to);
    to.assertNoValues();
    to.assertNoErrors();
    to.assertNotComplete();
    ps.onError(new TestException());
    to.assertNoValues();
    to.assertNotComplete();
    to.assertError(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 40 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver 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)

Aggregations

Test (org.junit.Test)225 TestException (io.reactivex.rxjava3.exceptions.TestException)169 TestObserver (io.reactivex.rxjava3.observers.TestObserver)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Disposable (io.reactivex.rxjava3.disposables.Disposable)34 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)27 TargetApi (android.annotation.TargetApi)21 Observable (io.reactivex.rxjava3.core.Observable)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)19 IOException (java.io.IOException)19 Matchers.anyString (org.mockito.Matchers.anyString)16 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)13 Action (io.reactivex.rxjava3.functions.Action)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 InOrder (org.mockito.InOrder)7 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)6 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)6 List (java.util.List)6 Assert (org.junit.Assert)6 Activity (android.app.Activity)5