Search in sources :

Example 91 with BooleanSubscription

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

the class FlowableSwitchIfEmptyTest method testSwitchTriggerUnsubscribe.

@Test
public void testSwitchTriggerUnsubscribe() throws Exception {
    final BooleanSubscription bs = new BooleanSubscription();
    Flowable<Long> withProducer = Flowable.unsafeCreate(new Publisher<Long>() {

        @Override
        public void subscribe(final Subscriber<? super Long> subscriber) {
            subscriber.onSubscribe(bs);
            subscriber.onNext(42L);
        }
    });
    Flowable.<Long>empty().switchIfEmpty(withProducer).lift(new FlowableOperator<Long, Long>() {

        @Override
        public Subscriber<? super Long> apply(final Subscriber<? super Long> child) {
            return new DefaultSubscriber<Long>() {

                @Override
                public void onComplete() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(Long aLong) {
                    cancel();
                }
            };
        }
    }).subscribe();
    assertTrue(bs.isCancelled());
// FIXME no longer assertable
//        assertTrue(sub.isUnsubscribed());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) FlowableOperator(io.reactivex.FlowableOperator) Test(org.junit.Test)

Example 92 with BooleanSubscription

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

the class FlowableSwitchTest method testSwitchWithError.

@Test
public void testSwitchWithError() {
    Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {

        @Override
        public void subscribe(Subscriber<? super Flowable<String>> observer) {
            observer.onSubscribe(new BooleanSubscription());
            publishNext(observer, 50, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(final Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 50, "one");
                    publishNext(observer, 100, "two");
                }
            }));
            publishNext(observer, 200, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 0, "three");
                    publishNext(observer, 100, "four");
                }
            }));
            publishError(observer, 250, new TestException());
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("one");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("two");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("three");
    verify(observer, never()).onComplete();
    verify(observer, never()).onError(any(Throwable.class));
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    verify(observer, never()).onComplete();
    verify(observer, times(1)).onError(any(TestException.class));
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 93 with BooleanSubscription

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

the class FlowableSwitchTest method badInnerSource.

@Test
public void badInnerSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.just(1).hide().switchMap(Functions.justFunction(new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> observer) {
                observer.onSubscribe(new BooleanSubscription());
                observer.onError(new TestException());
                observer.onComplete();
                observer.onError(new TestException());
                observer.onComplete();
            }
        })).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 94 with BooleanSubscription

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

the class FlowableSwitchTest method testUnsubscribe.

@Test
public void testUnsubscribe() {
    final AtomicBoolean isUnsubscribed = new AtomicBoolean();
    Flowable.switchOnNext(Flowable.unsafeCreate(new Publisher<Flowable<Integer>>() {

        @Override
        public void subscribe(final Subscriber<? super Flowable<Integer>> subscriber) {
            BooleanSubscription bs = new BooleanSubscription();
            subscriber.onSubscribe(bs);
            subscriber.onNext(Flowable.just(1));
            isUnsubscribed.set(bs.isCancelled());
        }
    })).take(1).subscribe();
    assertTrue("Switch doesn't propagate 'unsubscribe'", isUnsubscribed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 95 with BooleanSubscription

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

the class FlowableSwitchTest method testSwitchWhenOuterCompleteBeforeInner.

@Test
public void testSwitchWhenOuterCompleteBeforeInner() {
    Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {

        @Override
        public void subscribe(Subscriber<? super Flowable<String>> observer) {
            observer.onSubscribe(new BooleanSubscription());
            publishNext(observer, 50, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 70, "one");
                    publishNext(observer, 100, "two");
                    publishCompleted(observer, 200);
                }
            }));
            publishCompleted(observer, 60);
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(2)).onNext(anyString());
    inOrder.verify(observer, times(1)).onComplete();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

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