Search in sources :

Example 31 with CompositeDisposable

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

the class CompositeDisposableTest method success.

@Test
public void success() {
    final AtomicInteger counter = new AtomicInteger();
    CompositeDisposable cd = new CompositeDisposable();
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    cd.dispose();
    assertEquals(2, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 32 with CompositeDisposable

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

the class NewThreadSchedulerTest method shutdownRejects.

@Test
@SuppressUndeliverable
public void shutdownRejects() {
    final int[] calls = { 0 };
    Runnable r = new Runnable() {

        @Override
        public void run() {
            calls[0]++;
        }
    };
    Scheduler s = getScheduler();
    Worker w = s.createWorker();
    w.dispose();
    assertTrue(w.isDisposed());
    assertEquals(Disposable.disposed(), w.schedule(r));
    assertEquals(Disposable.disposed(), w.schedule(r, 1, TimeUnit.SECONDS));
    assertEquals(Disposable.disposed(), w.schedulePeriodically(r, 1, 1, TimeUnit.SECONDS));
    NewThreadWorker actual = (NewThreadWorker) w;
    CompositeDisposable cd = new CompositeDisposable();
    actual.scheduleActual(r, 1, TimeUnit.SECONDS, cd);
    assertEquals(0, cd.size());
    assertEquals(0, calls[0]);
}
Also used : Scheduler(io.reactivex.rxjava3.core.Scheduler) NewThreadWorker(io.reactivex.rxjava3.internal.schedulers.NewThreadWorker) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) NewThreadWorker(io.reactivex.rxjava3.internal.schedulers.NewThreadWorker) Test(org.junit.Test) SuppressUndeliverable(io.reactivex.rxjava3.testsupport.SuppressUndeliverable)

Example 33 with CompositeDisposable

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

the class TrampolineSchedulerTest method nestedTrampolineWithUnsubscribe.

@Test
public void nestedTrampolineWithUnsubscribe() {
    final ArrayList<String> workDone = new ArrayList<>();
    final CompositeDisposable workers = new CompositeDisposable();
    Worker worker = Schedulers.trampoline().createWorker();
    try {
        workers.add(worker);
        worker.schedule(new Runnable() {

            @Override
            public void run() {
                workers.add(doWorkOnNewTrampoline("A", workDone));
            }
        });
        final Worker worker2 = Schedulers.trampoline().createWorker();
        workers.add(worker2);
        worker2.schedule(new Runnable() {

            @Override
            public void run() {
                workers.add(doWorkOnNewTrampoline("B", workDone));
                // we unsubscribe worker2 ... it should not affect work scheduled on a separate Trampline.Worker
                worker2.dispose();
            }
        });
        assertEquals(6, workDone.size());
        assertEquals(Arrays.asList("A.1", "A.B.1", "A.B.2", "B.1", "B.B.1", "B.B.2"), workDone);
    } finally {
        workers.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 34 with CompositeDisposable

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

the class SchedulerLifecycleTest method tryOutSchedulers.

private void tryOutSchedulers() throws InterruptedException {
    final CountDownLatch cdl = new CountDownLatch(4);
    final Runnable countAction = new Runnable() {

        @Override
        public void run() {
            cdl.countDown();
        }
    };
    CompositeDisposable cd = new CompositeDisposable();
    try {
        Worker w1 = Schedulers.computation().createWorker();
        cd.add(w1);
        w1.schedule(countAction);
        Worker w2 = Schedulers.io().createWorker();
        cd.add(w2);
        w2.schedule(countAction);
        Worker w3 = Schedulers.newThread().createWorker();
        cd.add(w3);
        w3.schedule(countAction);
        Worker w4 = Schedulers.single().createWorker();
        cd.add(w4);
        w4.schedule(countAction);
        if (!cdl.await(3, TimeUnit.SECONDS)) {
            fail("countAction was not run by every worker");
        }
    } finally {
        cd.dispose();
    }
}
Also used : Worker(io.reactivex.rxjava3.core.Scheduler.Worker) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable)

Example 35 with CompositeDisposable

use of io.reactivex.rxjava3.disposables.CompositeDisposable in project Signal-Android by signalapp.

the class SignalWebSocket method getWebSocket.

private synchronized WebSocketConnection getWebSocket() throws WebSocketUnavailableException {
    if (!canConnect) {
        throw new WebSocketUnavailableException();
    }
    if (webSocket == null || webSocket.isDead()) {
        webSocketStateDisposable.dispose();
        webSocket = webSocketFactory.createWebSocket();
        webSocketStateDisposable = new CompositeDisposable();
        Disposable state = webSocket.connect().subscribeOn(Schedulers.computation()).observeOn(Schedulers.computation()).subscribe(webSocketState::onNext);
        webSocketStateDisposable.add(state);
    }
    return webSocket;
}
Also used : CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) WebSocketUnavailableException(org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable)

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