Search in sources :

Example 26 with CompositeDisposable

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

the class ScheduledRunnableTest method runFuture.

@Test
public void runFuture() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
        set.add(run);
        final FutureTask<Void> ft = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                run.call();
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                run.setFuture(ft);
            }
        };
        TestHelper.race(r1, r2);
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 27 with CompositeDisposable

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

the class ScheduledRunnableTest method setFutureCancelRace.

@Test
public void setFutureCancelRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        CompositeDisposable set = new CompositeDisposable();
        final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
        set.add(run);
        final FutureTask<Object> ft = new FutureTask<>(Functions.EMPTY_RUNNABLE, 0);
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                run.setFuture(ft);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                run.dispose();
            }
        };
        TestHelper.race(r1, r2);
        assertEquals(0, set.size());
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 28 with CompositeDisposable

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

the class ScheduledRunnableTest method toStringStates.

@Test
public void toStringStates() {
    CompositeDisposable set = new CompositeDisposable();
    ScheduledRunnable task = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
    assertEquals("ScheduledRunnable[Waiting]", task.toString());
    task.set(ScheduledRunnable.THREAD_INDEX, Thread.currentThread());
    assertEquals("ScheduledRunnable[Running on " + Thread.currentThread() + "]", task.toString());
    task.dispose();
    assertEquals("ScheduledRunnable[Disposed(Sync)]", task.toString());
    task.set(ScheduledRunnable.FUTURE_INDEX, ScheduledRunnable.DONE);
    assertEquals("ScheduledRunnable[Finished]", task.toString());
    task = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
    task.dispose();
    assertEquals("ScheduledRunnable[Disposed(Async)]", task.toString());
}
Also used : CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 29 with CompositeDisposable

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

the class ScheduledRunnableTest method withParentDisposed.

@Test
public void withParentDisposed() {
    ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, new CompositeDisposable());
    run.dispose();
    run.call();
}
Also used : CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 30 with CompositeDisposable

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

the class CompositeDisposableTest method unsubscribeIdempotenceConcurrently.

@Test
public void unsubscribeIdempotenceConcurrently() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    final CompositeDisposable cd = new CompositeDisposable();
    final int count = 10;
    final CountDownLatch start = new CountDownLatch(1);
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    final List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    start.await();
                    cd.dispose();
                } catch (final InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        };
        t.start();
        threads.add(t);
    }
    start.countDown();
    for (final Thread t : threads) {
        t.join();
    }
    // we should have only disposed once
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)24 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)4 Disposable (io.reactivex.rxjava3.disposables.Disposable)4 TestException (io.reactivex.rxjava3.exceptions.TestException)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 FutureTask (java.util.concurrent.FutureTask)4 WebSocketUnavailableException (org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException)4 AtomicThrowable (io.reactivex.rxjava3.internal.util.AtomicThrowable)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Scheduler (io.reactivex.rxjava3.core.Scheduler)1 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)1 Amb (io.reactivex.rxjava3.internal.operators.completable.CompletableAmb.Amb)1 TimeOutObserver (io.reactivex.rxjava3.internal.operators.completable.CompletableTimeout.TimeOutObserver)1 NewThreadWorker (io.reactivex.rxjava3.internal.schedulers.NewThreadWorker)1 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)1