Search in sources :

Example 31 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class FlowablePublishTest method subscribeDisconnectRace.

@Test
public void subscribeDisconnectRace() {
    for (int i = 0; i < 500; i++) {
        final PublishProcessor<Integer> ps = PublishProcessor.create();
        final ConnectableFlowable<Integer> co = ps.publish();
        final Disposable d = co.connect();
        final TestSubscriber<Integer> to = new TestSubscriber<Integer>();
        Runnable r1 = new Runnable() {

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

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

Example 32 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class FlowablePublishTest method testPublish.

@Test
public void testPublish() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableFlowable<String> o = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(final Subscriber<? super String> observer) {
            observer.onSubscribe(new BooleanSubscription());
            new Thread(new Runnable() {

                @Override
                public void run() {
                    counter.incrementAndGet();
                    observer.onNext("one");
                    observer.onComplete();
                }
            }).start();
        }
    }).publish();
    final CountDownLatch latch = new CountDownLatch(2);
    // subscribe once
    o.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            latch.countDown();
        }
    });
    // subscribe again
    o.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            latch.countDown();
        }
    });
    Disposable s = o.connect();
    try {
        if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
            fail("subscriptions did not receive values");
        }
        assertEquals(1, counter.get());
    } finally {
        s.dispose();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) HasUpstreamPublisher(io.reactivex.internal.fuseable.HasUpstreamPublisher) Test(org.junit.Test)

Example 33 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class FlowablePublishTest method testNoDisconnectSomeoneElse.

@Test
public void testNoDisconnectSomeoneElse() {
    ConnectableFlowable<Object> source = Flowable.never().publish();
    Disposable s1 = source.connect();
    Disposable s2 = source.connect();
    s1.dispose();
    Disposable s3 = source.connect();
    s2.dispose();
    assertTrue(checkPublishDisposed(s1));
    assertTrue(checkPublishDisposed(s2));
    assertFalse(checkPublishDisposed(s3));
}
Also used : Disposable(io.reactivex.disposables.Disposable) Test(org.junit.Test)

Example 34 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class FlowableRepeatTest method shouldDisposeInnerObservable.

@Test
public void shouldDisposeInnerObservable() {
    final PublishProcessor<Object> subject = PublishProcessor.create();
    final Disposable disposable = Flowable.just("Leak").repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<Object> completions) throws Exception {
            return completions.switchMap(new Function<Object, Flowable<Object>>() {

                @Override
                public Flowable<Object> apply(Object ignore) throws Exception {
                    return subject;
                }
            });
        }
    }).subscribe();
    assertTrue(subject.hasSubscribers());
    disposable.dispose();
    assertFalse(subject.hasSubscribers());
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 35 with Disposable

use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.

the class FlowableReplayTest method testIssue2191_SchedulerUnsubscribeOnError.

/**
     * Specifically test interaction with a Scheduler with subscribeOn.
     *
     * @throws Exception functional interfaces declare throws Exception
     */
@SuppressWarnings("unchecked")
@Test
public void testIssue2191_SchedulerUnsubscribeOnError() throws Exception {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.class);
    Consumer<Throwable> sourceError = mock(Consumer.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
    Function<Integer, Integer> mockFunc = mock(Function.class);
    IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
    when(mockFunc.apply(1)).thenReturn(1);
    when(mockFunc.apply(2)).thenThrow(illegalArgumentException);
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).map(mockFunc).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).doOnError(sourceError).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(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verify(sourceNext, times(1)).accept(1);
    verify(sourceError, times(1)).accept(illegalArgumentException);
    verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException);
    verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException);
    // FIXME no longer supported
    //        verify(spiedWorker, times(1)).isUnsubscribed();
    // FIXME publish also calls cancel
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    verifyNoMoreInteractions(sourceError);
    verifyNoMoreInteractions(sourceUnsubscribed);
    verifyNoMoreInteractions(spiedWorker);
    verifyNoMoreInteractions(mockSubscription);
    verifyNoMoreInteractions(mockScheduler);
    verifyNoMoreInteractions(mockObserverBeforeConnect);
    verifyNoMoreInteractions(mockObserverAfterConnect);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker)

Aggregations

Disposable (io.reactivex.disposables.Disposable)224 Test (org.junit.Test)95 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)31 Logger (chat.rocket.android.helper.Logger)19 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 Worker (io.reactivex.Scheduler.Worker)15 List (java.util.List)14 Single (io.reactivex.Single)13 Function (io.reactivex.functions.Function)13 NonNull (io.reactivex.annotations.NonNull)12 Nullable (android.support.annotation.Nullable)11 Bundle (android.os.Bundle)10 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)10 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)10 TestSubscriber (io.reactivex.subscribers.TestSubscriber)10 Optional (com.fernandocejas.arrow.optional.Optional)9 Schedulers (io.reactivex.schedulers.Schedulers)9 TimeUnit (java.util.concurrent.TimeUnit)8 NonNull (android.support.annotation.NonNull)7