Search in sources :

Example 81 with TestObserver

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

the class CompletableDelaySubscriptionTest method disposeMain.

@Test
public void disposeMain() {
    CompletableSubject cs = CompletableSubject.create();
    TestScheduler scheduler = new TestScheduler();
    TestObserver<Void> to = cs.delaySubscription(1, TimeUnit.SECONDS, scheduler).test();
    assertFalse(cs.hasObservers());
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertTrue(cs.hasObservers());
    to.dispose();
    assertFalse(cs.hasObservers());
}
Also used : CompletableSubject(io.reactivex.rxjava3.subjects.CompletableSubject) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 82 with TestObserver

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

the class CompletableDetachTest method errorDetaches.

@Test
public void errorDetaches() throws Exception {
    Disposable d = Disposable.empty();
    final WeakReference<Disposable> wr = new WeakReference<>(d);
    TestObserver<Void> to = new Completable() {

        @Override
        protected void subscribeActual(CompletableObserver observer) {
            observer.onSubscribe(wr.get());
            observer.onError(new TestException());
            observer.onError(new IOException());
        }
    }.onTerminateDetach().test();
    d = null;
    System.gc();
    Thread.sleep(200);
    to.assertFailure(TestException.class);
    assertNull(wr.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) WeakReference(java.lang.ref.WeakReference) IOException(java.io.IOException) Test(org.junit.Test)

Example 83 with TestObserver

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

the class CompletableTimerTest method timerInterruptible.

@Test
public void timerInterruptible() throws Exception {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    try {
        for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec, true) }) {
            final AtomicBoolean interrupted = new AtomicBoolean();
            TestObserver<Void> to = Completable.timer(1, TimeUnit.MILLISECONDS, s).doOnComplete(new Action() {

                @Override
                public void run() throws Exception {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        interrupted.set(true);
                    }
                }
            }).test();
            Thread.sleep(500);
            to.dispose();
            Thread.sleep(500);
            assertTrue(s.getClass().getSimpleName(), interrupted.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action(io.reactivex.rxjava3.functions.Action) Test(org.junit.Test)

Example 84 with TestObserver

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

the class CompletableMergeIterableTest method errorRace.

@Test
public void errorRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final PublishSubject<Integer> ps1 = PublishSubject.create();
            final PublishSubject<Integer> ps2 = PublishSubject.create();
            TestObserver<Void> to = Completable.merge(Arrays.asList(ps1.ignoreElements(), ps2.ignoreElements())).test();
            final TestException ex = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    ps1.onError(ex);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    ps2.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
            to.assertFailure(TestException.class);
            if (!errors.isEmpty()) {
                TestHelper.assertUndeliverable(errors, 0, TestException.class);
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 85 with TestObserver

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

the class FlowableSwitchMapCompletableTest method innerError.

@Test
public void innerError() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    CompletableSubject cs = CompletableSubject.create();
    TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs)).test();
    assertTrue(pp.hasSubscribers());
    assertFalse(cs.hasObservers());
    pp.onNext(1);
    assertTrue(cs.hasObservers());
    to.assertEmpty();
    cs.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse(pp.hasSubscribers());
    assertFalse(cs.hasObservers());
}
Also used : CompletableSubject(io.reactivex.rxjava3.subjects.CompletableSubject) Test(org.junit.Test)

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