Search in sources :

Example 41 with Consumer

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

the class FlowableRetryWithPredicateTest method unsubscribeFromRetry.

@Test
public void unsubscribeFromRetry() {
    PublishProcessor<Integer> processor = PublishProcessor.create();
    final AtomicInteger count = new AtomicInteger(0);
    Disposable sub = processor.retry(retryTwice).subscribe(new Consumer<Integer>() {

        @Override
        public void accept(Integer n) {
            count.incrementAndGet();
        }
    });
    processor.onNext(1);
    sub.dispose();
    processor.onNext(2);
    assertEquals(1, count.get());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 42 with Consumer

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

the class FlowablePublishTest method publish.

@Test
public void publish() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableFlowable<String> f = Flowable.unsafeCreate(new Publisher<String>() {

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

                @Override
                public void run() {
                    counter.incrementAndGet();
                    subscriber.onNext("one");
                    subscriber.onComplete();
                }
            }).start();
        }
    }).publish();
    final CountDownLatch latch = new CountDownLatch(2);
    // subscribe once
    f.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            latch.countDown();
        }
    });
    // subscribe again
    f.subscribe(new Consumer<String>() {

        @Override
        public void accept(String v) {
            assertEquals("one", v);
            latch.countDown();
        }
    });
    Disposable connection = f.connect();
    try {
        if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
            fail("subscriptions did not receive values");
        }
        assertEquals(1, counter.get());
    } finally {
        connection.dispose();
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) HasUpstreamPublisher(io.reactivex.rxjava3.internal.fuseable.HasUpstreamPublisher) Test(org.junit.Test)

Example 43 with Consumer

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

the class RxJavaPluginsTest method overrideConnectableFlowable.

@SuppressWarnings("rawtypes")
@Test
public void overrideConnectableFlowable() {
    try {
        RxJavaPlugins.setOnConnectableFlowableAssembly(new Function<ConnectableFlowable, ConnectableFlowable>() {

            @Override
            public ConnectableFlowable apply(ConnectableFlowable co) throws Exception {
                return new ConnectableFlowable() {

                    @Override
                    public void connect(Consumer connection) {
                    }

                    @Override
                    public void reset() {
                    // nothing to do in this test
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void subscribeActual(Subscriber subscriber) {
                        subscriber.onSubscribe(new ScalarSubscription(subscriber, 10));
                    }
                };
            }
        });
        Flowable.just(1).publish().autoConnect().test().assertResult(10);
    } finally {
        RxJavaPlugins.reset();
    }
    Flowable.just(1).publish().autoConnect().test().assertResult(1);
}
Also used : ConnectableFlowable(io.reactivex.rxjava3.flowables.ConnectableFlowable) ScalarSubscription(io.reactivex.rxjava3.internal.subscriptions.ScalarSubscription) Test(org.junit.Test)

Example 44 with Consumer

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

the class RxJavaPluginsTest method overrideConnectableObservable.

@SuppressWarnings("rawtypes")
@Test
public void overrideConnectableObservable() {
    try {
        RxJavaPlugins.setOnConnectableObservableAssembly(new Function<ConnectableObservable, ConnectableObservable>() {

            @Override
            public ConnectableObservable apply(ConnectableObservable co) throws Exception {
                return new ConnectableObservable() {

                    @Override
                    public void connect(Consumer connection) {
                    }

                    @Override
                    public void reset() {
                    // nothing to do in this test
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    protected void subscribeActual(Observer observer) {
                        observer.onSubscribe(Disposable.empty());
                        observer.onNext(10);
                        observer.onComplete();
                    }
                };
            }
        });
        Observable.just(1).publish().autoConnect().test().assertResult(10);
    } finally {
        RxJavaPlugins.reset();
    }
    Observable.just(1).publish().autoConnect().test().assertResult(1);
}
Also used : Observer(io.reactivex.rxjava3.core.Observer) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Example 45 with Consumer

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

the class MaybeTest method observeOnErrorThread.

@Test
public void observeOnErrorThread() {
    String main = Thread.currentThread().getName();
    final String[] name = { null };
    Maybe.error(new TestException()).observeOn(Schedulers.single()).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            name[0] = Thread.currentThread().getName();
        }
    }).test().awaitDone(5, TimeUnit.SECONDS).assertFailure(TestException.class);
    assertNotEquals(main, name[0]);
}
Also used : ArgsToString(io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString) 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