Search in sources :

Example 71 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxRelay by JakeWharton.

the class PublishRelayTest method testNestedSubscribe.

@Test
@SuppressWarnings("CheckReturnValue")
public void testNestedSubscribe() {
    final PublishRelay<Integer> s = PublishRelay.create();
    final AtomicInteger countParent = new AtomicInteger();
    final AtomicInteger countChildren = new AtomicInteger();
    final AtomicInteger countTotal = new AtomicInteger();
    final ArrayList<String> list = new ArrayList<String>();
    s.flatMap(new Function<Integer, Observable<String>>() {

        @Override
        public Observable<String> apply(final Integer v) {
            countParent.incrementAndGet();
            // then subscribe to subject again (it will not receive the previous value)
            return s.map(new Function<Integer, String>() {

                @Override
                public String apply(Integer v2) {
                    countChildren.incrementAndGet();
                    return "Parent: " + v + " Child: " + v2;
                }
            });
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            countTotal.incrementAndGet();
            list.add(v);
        }
    });
    for (int i = 0; i < 10; i++) {
        s.accept(i);
    }
    // System.out.println("countParent: " + countParent.get());
    // System.out.println("countChildren: " + countChildren.get());
    // System.out.println("countTotal: " + countTotal.get());
    // 9+8+7+6+5+4+3+2+1+0 == 45
    assertEquals(45, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.rxjava3.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 72 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class FlowableTimeoutWithSelectorTest method timeoutConsumerIsDisposed.

@Test
public void timeoutConsumerIsDisposed() {
    TimeoutConsumer consumer = new TimeoutConsumer(0, null);
    assertFalse(consumer.isDisposed());
    consumer.dispose();
    assertTrue(consumer.isDisposed());
}
Also used : TimeoutConsumer(io.reactivex.rxjava3.internal.operators.flowable.FlowableTimeout.TimeoutConsumer) Test(org.junit.Test)

Example 73 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class FlowableSwitchIfEmptyTest method switchWhenNotEmpty.

@Test
public void switchWhenNotEmpty() throws Exception {
    final AtomicBoolean subscribed = new AtomicBoolean(false);
    final Flowable<Integer> flowable = Flowable.just(4).switchIfEmpty(Flowable.just(2).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            subscribed.set(true);
        }
    }));
    assertEquals(4, flowable.blockingSingle().intValue());
    assertFalse(subscribed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.reactivex.rxjava3.functions.Consumer) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Test(org.junit.Test)

Example 74 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class SingleSafeSubscribeTest method onErrorCrash.

@Test
public void onErrorCrash() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        doThrow(new TestException()).when(consumer).onError(any());
        new Single<Integer>() {

            @Override
            protected void subscribeActual(@NonNull SingleObserver<? super Integer> observer) {
                observer.onSubscribe(Disposable.empty());
                // none of the following should arrive at the consumer
                observer.onError(new IOException());
            }
        }.safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onError(any(IOException.class));
        order.verifyNoMoreInteractions();
        TestHelper.assertError(errors, 0, CompositeException.class);
        CompositeException compositeException = (CompositeException) errors.get(0);
        TestHelper.assertError(compositeException.getExceptions(), 0, IOException.class);
        TestHelper.assertError(compositeException.getExceptions(), 1, TestException.class);
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) IOException(java.io.IOException) Test(org.junit.Test)

Example 75 with Consumer

use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.

the class SingleSafeSubscribeTest method normalError.

@Test
public void normalError() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        @SuppressWarnings("unchecked") SingleObserver<Integer> consumer = mock(SingleObserver.class);
        Single.<Integer>error(new TestException()).safeSubscribe(consumer);
        InOrder order = inOrder(consumer);
        order.verify(consumer).onSubscribe(any(Disposable.class));
        order.verify(consumer).onError(any(TestException.class));
        order.verifyNoMoreInteractions();
        assertTrue("" + errors, errors.isEmpty());
    });
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3