Search in sources :

Example 31 with PublishProcessor

use of io.reactivex.rxjava3.processors.PublishProcessor in project RxJava by ReactiveX.

the class FlowableSubscriberTest method forEachWhile.

@Test
public void forEachWhile() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    final List<Integer> list = new ArrayList<>();
    Disposable d = pp.forEachWhile(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            list.add(v);
            return v < 3;
        }
    });
    assertFalse(d.isDisposed());
    pp.onNext(1);
    pp.onNext(2);
    pp.onNext(3);
    assertFalse(pp.hasSubscribers());
    assertEquals(Arrays.asList(1, 2, 3), list);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 32 with PublishProcessor

use of io.reactivex.rxjava3.processors.PublishProcessor in project RxJava by ReactiveX.

the class FlowableThrottleLastTests method throttle.

@Test
public void throttle() {
    Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
    TestScheduler s = new TestScheduler();
    PublishProcessor<Integer> o = PublishProcessor.create();
    o.throttleLast(500, TimeUnit.MILLISECONDS, s).subscribe(subscriber);
    // 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
    o.onNext(6);
    s.advanceTimeTo(1001, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(7);
    s.advanceTimeTo(1501, TimeUnit.MILLISECONDS);
    o.onComplete();
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber).onNext(2);
    inOrder.verify(subscriber).onNext(6);
    inOrder.verify(subscriber).onNext(7);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 33 with PublishProcessor

use of io.reactivex.rxjava3.processors.PublishProcessor in project RxJava by ReactiveX.

the class FlowableThrottleWithTimeoutTests method throttle.

@Test
public void throttle() {
    Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
    TestScheduler s = new TestScheduler();
    PublishProcessor<Integer> o = PublishProcessor.create();
    o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s).subscribe(subscriber);
    // 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(subscriber);
    inOrder.verify(subscriber).onNext(2);
    inOrder.verify(subscriber).onNext(6);
    inOrder.verify(subscriber).onNext(7);
    inOrder.verify(subscriber).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 34 with PublishProcessor

use of io.reactivex.rxjava3.processors.PublishProcessor in project RxJava by ReactiveX.

the class FlowableReplayTest method disposeNoNeedForResetTimeBound.

@Test
public void disposeNoNeedForResetTimeBound() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    ConnectableFlowable<Integer> cf = pp.replay(10, TimeUnit.MINUTES);
    TestSubscriber<Integer> ts = cf.test();
    Disposable d = cf.connect();
    pp.onNext(1);
    d.dispose();
    ts = cf.test();
    ts.assertEmpty();
    cf.connect();
    ts.assertEmpty();
    pp.onNext(2);
    ts.assertValuesOnly(2);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Example 35 with PublishProcessor

use of io.reactivex.rxjava3.processors.PublishProcessor in project RxJava by ReactiveX.

the class FlowableReplayTest method disposeNoNeedForReset.

@Test
public void disposeNoNeedForReset() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    ConnectableFlowable<Integer> cf = pp.replay();
    TestSubscriber<Integer> ts = cf.test();
    Disposable d = cf.connect();
    pp.onNext(1);
    d.dispose();
    ts = cf.test();
    ts.assertEmpty();
    cf.connect();
    ts.assertEmpty();
    pp.onNext(2);
    ts.assertValuesOnly(2);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable)

Aggregations

Test (org.junit.Test)124 TestException (io.reactivex.rxjava3.exceptions.TestException)88 InOrder (org.mockito.InOrder)25 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)24 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)18 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)18 Disposable (io.reactivex.rxjava3.disposables.Disposable)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Assert (org.junit.Assert)8 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)7 io.reactivex.rxjava3.exceptions (io.reactivex.rxjava3.exceptions)6 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)6 PublishProcessor (io.reactivex.rxjava3.processors.PublishProcessor)6 IOException (java.io.IOException)6 TimeUnit (java.util.concurrent.TimeUnit)6 Functions (io.reactivex.rxjava3.internal.functions.Functions)5 FlowableReplay (io.reactivex.rxjava3.internal.operators.flowable.FlowableReplay)4 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)4 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)4