Search in sources :

Example 61 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable 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)

Example 62 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class ExecutorSchedulerInterruptibleTest method interruptibleWorkerTaskScheduledExecutor.

@Test
public void interruptibleWorkerTaskScheduledExecutor() throws Exception {
    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    try {
        Scheduler scheduler = Schedulers.from(exec, true);
        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);
            }
            assertTrue("Interruption did not propagate", 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 63 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class ExecutorSchedulerInterruptibleTest method runnableDisposedAsync.

@Test
public void runnableDisposedAsync() throws Exception {
    final Scheduler s = Schedulers.from(new Executor() {

        @Override
        public void execute(Runnable r) {
            new Thread(r).start();
        }
    }, true);
    Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE);
    while (!d.isDisposed()) {
        Thread.sleep(1);
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Scheduler(io.reactivex.rxjava3.core.Scheduler) Test(org.junit.Test)

Example 64 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class TestSchedulerTest method immediateUnsubscribes2.

@Test
public final void immediateUnsubscribes2() {
    TestScheduler s = new TestScheduler();
    final Scheduler.Worker inner = s.createWorker();
    try {
        final AtomicInteger counter = new AtomicInteger(0);
        final Disposable subscription = inner.schedule(new Runnable() {

            @Override
            public void run() {
                counter.incrementAndGet();
                System.out.println("counter: " + counter.get());
                inner.schedule(this);
            }
        });
        subscription.dispose();
        assertEquals(0, counter.get());
    } finally {
        inner.dispose();
    }
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 65 with Disposable

use of io.reactivex.rxjava3.disposables.Disposable in project RxJava by ReactiveX.

the class RxJavaPluginsTest method observableStart.

@SuppressWarnings("rawtypes")
@Test
public void observableStart() {
    try {
        RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() {

            @Override
            public Observer apply(Observable o, final Observer t) {
                return new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        t.onSubscribe(d);
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object value) {
                        t.onNext((Integer) value - 9);
                    }

                    @Override
                    public void onError(Throwable e) {
                        t.onError(e);
                    }

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

Aggregations

Test (org.junit.Test)250 Disposable (io.reactivex.rxjava3.disposables.Disposable)219 TestException (io.reactivex.rxjava3.exceptions.TestException)74 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)56 IOException (java.io.IOException)37 Scheduler (io.reactivex.rxjava3.core.Scheduler)36 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)35 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)34 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)32 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TestObserver (io.reactivex.rxjava3.observers.TestObserver)18 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)15 QueueDisposable (io.reactivex.rxjava3.operators.QueueDisposable)13 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)10 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)9 Observable (io.reactivex.rxjava3.core.Observable)6 Observer (io.reactivex.rxjava3.core.Observer)6 ScalarDisposable (io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable)6 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)6