Search in sources :

Example 36 with Disposable

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

the class FlowableReplayTest method connectConsumerThrows.

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

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

Example 37 with Disposable

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

the class FlowableRetryTest method shouldDisposeInnerObservable.

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

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

                @Override
                public Flowable<Object> apply(Throwable 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) GroupedFlowable(io.reactivex.flowables.GroupedFlowable) Test(org.junit.Test)

Example 38 with Disposable

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

the class FlowablePublishTest method testSubscribeAfterDisconnectThenConnect.

@Test
public void testSubscribeAfterDisconnectThenConnect() {
    ConnectableFlowable<Integer> source = Flowable.just(1).publish();
    TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();
    source.subscribe(ts1);
    Disposable s = source.connect();
    ts1.assertValue(1);
    ts1.assertNoErrors();
    ts1.assertTerminated();
    TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>();
    source.subscribe(ts2);
    Disposable s2 = source.connect();
    ts2.assertValue(1);
    ts2.assertNoErrors();
    ts2.assertTerminated();
    System.out.println(s);
    System.out.println(s2);
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 39 with Disposable

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

the class FlowablePublishTest method syncFusedObserveOn2.

@Test
public void syncFusedObserveOn2() {
    ConnectableFlowable<Integer> co = Flowable.range(0, 1000).publish();
    Flowable<Integer> obs = co.observeOn(ImmediateThinScheduler.INSTANCE);
    for (int i = 0; i < 1000; i++) {
        for (int j = 1; j < 6; j++) {
            List<TestSubscriber<Integer>> tss = new ArrayList<TestSubscriber<Integer>>();
            for (int k = 1; k < j; k++) {
                TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
                tss.add(ts);
                obs.subscribe(ts);
            }
            Disposable s = co.connect();
            for (TestSubscriber<Integer> ts : tss) {
                ts.awaitDone(5, TimeUnit.SECONDS).assertSubscribed().assertValueCount(1000).assertNoErrors().assertComplete();
            }
            s.dispose();
        }
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 40 with Disposable

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

the class FlowableRefCountTest method onlyFirstShouldSubscribeAndLastUnsubscribe.

@Test
public void onlyFirstShouldSubscribeAndLastUnsubscribe() {
    final AtomicInteger subscriptionCount = new AtomicInteger();
    final AtomicInteger unsubscriptionCount = new AtomicInteger();
    Flowable<Integer> observable = Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> observer) {
            subscriptionCount.incrementAndGet();
            observer.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                }

                @Override
                public void cancel() {
                    unsubscriptionCount.incrementAndGet();
                }
            });
        }
    });
    Flowable<Integer> refCounted = observable.publish().refCount();
    Disposable first = refCounted.subscribe();
    assertEquals(1, subscriptionCount.get());
    Disposable second = refCounted.subscribe();
    assertEquals(1, subscriptionCount.get());
    first.dispose();
    assertEquals(0, unsubscriptionCount.get());
    second.dispose();
    assertEquals(1, unsubscriptionCount.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

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