Search in sources :

Example 1 with CompositeDisposable

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

the class SignalWebSocket method getUnidentifiedWebSocket.

private synchronized WebSocketConnection getUnidentifiedWebSocket() throws WebSocketUnavailableException {
    if (!canConnect) {
        throw new WebSocketUnavailableException();
    }
    if (unidentifiedWebSocket == null || unidentifiedWebSocket.isDead()) {
        unidentifiedWebSocketStateDisposable.dispose();
        unidentifiedWebSocket = webSocketFactory.createUnidentifiedWebSocket();
        unidentifiedWebSocketStateDisposable = new CompositeDisposable();
        Disposable state = unidentifiedWebSocket.connect().subscribeOn(Schedulers.computation()).observeOn(Schedulers.computation()).subscribe(unidentifiedWebSocketState::onNext);
        unidentifiedWebSocketStateDisposable.add(state);
    }
    return unidentifiedWebSocket;
}
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)

Example 2 with CompositeDisposable

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

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)

Example 3 with CompositeDisposable

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

the class CompositeDisposableTest method exception.

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

        @Override
        public void run() {
            throw new RuntimeException("failed on first one");
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    try {
        cd.dispose();
        fail("Expecting an exception");
    } catch (RuntimeException e) {
        // we expect this
        assertEquals(e.getMessage(), "failed on first one");
    }
    // we should still have disposed to the second one
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 4 with CompositeDisposable

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

the class CompositeDisposableTest method unsubscribeIdempotence.

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

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    cd.dispose();
    cd.dispose();
    cd.dispose();
    // we should have only disposed once
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 5 with CompositeDisposable

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

the class CompositeDisposableTest method compositeException.

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

        @Override
        public void run() {
            throw new RuntimeException("failed on first one");
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            throw new RuntimeException("failed on second one too");
        }
    }));
    cd.add(Disposable.fromRunnable(new Runnable() {

        @Override
        public void run() {
            counter.incrementAndGet();
        }
    }));
    try {
        cd.dispose();
        fail("Expecting an exception");
    } catch (CompositeException e) {
        // we expect this
        assertEquals(e.getExceptions().size(), 2);
    }
    // we should still have disposed to the second one
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositeException(io.reactivex.rxjava3.exceptions.CompositeException) 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