Search in sources :

Example 46 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableTimeoutTests method onNextOnTimeoutRaceFallback.

@Test
public void onNextOnTimeoutRaceFallback() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch, Observable.just(2)).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                ps.onNext(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.isTerminated()) {
            int c = to.values().size();
            if (c == 1) {
                int v = to.values().get(0);
                assertTrue("" + v, v == 1 || v == 2);
            } else {
                to.assertResult(1, 2);
            }
        } else {
            to.assertValuesOnly(1);
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 47 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableTimeoutTests method onNextOnTimeoutRace.

@Test
public void onNextOnTimeoutRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                ps.onNext(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.values().size() != 0) {
            if (to.errors().size() != 0) {
                to.assertFailure(TimeoutException.class, 1);
                to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
            } else {
                to.assertValuesOnly(1);
            }
        } else {
            to.assertFailure(TimeoutException.class);
            to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 48 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableThrottleLatestTest method normalEmitLast.

@Test
public void normalEmitLast() {
    TestScheduler sch = new TestScheduler();
    PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = ps.throttleLatest(1, TimeUnit.SECONDS, sch, true).test();
    ps.onNext(1);
    to.assertValuesOnly(1);
    ps.onNext(2);
    to.assertValuesOnly(1);
    ps.onNext(3);
    to.assertValuesOnly(1);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3);
    ps.onNext(4);
    to.assertValuesOnly(1, 3);
    ps.onNext(5);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3, 5);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3, 5);
    ps.onNext(6);
    to.assertValuesOnly(1, 3, 5, 6);
    ps.onNext(7);
    ps.onComplete();
    to.assertResult(1, 3, 5, 6, 7);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertResult(1, 3, 5, 6, 7);
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 49 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableThrottleLatestTest method reentrantComplete.

@Test
public void reentrantComplete() {
    TestScheduler sch = new TestScheduler();
    final PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = new TestObserver<Integer>() {

        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            if (t == 1) {
                ps.onNext(2);
            }
            if (t == 2) {
                ps.onComplete();
            }
        }
    };
    ps.throttleLatest(1, TimeUnit.SECONDS, sch).subscribe(to);
    ps.onNext(1);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertResult(1, 2);
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.Test)

Example 50 with PublishSubject

use of io.reactivex.rxjava3.subjects.PublishSubject in project RxJava by ReactiveX.

the class ObservableTakeTimedTest method takeTimed.

@Test
public void takeTimed() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> source = PublishSubject.create();
    Observable<Integer> result = source.take(1, TimeUnit.SECONDS, scheduler);
    Observer<Object> o = TestHelper.mockObserver();
    result.subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(4);
    verify(o, never()).onError(any(Throwable.class));
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestException (io.reactivex.rxjava3.exceptions.TestException)69 Test (org.junit.Test)68 InOrder (org.mockito.InOrder)36 Observable (io.reactivex.rxjava3.core.Observable)28 Disposable (io.reactivex.rxjava3.disposables.Disposable)17 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)14 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 ObservableReplay (io.reactivex.rxjava3.internal.operators.observable.ObservableReplay)4 IOException (java.io.IOException)4 TargetApi (android.annotation.TargetApi)3 Function (io.reactivex.rxjava3.functions.Function)3 CrashingIterable (io.reactivex.rxjava3.internal.util.CrashingIterable)3 PublishSubject (io.reactivex.rxjava3.subjects.PublishSubject)3 Matchers.anyString (org.mockito.Matchers.anyString)3 NonNull (io.reactivex.rxjava3.annotations.NonNull)2 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)1 Observer (io.reactivex.rxjava3.core.Observer)1 GroupedObservable (io.reactivex.rxjava3.observables.GroupedObservable)1