Search in sources :

Example 71 with BooleanSubscription

use of io.reactivex.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableTests method testErrorThrownWithoutErrorHandlerAsynchronous.

/**
     * https://github.com/ReactiveX/RxJava/issues/198
     *
     * Rx Design Guidelines 5.2
     *
     * "when calling the Subscribe method that only has an onNext argument, the OnError behavior will be
     * to rethrow the exception on the thread that the message comes out from the Observable.
     * The OnCompleted behavior in this case is to do nothing."
     *
     * @throws InterruptedException if the await is interrupted
     */
@Test
@Ignore("Subscribers can't throw")
public void testErrorThrownWithoutErrorHandlerAsynchronous() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    Flowable.unsafeCreate(new Publisher<Object>() {

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

                @Override
                public void run() {
                    try {
                        observer.onError(new Error("failure"));
                    } catch (Throwable e) {
                        // without an onError handler it has to just throw on whatever thread invokes it
                        exception.set(e);
                    }
                    latch.countDown();
                }
            }).start();
        }
    }).subscribe();
    // wait for exception
    latch.await(3000, TimeUnit.MILLISECONDS);
    assertNotNull(exception.get());
    assertEquals("failure", exception.get().getMessage());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 72 with BooleanSubscription

use of io.reactivex.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableTests method testReplay.

@Test
public void testReplay() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableFlowable<String> o = Flowable.<String>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();
        }
    }).replay();
    // we connect immediately and it will emit the value
    Disposable s = o.connect();
    try {
        // we then expect the following 2 subscriptions to get that same value
        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();
            }
        });
        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)

Example 73 with BooleanSubscription

use of io.reactivex.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableDoOnEachTest method onCompleteCrash.

@Test
public void onCompleteCrash() {
    Flowable.fromPublisher(new Publisher<Object>() {

        @Override
        public void subscribe(Subscriber<? super Object> s) {
            s.onSubscribe(new BooleanSubscription());
            s.onComplete();
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Exception {
            throw new IOException();
        }
    }).test().assertFailure(IOException.class);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) IOException(java.io.IOException) IOException(java.io.IOException)

Example 74 with BooleanSubscription

use of io.reactivex.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableDoOnEachTest method onErrorAfterCrash.

@Test
public void onErrorAfterCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Object>() {

            @Override
            public void subscribe(Subscriber<? super Object> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onError(new TestException());
            }
        }).doAfterTerminate(new Action() {

            @Override
            public void run() throws Exception {
                throw new IOException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) IOException(java.io.IOException) IOException(java.io.IOException)

Example 75 with BooleanSubscription

use of io.reactivex.internal.subscriptions.BooleanSubscription in project RxJava by ReactiveX.

the class FlowableDoOnEachTest method onCompleteAfterCrash.

@Test
public void onCompleteAfterCrash() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Object>() {

            @Override
            public void subscribe(Subscriber<? super Object> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onComplete();
            }
        }).doAfterTerminate(new Action() {

            @Override
            public void run() throws Exception {
                throw new IOException();
            }
        }).test().assertResult();
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)131 Test (org.junit.Test)70 TestSubscriber (io.reactivex.subscribers.TestSubscriber)31 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 IOException (java.io.IOException)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSupplier (io.reactivex.functions.BooleanSupplier)5 ForEachWhileSubscriber (io.reactivex.internal.subscribers.ForEachWhileSubscriber)5 ArrayDeque (java.util.ArrayDeque)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 Disposable (io.reactivex.disposables.Disposable)4 GroupedFlowable (io.reactivex.flowables.GroupedFlowable)4 Subscriber (org.reactivestreams.Subscriber)4 BaseObserveOnSubscriber (io.reactivex.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber)3 Worker (io.reactivex.Scheduler.Worker)2 Nullable (io.reactivex.annotations.Nullable)2 ConnectableFlowable (io.reactivex.flowables.ConnectableFlowable)2 SubscribeOnSubscriber (io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)2 FutureSubscriber (io.reactivex.internal.subscribers.FutureSubscriber)2