Search in sources :

Example 41 with BooleanSubscription

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

the class FlowableTakeTest method testMultiTake.

@Test(timeout = 2000)
public void testMultiTake() {
    final AtomicInteger count = new AtomicInteger();
    Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            BooleanSubscription bs = new BooleanSubscription();
            s.onSubscribe(bs);
            for (int i = 0; !bs.isCancelled(); i++) {
                System.out.println("Emit: " + i);
                count.incrementAndGet();
                s.onNext(i);
            }
        }
    }).take(100).take(1).blockingForEach(new Consumer<Integer>() {

        @Override
        public void accept(Integer t1) {
            System.out.println("Receive: " + t1);
        }
    });
    assertEquals(1, count.get());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Example 42 with BooleanSubscription

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

the class FlowableSwitchIfEmptyTest method testSwitchShouldTriggerUnsubscribe.

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

        @Override
        public void subscribe(final Subscriber<? super Long> subscriber) {
            subscriber.onSubscribe(bs);
            subscriber.onComplete();
        }
    }).switchIfEmpty(Flowable.<Long>never()).subscribe();
    assertTrue(bs.isCancelled());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 43 with BooleanSubscription

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

the class FlowableSwitchTest method testSwitchIssue737.

@Test
public void testSwitchIssue737() {
    // https://github.com/ReactiveX/RxJava/issues/737
    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, 0, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 10, "1-one");
                    publishNext(observer, 20, "1-two");
                    // The following events will be ignored
                    publishNext(observer, 30, "1-three");
                    publishCompleted(observer, 40);
                }
            }));
            publishNext(observer, 25, Flowable.unsafeCreate(new Publisher<String>() {

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 10, "2-one");
                    publishNext(observer, 20, "2-two");
                    publishNext(observer, 30, "2-three");
                    publishCompleted(observer, 40);
                }
            }));
            publishCompleted(observer, 30);
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(observer);
    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("1-one");
    inOrder.verify(observer, times(1)).onNext("1-two");
    inOrder.verify(observer, times(1)).onNext("2-one");
    inOrder.verify(observer, times(1)).onNext("2-two");
    inOrder.verify(observer, times(1)).onNext("2-three");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 44 with BooleanSubscription

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

the class FlowableSwitchTest method testSwitchWithSubsequenceError.

@Test
public void testSwitchWithSubsequenceError() {
    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, 50, "one");
                    publishNext(observer, 100, "two");
                }
            }));
            publishNext(observer, 130, Flowable.unsafeCreate(new Publisher<String>() {

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

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 50, "three");
                }
            }));
        }
    });
    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(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext("three");
    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 45 with BooleanSubscription

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

the class FlowableSwitchTest method testSwitchWhenInnerCompleteBeforeOuter.

@Test
public void testSwitchWhenInnerCompleteBeforeOuter() {
    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, 10, Flowable.unsafeCreate(new Publisher<String>() {

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

                @Override
                public void subscribe(Subscriber<? super String> observer) {
                    observer.onSubscribe(new BooleanSubscription());
                    publishNext(observer, 0, "three");
                    publishNext(observer, 10, "four");
                    publishCompleted(observer, 20);
                }
            }));
            publishCompleted(observer, 200);
        }
    });
    Flowable<String> sampled = Flowable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(150, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onComplete();
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(1)).onNext("four");
    scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).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