Search in sources :

Example 71 with PublishSubject

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

the class ObservableThrottleWithTimeoutTests method throttle.

@Test
public void throttle() {
    Observer<Integer> observer = TestHelper.mockObserver();
    TestScheduler s = new TestScheduler();
    PublishSubject<Integer> o = PublishSubject.create();
    o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s).subscribe(observer);
    // send events with simulated time increments
    s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(1);
    // deliver
    o.onNext(2);
    s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(3);
    s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(4);
    s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(5);
    // deliver at 1300 after 500ms has passed since onNext(5)
    o.onNext(6);
    s.advanceTimeTo(1300, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(7);
    s.advanceTimeTo(1800, TimeUnit.MILLISECONDS);
    o.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onNext(2);
    inOrder.verify(observer).onNext(6);
    inOrder.verify(observer).onNext(7);
    inOrder.verify(observer).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 72 with PublishSubject

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

the class SingleFlatMapObservableTest method errorMain.

@Test
public void errorMain() {
    SingleSubject<Integer> ss = SingleSubject.create();
    PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = ss.flatMapObservable(Functions.justFunction(ps)).test();
    assertTrue(ss.hasObservers());
    assertFalse(ps.hasObservers());
    ss.onError(new TestException());
    assertFalse(ss.hasObservers());
    assertFalse(ps.hasObservers());
    to.assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 73 with PublishSubject

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

the class ObservableReplayTest method noHeadRetentionErrorTime.

@Test
public void noHeadRetentionErrorTime() {
    PublishSubject<Integer> source = PublishSubject.create();
    ObservableReplay<Integer> co = (ObservableReplay<Integer>) source.replay(1, TimeUnit.MINUTES, Schedulers.computation());
    co.connect();
    BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>) (co.current.get().buffer);
    source.onNext(1);
    source.onNext(2);
    source.onError(new TestException());
    assertNull(buf.get().value);
    Object o = buf.get();
    buf.trimHead();
    assertSame(o, buf.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) ObservableReplay(io.reactivex.rxjava3.internal.operators.observable.ObservableReplay)

Example 74 with PublishSubject

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

the class ObservableWindowWithStartEndObservableTest method startError.

@Test
public void startError() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> start = PublishSubject.create();
    final PublishSubject<Integer> end = PublishSubject.create();
    TestObserver<Integer> to = source.window(start, new Function<Integer, ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> apply(Integer v) throws Exception {
            return end;
        }
    }).flatMap(Functions.<Observable<Integer>>identity()).test();
    start.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("Source has observers!", source.hasObservers());
    assertFalse("Start has observers!", start.hasObservers());
    assertFalse("End has observers!", end.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

Example 75 with PublishSubject

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

the class ObservableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
    final PublishSubject<String> subject = PublishSubject.create();
    final TestScheduler scheduler = new TestScheduler();
    final TestObserver<String> observer = subject.timeout(100, TimeUnit.MILLISECONDS, scheduler).test();
    assertTrue(subject.hasObservers());
    observer.dispose();
    assertFalse(subject.hasObservers());
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

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