Search in sources :

Example 6 with BooleanSubscription

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

the class FlowableTests method testCache.

@Test
public void testCache() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Flowable<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();
        }
    }).cache();
    // 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());
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 7 with BooleanSubscription

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

the class FlowableConcatTest method testConcatNonBlockingObservables.

@Test
public void testConcatNonBlockingObservables() {
    final CountDownLatch okToContinueW1 = new CountDownLatch(1);
    final CountDownLatch okToContinueW2 = new CountDownLatch(1);
    final TestObservable<String> w1 = new TestObservable<String>(null, okToContinueW1, "one", "two", "three");
    final TestObservable<String> w2 = new TestObservable<String>(null, okToContinueW2, "four", "five", "six");
    Subscriber<String> observer = TestHelper.mockSubscriber();
    Flowable<Flowable<String>> observableOfObservables = Flowable.unsafeCreate(new Publisher<Flowable<String>>() {

        @Override
        public void subscribe(Subscriber<? super Flowable<String>> observer) {
            observer.onSubscribe(new BooleanSubscription());
            // simulate what would happen in an observable
            observer.onNext(Flowable.unsafeCreate(w1));
            observer.onNext(Flowable.unsafeCreate(w2));
            observer.onComplete();
        }
    });
    Flowable<String> concat = Flowable.concat(observableOfObservables);
    concat.subscribe(observer);
    verify(observer, times(0)).onComplete();
    try {
        // release both threads
        okToContinueW1.countDown();
        okToContinueW2.countDown();
        // wait for both to finish
        w1.t.join();
        w2.t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    InOrder inOrder = inOrder(observer);
    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");
    inOrder.verify(observer, times(1)).onNext("five");
    inOrder.verify(observer, times(1)).onNext("six");
    verify(observer, times(1)).onComplete();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) InOrder(org.mockito.InOrder)

Example 8 with BooleanSubscription

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

the class BlockingFlowableNextTest method testNoBufferingOrBlockingOfSequence.

/**
     * Confirm that no buffering or blocking of the Observable onNext calls occurs and it just grabs the next emitted value.
     * <p/>
     * This results in output such as => a: 1 b: 2 c: 89
     *
     * @throws Throwable some method call is declared throws
     */
@Test
public void testNoBufferingOrBlockingOfSequence() throws Throwable {
    int repeat = 0;
    for (; ; ) {
        final SerialDisposable task = new SerialDisposable();
        try {
            final CountDownLatch finished = new CountDownLatch(1);
            final int COUNT = 30;
            final CountDownLatch timeHasPassed = new CountDownLatch(COUNT);
            final AtomicBoolean running = new AtomicBoolean(true);
            final AtomicInteger count = new AtomicInteger(0);
            final Flowable<Integer> obs = Flowable.unsafeCreate(new Publisher<Integer>() {

                @Override
                public void subscribe(final Subscriber<? super Integer> o) {
                    o.onSubscribe(new BooleanSubscription());
                    task.replace(Schedulers.single().scheduleDirect(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                while (running.get() && !task.isDisposed()) {
                                    o.onNext(count.incrementAndGet());
                                    timeHasPassed.countDown();
                                }
                                o.onComplete();
                            } catch (Throwable e) {
                                o.onError(e);
                            } finally {
                                finished.countDown();
                            }
                        }
                    }));
                }
            });
            Iterator<Integer> it = obs.blockingNext().iterator();
            assertTrue(it.hasNext());
            int a = it.next();
            assertTrue(it.hasNext());
            int b = it.next();
            // we should have a different value
            assertTrue("a and b should be different", a != b);
            // wait for some time (if times out we are blocked somewhere so fail ... set very high for very slow, constrained machines)
            timeHasPassed.await(8000, TimeUnit.MILLISECONDS);
            assertTrue(it.hasNext());
            int c = it.next();
            assertTrue("c should not just be the next in sequence", c != (b + 1));
            assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT);
            assertTrue(it.hasNext());
            int d = it.next();
            assertTrue(d > c);
            // shut down the thread
            running.set(false);
            finished.await();
            assertFalse(it.hasNext());
            System.out.println("a: " + a + " b: " + b + " c: " + c);
            break;
        } catch (AssertionError ex) {
            if (++repeat == 3) {
                throw ex;
            }
            Thread.sleep((int) (1000 * Math.pow(2, repeat - 1)));
        } finally {
            task.dispose();
        }
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) SerialDisposable(io.reactivex.disposables.SerialDisposable)

Example 9 with BooleanSubscription

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

the class BlockingFlowableToIteratorTest method overflowQueue.

@Test(expected = MissingBackpressureException.class)
public void overflowQueue() {
    Iterator<Integer> it = new Flowable<Integer>() {

        @Override
        protected void subscribeActual(Subscriber<? super Integer> s) {
            s.onSubscribe(new BooleanSubscription());
            s.onNext(1);
            s.onNext(2);
        }
    }.blockingIterable(1).iterator();
    it.next();
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Flowable(io.reactivex.Flowable)

Example 10 with BooleanSubscription

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

the class FlowableAllTest method predicateThrowsObservable.

@Test
public void predicateThrowsObservable() {
    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();
            }
        }.all(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                throw new TestException();
            }
        }).toFlowable().test().assertFailure(TestException.class);
        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)

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