Search in sources :

Example 91 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class RxJavaPluginsTest method overrideInitIoScheduler.

@Test
public void overrideInitIoScheduler() {
    // make sure the Schedulers is initialized;
    final Scheduler s = Schedulers.io();
    Supplier<Scheduler> c = new Supplier<Scheduler>() {

        @Override
        public Scheduler get() throws Exception {
            return s;
        }
    };
    try {
        RxJavaPlugins.setInitIoSchedulerHandler(initReplaceWithImmediate);
        assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initIoScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    assertSame(s, RxJavaPlugins.initIoScheduler(c));
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Example 92 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class AbstractSchedulerConcurrencyTests method unsubscribeRecursiveScheduleFromOutside.

@Test
public void unsubscribeRecursiveScheduleFromOutside() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch unsubscribeLatch = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();
    final Worker inner = getScheduler().createWorker();
    try {
        inner.schedule(new Runnable() {

            @Override
            public void run() {
                inner.schedule(new Runnable() {

                    int i;

                    @Override
                    public void run() {
                        System.out.println("Run: " + i++);
                        if (i == 10) {
                            latch.countDown();
                            try {
                                // wait for unsubscribe to finish so we are not racing it
                                unsubscribeLatch.await();
                            } catch (InterruptedException e) {
                            // we expect the countDown if unsubscribe is not working
                            // or to be interrupted if unsubscribe is successful since
                            // the unsubscribe will interrupt it as it is calling Future.cancel(true)
                            // so we will ignore the stacktrace
                            }
                        }
                        counter.incrementAndGet();
                        inner.schedule(this);
                    }
                });
            }
        });
        latch.await();
        inner.dispose();
        unsubscribeLatch.countDown();
        // let time pass to see if the scheduler is still doing work
        Thread.sleep(200);
        assertEquals(10, counter.get());
    } finally {
        inner.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 93 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class FlowableBufferTimed method subscribeActual.

@Override
protected void subscribeActual(Subscriber<? super U> s) {
    if (timespan == timeskip && maxSize == Integer.MAX_VALUE) {
        source.subscribe(new BufferExactUnboundedSubscriber<>(new SerializedSubscriber<>(s), bufferSupplier, timespan, unit, scheduler));
        return;
    }
    Scheduler.Worker w = scheduler.createWorker();
    if (timespan == timeskip) {
        source.subscribe(new BufferExactBoundedSubscriber<>(new SerializedSubscriber<>(s), bufferSupplier, timespan, unit, maxSize, restartTimerOnMaxSize, w));
        return;
    }
    // Can't use maxSize because what to do if a buffer is full but its
    // timespan hasn't been elapsed?
    source.subscribe(new BufferSkipBoundedSubscriber<>(new SerializedSubscriber<>(s), bufferSupplier, timespan, timeskip, unit, w));
}
Also used : SerializedSubscriber(io.reactivex.rxjava3.subscribers.SerializedSubscriber) Worker(io.reactivex.rxjava3.core.Scheduler.Worker)

Example 94 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class ConnectableFlowable method connect.

/**
 * Instructs the {@code ConnectableFlowable} to begin emitting the items from its underlying
 * {@link Flowable} to its {@link Subscriber}s.
 * <p>
 * To disconnect from a synchronous source, use the {@link #connect(io.reactivex.rxjava3.functions.Consumer)} method.
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>The behavior is determined by the implementor of this abstract class.</dd>
 * </dl>
 *
 * @return the subscription representing the connection
 * @see <a href="http://reactivex.io/documentation/operators/connect.html">ReactiveX documentation: Connect</a>
 */
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final Disposable connect() {
    ConnectConsumer cc = new ConnectConsumer();
    connect(cc);
    return cc.disposable;
}
Also used : ConnectConsumer(io.reactivex.rxjava3.internal.util.ConnectConsumer)

Example 95 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class CallableAsyncPerf method setup.

@Setup
public void setup() {
    Scheduler s = Schedulers.single();
    Scheduler s2 = new SingleScheduler();
    Callable<Integer> c = new Callable<Integer>() {

        @Override
        public Integer call() {
            return 1;
        }
    };
    subscribeOnFlowable = Flowable.fromCallable(c).subscribeOn(s);
    observeOnFlowable = Flowable.fromCallable(c).observeOn(s);
    pipelineFlowable = Flowable.fromCallable(c).subscribeOn(s).observeOn(s2);
    // ----
    subscribeOnObservable = Observable.fromCallable(c).subscribeOn(s);
    observeOnObservable = Observable.fromCallable(c).observeOn(s);
    pipelineObservable = Observable.fromCallable(c).subscribeOn(s).observeOn(s2);
    // ----
    observeOnSingle = Single.fromCallable(c).observeOn(s);
    subscribeOnSingle = Single.fromCallable(c).subscribeOn(s);
    pipelineSingle = Single.fromCallable(c).subscribeOn(s).observeOn(s2);
    // ----
    observeOnCompletable = Completable.fromCallable(c).observeOn(s);
    subscribeOnCompletable = Completable.fromCallable(c).subscribeOn(s);
    pipelineCompletable = Completable.fromCallable(c).subscribeOn(s).observeOn(s2);
    // ----
    observeOnMaybe = Maybe.fromCallable(c).observeOn(s);
    subscribeOnMaybe = Maybe.fromCallable(c).subscribeOn(s);
    pipelineMaybe = Maybe.fromCallable(c).subscribeOn(s).observeOn(s2);
}
Also used : SingleScheduler(io.reactivex.rxjava3.internal.schedulers.SingleScheduler) SingleScheduler(io.reactivex.rxjava3.internal.schedulers.SingleScheduler)

Aggregations

Test (org.junit.Test)169 Disposable (io.reactivex.rxjava3.disposables.Disposable)69 Scheduler (io.reactivex.rxjava3.core.Scheduler)61 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)54 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)50 TestException (io.reactivex.rxjava3.exceptions.TestException)34 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)32 InOrder (org.mockito.InOrder)32 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)20 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)8 Observable (io.reactivex.rxjava3.core.Observable)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Action (io.reactivex.rxjava3.functions.Action)5 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5