Search in sources :

Example 56 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest method rejectingExecutor.

@Test
public void rejectingExecutor() {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.shutdown();
    Scheduler s = Schedulers.from(exec, true);
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE));
        assertSame(EmptyDisposable.INSTANCE, s.scheduleDirect(Functions.EMPTY_RUNNABLE, 10, TimeUnit.MILLISECONDS));
        assertSame(EmptyDisposable.INSTANCE, s.schedulePeriodicallyDirect(Functions.EMPTY_RUNNABLE, 10, 10, TimeUnit.MILLISECONDS));
        TestHelper.assertUndeliverable(errors, 0, RejectedExecutionException.class);
        TestHelper.assertUndeliverable(errors, 1, RejectedExecutionException.class);
        TestHelper.assertUndeliverable(errors, 2, RejectedExecutionException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 57 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest method nonInterruptibleWorkerTask.

@Test
public void nonInterruptibleWorkerTask() throws Exception {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    try {
        Scheduler scheduler = Schedulers.from(exec, false);
        Worker worker = scheduler.createWorker();
        try {
            final AtomicInteger sync = new AtomicInteger(2);
            final AtomicBoolean isInterrupted = new AtomicBoolean();
            Disposable d = worker.schedule(new Runnable() {

                @Override
                public void run() {
                    if (sync.decrementAndGet() != 0) {
                        while (sync.get() != 0) {
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        isInterrupted.set(true);
                    }
                }
            });
            if (sync.decrementAndGet() != 0) {
                while (sync.get() != 0) {
                }
            }
            Thread.sleep(500);
            d.dispose();
            int i = 20;
            while (i-- > 0 && !isInterrupted.get()) {
                Thread.sleep(50);
            }
            assertFalse("Interruption happened", isInterrupted.get());
        } finally {
            worker.dispose();
        }
    } finally {
        exec.shutdown();
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 58 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest method nonInterruptibleWorkerTaskScheduledExecutorTimed.

@Test
public void nonInterruptibleWorkerTaskScheduledExecutorTimed() throws Exception {
    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    try {
        Scheduler scheduler = Schedulers.from(exec, false);
        Worker worker = scheduler.createWorker();
        try {
            final AtomicInteger sync = new AtomicInteger(2);
            final AtomicBoolean isInterrupted = new AtomicBoolean();
            Disposable d = worker.schedule(new Runnable() {

                @Override
                public void run() {
                    if (sync.decrementAndGet() != 0) {
                        while (sync.get() != 0) {
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        isInterrupted.set(true);
                    }
                }
            }, 1, TimeUnit.MILLISECONDS);
            if (sync.decrementAndGet() != 0) {
                while (sync.get() != 0) {
                }
            }
            Thread.sleep(500);
            d.dispose();
            int i = 20;
            while (i-- > 0 && !isInterrupted.get()) {
                Thread.sleep(50);
            }
            assertFalse("Interruption happened", isInterrupted.get());
        } finally {
            worker.dispose();
        }
    } finally {
        exec.shutdown();
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 59 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest method runnableDisposedAsyncTimed2.

@Test
public void runnableDisposedAsyncTimed2() throws Exception {
    ExecutorService executorScheduler = Executors.newScheduledThreadPool(1, new RxThreadFactory("TestCustomPoolTimed"));
    try {
        final Scheduler s = Schedulers.from(executorScheduler, true);
        Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE, 1, TimeUnit.MILLISECONDS);
        while (!d.isDisposed()) {
            Thread.sleep(1);
        }
    } finally {
        executorScheduler.shutdownNow();
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) RxThreadFactory(io.reactivex.rxjava3.internal.schedulers.RxThreadFactory) Test(org.junit.Test)

Example 60 with Scheduler

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

the class ExecutorSchedulerInterruptibleTest method nonInterruptibleWorkerTaskScheduledExecutor.

@Test
public void nonInterruptibleWorkerTaskScheduledExecutor() throws Exception {
    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    try {
        Scheduler scheduler = Schedulers.from(exec, false);
        Worker worker = scheduler.createWorker();
        try {
            final AtomicInteger sync = new AtomicInteger(2);
            final AtomicBoolean isInterrupted = new AtomicBoolean();
            Disposable d = worker.schedule(new Runnable() {

                @Override
                public void run() {
                    if (sync.decrementAndGet() != 0) {
                        while (sync.get() != 0) {
                        }
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        isInterrupted.set(true);
                    }
                }
            });
            if (sync.decrementAndGet() != 0) {
                while (sync.get() != 0) {
                }
            }
            Thread.sleep(500);
            d.dispose();
            int i = 20;
            while (i-- > 0 && !isInterrupted.get()) {
                Thread.sleep(50);
            }
            assertFalse("Interruption happened", isInterrupted.get());
        } finally {
            worker.dispose();
        }
    } finally {
        exec.shutdown();
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

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