Search in sources :

Example 61 with Function

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

the class FlowableRepeatTest method noCancelPreviousRepeatWhen.

@Test
public void noCancelPreviousRepeatWhen() {
    final AtomicInteger counter = new AtomicInteger();
    Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    final AtomicInteger times = new AtomicInteger();
    source.repeatWhen(new Function<Flowable<Object>, Flowable<?>>() {

        @Override
        public Flowable<?> apply(Flowable<Object> e) throws Exception {
            return e.takeWhile(new Predicate<Object>() {

                @Override
                public boolean test(Object v) throws Exception {
                    return times.getAndIncrement() < 4;
                }
            });
        }
    }).test().assertResult(1, 1, 1, 1, 1);
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) Flowable(io.reactivex.rxjava3.core.Flowable) Test(org.junit.Test)

Example 62 with Function

use of io.reactivex.rxjava3.functions.Function 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 63 with Function

use of io.reactivex.rxjava3.functions.Function 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 64 with Function

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

the class RxJavaPluginsTest method createNewThreadScheduler.

@Test
public void createNewThreadScheduler() {
    final String name = "NewThreadSchedulerTest";
    ThreadFactory factory = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, name);
        }
    };
    final Scheduler customScheduler = RxJavaPlugins.createNewThreadScheduler(factory);
    RxJavaPlugins.setNewThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {

        @Override
        public Scheduler apply(Scheduler scheduler) throws Exception {
            return customScheduler;
        }
    });
    try {
        verifyThread(Schedulers.newThread(), name);
    } finally {
        customScheduler.shutdown();
        RxJavaPlugins.reset();
    }
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Example 65 with Function

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

the class RxJavaPluginsTest method observableCreate.

@SuppressWarnings("rawtypes")
@Test
public void observableCreate() {
    try {
        RxJavaPlugins.setOnObservableAssembly(new Function<Observable, Observable>() {

            @Override
            public Observable apply(Observable t) {
                return new ObservableRange(1, 2);
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : ObservableRange(io.reactivex.rxjava3.internal.operators.observable.ObservableRange) Observable(io.reactivex.rxjava3.core.Observable) ConnectableObservable(io.reactivex.rxjava3.observables.ConnectableObservable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5