Search in sources :

Example 16 with BooleanSubscription

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

the class FlowablePublishTest method delayedUpstreamOnSubscribe.

@Test
public void delayedUpstreamOnSubscribe() {
    final Subscriber<?>[] sub = { null };
    new Flowable<Integer>() {

        @Override
        protected void subscribeActual(Subscriber<? super Integer> s) {
            sub[0] = s;
        }
    }.publish().connect().dispose();
    BooleanSubscription bs = new BooleanSubscription();
    sub[0].onSubscribe(bs);
    assertTrue(bs.isCancelled());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 17 with BooleanSubscription

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

the class FlowablePublishTest method testPublish.

@Test
public void testPublish() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableFlowable<String> o = Flowable.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();
        }
    }).publish();
    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();
        }
    });
    Disposable s = o.connect();
    try {
        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) TestSubscriber(io.reactivex.subscribers.TestSubscriber) HasUpstreamPublisher(io.reactivex.internal.fuseable.HasUpstreamPublisher) Test(org.junit.Test)

Example 18 with BooleanSubscription

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

the class FlowableRepeatTest method testRepeatTakeWithSubscribeOn.

@Test
public void testRepeatTakeWithSubscribeOn() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Flowable<Integer> oi = Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> sub) {
            sub.onSubscribe(new BooleanSubscription());
            counter.incrementAndGet();
            sub.onNext(1);
            sub.onNext(2);
            sub.onComplete();
        }
    }).subscribeOn(Schedulers.newThread());
    Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {

        @Override
        public Integer apply(Integer t1) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return t1;
        }
    }).take(4).toList().blockingGet().toArray();
    assertEquals(2, counter.get());
    assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 19 with BooleanSubscription

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

the class FlowableRetryTest method testSingleSubscriptionOnFirst.

@Test
public void testSingleSubscriptionOnFirst() throws Exception {
    final AtomicInteger inc = new AtomicInteger(0);
    Publisher<Integer> onSubscribe = new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> subscriber) {
            subscriber.onSubscribe(new BooleanSubscription());
            final int emit = inc.incrementAndGet();
            subscriber.onNext(emit);
            subscriber.onComplete();
        }
    };
    int first = Flowable.unsafeCreate(onSubscribe).retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() {

        @Override
        public Flowable<Object> apply(Flowable<? extends Throwable> attempt) {
            return attempt.zipWith(Flowable.just(1), new BiFunction<Throwable, Integer, Object>() {

                @Override
                public Object apply(Throwable o, Integer integer) {
                    return 0;
                }
            });
        }
    }).blockingFirst();
    assertEquals("Observer did not receive the expected output", 1, first);
    assertEquals("Subscribe was not called once", 1, inc.get());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) GroupedFlowable(io.reactivex.flowables.GroupedFlowable) Test(org.junit.Test)

Example 20 with BooleanSubscription

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

the class FlowableRetryTest method testSourceObservableRetry0.

@Test
public void testSourceObservableRetry0() throws InterruptedException {
    final AtomicInteger subsCount = new AtomicInteger(0);
    final TestSubscriber<String> ts = new TestSubscriber<String>();
    Publisher<String> onSubscribe = new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> s) {
            s.onSubscribe(new BooleanSubscription());
            subsCount.incrementAndGet();
            s.onError(new RuntimeException("failed"));
        }
    };
    Flowable.unsafeCreate(onSubscribe).retry(0).subscribe(ts);
    assertEquals(1, subsCount.get());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

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