Search in sources :

Example 41 with Disposable

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

the class ExecutorSchedulerInterruptibleTest method interruptibleDirectTask.

@Test
public void interruptibleDirectTask() throws Exception {
    Scheduler scheduler = getScheduler();
    final AtomicInteger sync = new AtomicInteger(2);
    final AtomicBoolean isInterrupted = new AtomicBoolean();
    Disposable d = scheduler.scheduleDirect(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());
}
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 42 with Disposable

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

the class ExecutorSchedulerInterruptibleTest method interruptibleDirectTaskScheduledExecutor.

@Test
public void interruptibleDirectTaskScheduledExecutor() throws Exception {
    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    try {
        Scheduler scheduler = Schedulers.from(exec, true);
        final AtomicInteger sync = new AtomicInteger(2);
        final AtomicBoolean isInterrupted = new AtomicBoolean();
        Disposable d = scheduler.scheduleDirect(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 {
        exec.shutdown();
    }
}
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 43 with Disposable

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

the class ExecutorSchedulerInterruptibleTest method cancelledTasksDontRun.

@Test
public void cancelledTasksDontRun() {
    final AtomicInteger calls = new AtomicInteger();
    Runnable task = new Runnable() {

        @Override
        public void run() {
            calls.getAndIncrement();
        }
    };
    TestExecutor exec = new TestExecutor();
    Scheduler custom = Schedulers.from(exec, true);
    Worker w = custom.createWorker();
    try {
        Disposable d1 = w.schedule(task);
        Disposable d2 = w.schedule(task);
        Disposable d3 = w.schedule(task);
        d1.dispose();
        d2.dispose();
        d3.dispose();
        exec.executeAll();
        assertEquals(0, calls.get());
    } finally {
        w.dispose();
    }
}
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 44 with Disposable

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

the class ExecutorSchedulerInterruptibleTest method interruptibleWorkerTask.

@Test
public void interruptibleWorkerTask() throws Exception {
    Scheduler scheduler = getScheduler();
    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();
    }
}
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 45 with Disposable

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

the class ExecutorSchedulerInterruptibleTest method runnableDisposedAsyncTimed.

@Test
public void runnableDisposedAsyncTimed() 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, 1, TimeUnit.MILLISECONDS);
    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)

Aggregations

Test (org.junit.Test)251 Disposable (io.reactivex.rxjava3.disposables.Disposable)236 TestException (io.reactivex.rxjava3.exceptions.TestException)74 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)56 IOException (java.io.IOException)38 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 LifecycleDisposable (org.thoughtcrime.securesms.util.LifecycleDisposable)8 Observable (io.reactivex.rxjava3.core.Observable)6 Observer (io.reactivex.rxjava3.core.Observer)6 ScalarDisposable (io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable)6