Search in sources :

Example 66 with BooleanSubscription

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

the class SafeSubscriberTest method onNextFailureSafe.

@Test
public void onNextFailureSafe() {
    AtomicReference<Throwable> onError = new AtomicReference<Throwable>();
    try {
        SafeSubscriber<String> safeObserver = new SafeSubscriber<String>(OBSERVER_ONNEXT_FAIL(onError));
        safeObserver.onSubscribe(new BooleanSubscription());
        safeObserver.onNext("one");
        assertNotNull(onError.get());
        assertTrue(onError.get() instanceof SafeSubscriberTestException);
        assertEquals("onNextFail", onError.get().getMessage());
    } catch (Exception e) {
        fail("expects exception to be passed to onError");
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 67 with BooleanSubscription

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

the class SafeSubscriberTest method onNextNull.

@Test
public void onNextNull() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    SafeSubscriber<Integer> so = new SafeSubscriber<Integer>(ts);
    BooleanSubscription d = new BooleanSubscription();
    so.onSubscribe(d);
    so.onNext(null);
    ts.assertFailure(NullPointerException.class);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 68 with BooleanSubscription

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

the class FlowableSubscriberTest method doubleSubscribe.

@Test
public void doubleSubscribe() {
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<Integer>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            return true;
        }
    }, Functions.<Throwable>emptyConsumer(), Functions.EMPTY_ACTION);
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        s.onSubscribe(new BooleanSubscription());
        BooleanSubscription d = new BooleanSubscription();
        s.onSubscribe(d);
        assertTrue(d.isCancelled());
        TestHelper.assertError(list, 0, IllegalStateException.class, "Subscription already set!");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : ForEachWhileSubscriber(io.reactivex.internal.subscribers.ForEachWhileSubscriber) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription)

Example 69 with BooleanSubscription

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

the class FlowableTests method testCacheWithCapacity.

@Test
public void testCacheWithCapacity() 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();
        }
    }).cacheWithInitialCapacity(1);
    // 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 70 with BooleanSubscription

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

the class FlowableTests method testPublishLast.

@Test
public void testPublishLast() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();
    ConnectableFlowable<String> connectable = Flowable.<String>unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(final Subscriber<? super String> observer) {
            observer.onSubscribe(new BooleanSubscription());
            count.incrementAndGet();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    observer.onNext("first");
                    observer.onNext("last");
                    observer.onComplete();
                }
            }).start();
        }
    }).takeLast(1).publish();
    // subscribe once
    final CountDownLatch latch = new CountDownLatch(1);
    connectable.subscribe(new Consumer<String>() {

        @Override
        public void accept(String value) {
            assertEquals("last", value);
            latch.countDown();
        }
    });
    // subscribe twice
    connectable.subscribe();
    Disposable subscription = connectable.connect();
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    assertEquals(1, count.get());
    subscription.dispose();
}
Also used : Disposable(io.reactivex.disposables.Disposable) 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