Search in sources :

Example 26 with ConnectableFlowable

use of io.reactivex.rxjava3.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class FlowablePublishTest method disposeResets.

@Test
public void disposeResets() {
    PublishProcessor<Integer> pp = PublishProcessor.create();
    ConnectableFlowable<Integer> cf = pp.publish();
    assertFalse(pp.hasSubscribers());
    Disposable d = cf.connect();
    assertTrue(pp.hasSubscribers());
    d.dispose();
    assertFalse(pp.hasSubscribers());
    TestSubscriber<Integer> ts = cf.test();
    cf.connect();
    assertTrue(pp.hasSubscribers());
    pp.onNext(1);
    ts.assertValuesOnly(1);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 27 with ConnectableFlowable

use of io.reactivex.rxjava3.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class FlowablePublishTest method noDisconnectSomeoneElse.

@Test
public void noDisconnectSomeoneElse() {
    ConnectableFlowable<Object> source = Flowable.never().publish();
    Disposable connection1 = source.connect();
    Disposable connection2 = source.connect();
    connection1.dispose();
    Disposable connection3 = source.connect();
    connection2.dispose();
    assertTrue(checkPublishDisposed(connection1));
    assertTrue(checkPublishDisposed(connection2));
    assertFalse(checkPublishDisposed(connection3));
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 28 with ConnectableFlowable

use of io.reactivex.rxjava3.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class FlowablePublishTest method subscribeDisconnectRace.

@Test
public void subscribeDisconnectRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final PublishProcessor<Integer> pp = PublishProcessor.create();
        final ConnectableFlowable<Integer> cf = pp.publish();
        final Disposable d = cf.connect();
        final TestSubscriber<Integer> ts = new TestSubscriber<>();
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                d.dispose();
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                cf.subscribe(ts);
            }
        };
        TestHelper.race(r1, r2);
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 29 with ConnectableFlowable

use of io.reactivex.rxjava3.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class FlowableReplayEagerTruncateTest method issue2191_SchedulerUnsubscribe.

/**
 * Specifically test interaction with a Scheduler with subscribeOn.
 *
 * @throws Throwable functional interfaces declare throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribe() throws Throwable {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.class);
    Action sourceUnsubscribed = mock(Action.class);
    final Scheduler mockScheduler = mock(Scheduler.class);
    final Disposable mockSubscription = mock(Disposable.class);
    Worker spiedWorker = workerSpy(mockSubscription);
    Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber();
    Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber();
    when(mockScheduler.createWorker()).thenReturn(spiedWorker);
    // Flowable under test
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).subscribeOn(mockScheduler).replay();
    replay.subscribe(mockObserverBeforeConnect);
    replay.subscribe(mockObserverBeforeConnect);
    replay.connect();
    replay.subscribe(mockObserverAfterConnect);
    replay.subscribe(mockObserverAfterConnect);
    verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription) any());
    verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription) any());
    // verify interactions
    verify(sourceNext, times(1)).accept(1);
    verify(sourceNext, times(1)).accept(2);
    verify(sourceNext, times(1)).accept(3);
    verify(sourceCompleted, times(1)).run();
    verify(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verifyObserverMock(mockObserverBeforeConnect, 2, 6);
    verifyObserverMock(mockObserverAfterConnect, 2, 6);
    // FIXME publish calls cancel too
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    verifyNoMoreInteractions(sourceUnsubscribed);
    verifyNoMoreInteractions(spiedWorker);
    verifyNoMoreInteractions(mockSubscription);
    verifyNoMoreInteractions(mockScheduler);
    verifyNoMoreInteractions(mockObserverBeforeConnect);
    verifyNoMoreInteractions(mockObserverAfterConnect);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker)

Example 30 with ConnectableFlowable

use of io.reactivex.rxjava3.flowables.ConnectableFlowable in project RxJava by ReactiveX.

the class FlowableReplayEagerTruncateTest method connectConsumerThrows.

@Test
public void connectConsumerThrows() {
    ConnectableFlowable<Integer> cf = Flowable.range(1, 2).replay();
    try {
        cf.connect(new Consumer<Disposable>() {

            @Override
            public void accept(Disposable t) throws Exception {
                throw new TestException();
            }
        });
        fail("Should have thrown");
    } catch (TestException ex) {
    // expected
    }
    cf.test().assertEmpty().cancel();
    cf.connect();
    cf.test().assertResult(1, 2);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException)

Aggregations

Disposable (io.reactivex.rxjava3.disposables.Disposable)25 Test (org.junit.Test)14 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)4 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)3 TestException (io.reactivex.rxjava3.exceptions.TestException)2 ConnectableFlowable (io.reactivex.rxjava3.flowables.ConnectableFlowable)2 ScalarSubscription (io.reactivex.rxjava3.internal.subscriptions.ScalarSubscription)2 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)2 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)2 Observable (io.reactivex.rxjava3.core.Observable)1 Observer (io.reactivex.rxjava3.core.Observer)1 HasUpstreamPublisher (io.reactivex.rxjava3.internal.fuseable.HasUpstreamPublisher)1 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)1 ConnectConsumer (io.reactivex.rxjava3.internal.util.ConnectConsumer)1 ConnectableObservable (io.reactivex.rxjava3.observables.ConnectableObservable)1 ParallelFlowable (io.reactivex.rxjava3.parallel.ParallelFlowable)1 IOException (java.io.IOException)1