Search in sources :

Example 36 with BooleanSubscription

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

the class FlowableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutAndOnNextRaceCondition.

@Test
public void testTimeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException {
    // Thread 1                                    Thread 2
    //
    // observer.onNext(1)
    // start timeout
    // unsubscribe timeout in thread 2          start to do some long-time work in "unsubscribe"
    // observer.onNext(2)
    // timeout.onNext(1)
    //                                          "unsubscribe" done
    //
    //
    // In the above case, the timeout operator should ignore "timeout.onNext(1)"
    // since "observer" has already seen 2.
    final CountDownLatch observerReceivedTwo = new CountDownLatch(1);
    final CountDownLatch timeoutEmittedOne = new CountDownLatch(1);
    final CountDownLatch observerCompleted = new CountDownLatch(1);
    final CountDownLatch enteredTimeoutOne = new CountDownLatch(1);
    final AtomicBoolean latchTimeout = new AtomicBoolean(false);
    final Function<Integer, Flowable<Integer>> timeoutFunc = new Function<Integer, Flowable<Integer>>() {

        @Override
        public Flowable<Integer> apply(Integer t1) {
            if (t1 == 1) {
                // Force "unsubscribe" run on another thread
                return Flowable.unsafeCreate(new Publisher<Integer>() {

                    @Override
                    public void subscribe(Subscriber<? super Integer> subscriber) {
                        subscriber.onSubscribe(new BooleanSubscription());
                        enteredTimeoutOne.countDown();
                        // force the timeout message be sent after observer.onNext(2)
                        while (true) {
                            try {
                                if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) {
                                    // CountDownLatch timeout
                                    // There should be something wrong
                                    latchTimeout.set(true);
                                }
                                break;
                            } catch (InterruptedException e) {
                            // Since we just want to emulate a busy method,
                            // we ignore the interrupt signal from Scheduler.
                            }
                        }
                        subscriber.onNext(1);
                        timeoutEmittedOne.countDown();
                    }
                }).subscribeOn(Schedulers.newThread());
            } else {
                return PublishProcessor.create();
            }
        }
    };
    final Subscriber<Integer> o = TestHelper.mockSubscriber();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerReceivedTwo.countDown();
            return null;
        }
    }).when(o).onNext(2);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            observerCompleted.countDown();
            return null;
        }
    }).when(o).onComplete();
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(o);
    new Thread(new Runnable() {

        @Override
        public void run() {
            PublishProcessor<Integer> source = PublishProcessor.create();
            source.timeout(timeoutFunc, Flowable.just(3)).subscribe(ts);
            // start timeout
            source.onNext(1);
            try {
                if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // disable timeout
            source.onNext(2);
            try {
                if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) {
                    latchTimeout.set(true);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            source.onComplete();
        }
    }).start();
    if (!observerCompleted.await(30, TimeUnit.SECONDS)) {
        latchTimeout.set(true);
    }
    assertFalse("CoundDownLatch timeout", latchTimeout.get());
    InOrder inOrder = inOrder(o);
    inOrder.verify(o).onSubscribe((Subscription) notNull());
    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o, never()).onNext(3);
    inOrder.verify(o).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(io.reactivex.functions.Function) TestSubscriber(io.reactivex.subscribers.TestSubscriber) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 37 with BooleanSubscription

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

the class FlowableUnsubscribeOnTest method signalAfterDispose.

@Test
public void signalAfterDispose() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> observer) {
                observer.onSubscribe(new BooleanSubscription());
                observer.onNext(1);
                observer.onNext(2);
                observer.onError(new TestException());
                observer.onComplete();
            }
        }.unsubscribeOn(Schedulers.single()).take(1).test().assertResult(1);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 38 with BooleanSubscription

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

the class FlowableStrictTest method noCancelOnComplete.

@Test
public void noCancelOnComplete() {
    final BooleanSubscription bs = new BooleanSubscription();
    Flowable.fromPublisher(new Publisher<Object>() {

        @Override
        public void subscribe(Subscriber<? super Object> p) {
            p.onSubscribe(bs);
            p.onComplete();
        }
    }).strict().subscribe(new Subscriber<Object>() {

        Subscription s;

        @Override
        public void onSubscribe(Subscription s) {
            this.s = s;
        }

        @Override
        public void onNext(Object t) {
        // not called
        }

        @Override
        public void onError(Throwable t) {
        // not called
        }

        @Override
        public void onComplete() {
            s.cancel();
        }
    });
    assertFalse(bs.isCancelled());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 39 with BooleanSubscription

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

the class FlowableSubscribeOnTest method testUnsubscribeInfiniteStream.

@Test(timeout = 5000)
public void testUnsubscribeInfiniteStream() throws InterruptedException {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    final AtomicInteger count = new AtomicInteger();
    Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> sub) {
            BooleanSubscription bs = new BooleanSubscription();
            sub.onSubscribe(bs);
            for (int i = 1; !bs.isCancelled(); i++) {
                count.incrementAndGet();
                sub.onNext(i);
            }
        }
    }).subscribeOn(Schedulers.newThread()).take(10).subscribe(ts);
    ts.awaitTerminalEvent(1000, TimeUnit.MILLISECONDS);
    ts.dispose();
    // give time for the loop to continue
    Thread.sleep(200);
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    assertEquals(10, count.get());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) SubscribeOnSubscriber(io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)

Example 40 with BooleanSubscription

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

the class FlowableTakeTest method testTakeZeroDoesntLeakError.

@Test
@Ignore("take(0) is now empty() and doesn't even subscribe to the original source")
public void testTakeZeroDoesntLeakError() {
    final AtomicBoolean subscribed = new AtomicBoolean(false);
    final BooleanSubscription bs = new BooleanSubscription();
    Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> observer) {
            subscribed.set(true);
            observer.onSubscribe(bs);
            observer.onError(new Throwable("test failed"));
        }
    });
    Subscriber<String> observer = TestHelper.mockSubscriber();
    source.take(0).subscribe(observer);
    assertTrue("source subscribed", subscribed.get());
    assertTrue("source unsubscribed", bs.isCancelled());
    verify(observer, never()).onNext(anyString());
    // even though onError is called we take(0) so shouldn't see it
    verify(observer, never()).onError(any(Throwable.class));
    verify(observer, times(1)).onComplete();
    verifyNoMoreInteractions(observer);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

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